Engineering Project: Introduction of Matlab

advertisement
1. Introduction
MATLAB is a high-level technical computing language and interactive
environment for algorithm development, data visualization, data analysis, and numerical
computation. Using MATLAB, you can solve technical computing problems faster than
with traditional programming languages, such as C, C++, and Fortran.
2. Field of Matlab
Multivariable Calculus
Computational
Programming
Control Systems
Fluid Mechanics
Speech Processing
Neurology
Vibration Analysis
Physics
Ordinary Differential
Equations
Partial Differential Equations
Numerical Analysis
Computational Mechanics
Electromechanical Dynamics
Digital Signal Processing
Fuzzy Controls
Geology
Robotics
Economics
Flight Dynamics and Control
Image Processing
Process Control
Oceanography
Instrumentation and Measurement
Soft Computing
3. Install Matlab
1) Many PC at UTA already has Matlab.
2) MatLab education version $99.
3) Please do not use illegal software
4) Install Matlab and run.
Exercise 1 Getting Start Matlab
Program Development
This section explains the basic steps in developing an M-file program in MATLAB. Associated
with each step in this process are certain MATLAB tools and utilities that are fully documented in
the Desktop Tools and Development Environment documentation.
For more ideas on good programming style, see Program Development. The MATLAB
Programming Tips is a compilation of useful pieces of information that can show you alternate
and often more efficient ways to accomplish common programming tasks while also expanding
your knowledge of MATLAB.
Creating a Program
You can type in your program code using any text editor. This section focuses on using the
MATLAB Editor/Debugger for this purpose. The Editor/Debugger is fully documented in Ways to
Edit and Debug Files in the Desktop Tools and Development Environment documentation.
The first step in creating a program is to open an editing window. To create a new M-file, type the
word edit at the MATLAB command prompt. To edit an existing M-file, type edit followed by
the filename:
edit drawPlot.m
MATLAB opens a new window for entering your program code. As you type in your program,
MATLAB keeps track of the line numbers in the left column.
Saving the Program
It is usually a good idea to save your program periodically while you are in the development
process. To do this, click File -> Save in the Editor/Debugger. Enter a filename with a .m
extension in the Save file as dialog box that appears and click OK. It is customary and less
confusing if you give the M-file the same name as the first function in the M-file.
Running the Program
Before trying to run your program, make sure that its M-file is on the MATLAB path. The MATLAB
path defines those directories that you want MATLAB to know about when executing M-files. The
path includes all the directories that contain functions provided with MATLAB. It should also
include any directories that you use for your own functions.
Use the which function to see if your program is on the path:
which drawPlot
D:\matlabR14\work\drawPlot.m
If not, add its directory to the path using the addpath function:
addpath('D:\matlabR14\work')
Now you can run the program just by typing the name of the M-file at the MATLAB command
prompt:
drawPlot(xdata, ydata)
Basic Parts of an M-File
This simple function shows the basic parts of an M-file. Note that any line that begins with % is not
executable:
function f = fact(n)
% Compute a factorial value.
% FACT(N) returns the factorial of N,
% usually denoted by N!
Function definition line
H1 line
Help text
% Put simply, FACT(N) is PROD(1:N).
f = prod(1:n);
Comment
Function body
The table below briefly describes each of these M-file parts. Both functions and scripts can have
all of these parts, except for the function definition line which applies to functions only. These
parts are described in greater detail following the table.
M-File Element
Description
Function
definition line
(functions only)
Defines the function name, and the number and order of input and output
arguments
H1 line
A one line summary description of the program, displayed when you request
help on an entire directory, or when you use lookfor
Help text
A more detailed description of the program, displayed together with the H1
line when you request help on a specific function
Function or script Program code that performs the actual computations and assigns values to
body
any output arguments
Comments
Text in the body of the program that explains the internal workings of the
program
Function Definition Line
The function definition line informs MATLAB that the M-file contains a function, and specifies the
argument calling sequence of the function. The function definition line for the fact function is
All MATLAB functions have a function definition line that follows this pattern.
Function Name. Function names must begin with a letter, may contain any alphanumeric
characters or underscores, and must be no longer than the maximum allowed length (returned by
the function namelengthmax). Because variables must obey similar rules, you can use the
isvarname function to check whether a function name is valid:
isvarname myfun
Although function names can be of any length, MATLAB uses only the first N characters of the
name (where N is the number returned by the function namelengthmax) and ignores the rest.
Hence, it is important to make each function name unique in the first N characters:
N = namelengthmax
N =
63
Note
Some operating systems may restrict file names to shorter lengths.
The name of the text file that contains a MATLAB function consists of the function name with the
extension .m appended. For example,
average.m
If the filename and the function definition line name are different, the internal (function) name is
ignored. Thus, if average.m is the file that defines a function named computeAverage, you
would invoke the function by typing
average
Note While the function name specified on the function definition line does not have to
be the same as the filename, it is best to use the same name for both to avoid confusion.
Function Arguments. If the function has multiple output values, enclose the output argument
list in square brackets. Input arguments, if present, are enclosed in parentheses following the
function name. Use commas to separate multiple input or output arguments. Here is the
declaration for a function named sphere that has three inputs and three outputs:
function [x, y, z] = sphere(theta, phi, rho)
If there is no output, leave the output blank
function printresults(x)
or use empty square brackets:
function [] = printresults(x)
The variables that you pass to the function do not need to have the same name as those in the
function definition line.
The H1 Line
The H1 line, so named because it is the first help text line, is a comment line immediately
following the function definition line. Because it consists of comment text, the H1 line begins with
a percent sign, %. For the average function, the H1 line is
% AVERAGE Mean of vector elements.
This is the first line of text that appears when a user types help functionname at the MATLAB
prompt. Further, the lookfor function searches on and displays only the H1 line. Because this
line provides important summary information about the M-file, it is important to make it as
descriptive as possible.
Help Text
You can create online help for your M-files by entering help text on one or more consecutive
comment lines at the start of your M-file program. MATLAB considers the first group of
consecutive lines immediately following the H1 line that begin with % to be the online help text for
the function. The first line without % as the left-most character ends the help.
The help text for the average function is
% AVERAGE(X), where X is a vector, is the mean of vector elements.
% Nonvector input results in an error.
When you type help functionname at the command prompt, MATLAB displays the H1 line
followed by the online help text for that function. The help system ignores any comment lines that
appear after this help block.
Note Help text in an M-file can be viewed at the MATLAB command prompt only (using
help functionname). You cannot display this text using the MATLAB Help browser.
You can, however, use the Help browser to get help on MATLAB functions and also to
read the documentation on any MathWorks products.
The Function or Script Body
The function body contains all the MATLAB code that performs computations and assigns values
to output arguments. The statements in the function body can consist of function calls,
programming constructs like flow control and interactive input/output, calculations, assignments,
comments, and blank lines.
For example, the body of the average function contains a number of simple programming
statements:
[m,n] = size(x);
if (~((m == 1) | (n == 1)) | (m == 1 & n == 1)) % Flow control
error('Input must be a vector') % Error message display
end
y = sum(x)/length(x);
% Computation and assignment
Comments
As mentioned earlier, comment lines begin with a percent sign (%). Comment lines can appear
anywhere in an M-file, and you can append comments to the end of a line of code. For example,
% Add up all the vector elements.
y = sum(x)
% Use the sum function.
In addition to comment lines, you can insert blank lines anywhere in an M-file. Blank lines are
ignored. However, a blank line can indicate the end of the help text entry for an M-file.
Block Comments. To write comments that require more than one line, use the block comment
operators, %{ and %}:
%{
This next block of code checks the number of inputs
passed in, makes sure that each input is a valid data
type, and then branches to start processing the data.
%}
Note The %{ and %} operators must appear alone on the lines that immediately
precede and follow the block of help text. Do not include any other text on these lines.
Download