How To Use MATLAB
How To Use MATLAB
How To Use MATLAB
What is MATLAB
MATLAB is an interactive package for numerical analysis, matrix computation, control system design
and linear system analysis and design. On the server ”bass”, MATLAB version 4.0 is available. In
addition to the standard functions provided by MATLAB, there are seven ”toolboxes”, or collections
of functions and procedures, available as part of the MATLAB package:
• Control System Toolbox.
• System Identification Toolbox.
• Neural Network Toolbox.
• Optimization Toolbox.
• Robust Control Toolbox.
• Signal Processing Toolbox.
• Spline Toolbox.
• SIMULINK
Getting Started
To start MATLAB on any Unix workstation or X-terminal, type matlab. A >> prompt will appear
to indicate that you are in MATLAB. To terminate MATLAB, type quit. Notice that MATLAB is
case sensitive, so all commands must be entered in lower case.
Online Help
During any MATLAB session, online help for a variety of topics is available. To see a list of help
topics, type help. For help on a specific topic, type help followed by the directory/topic name (e.g.
help matlab/general for General purpose commands), which will give you all command name in
the topic. For help on a specific command, type help command name (e.g. help who).
Once a diary command is issued, all subsequent commands and output are written to a transcript
file. diary <fname> open a transcript file named fname. diary off suspends it. diary on turns it
back on. Close the transcript file using the diary off command at the end of the session.
save <fname> saves all workspace variables to the binary ”MAT-file” named fname.mat. The data
may be retrieved with command load <fname>. save <fname > X Y Z save only variables X, Y,
and Z. Notice that if you only want to save several variables, make sure to put variable names after
command save <fname>.
clear removes all variables from the workspace. clear X removes variable or function X from the
workspace.
All computations in MATLAB are done in double precision. The format of the displayed output can
be controlled by the following commands.
During MATLAB session, Unix commands can be executed by using !Unix- command
Defining Variables
The basic variable types in MATLAB are scalar, vector, and matrix. Variables are defined by typing
the name of the variable followed by an equal sign and the value of the variable. The values of a
vector or matrix are enclosed by brackets, and rows of matrices are separated by semicolons. For
example:
Displaying Data
The values of a variable can be displayed by simply typing the variable name. In matrix case, a
specific element, row or column of a matrix can be examined separately. A single element of a matrix
can be addressed by typing the name of the matrix followed by the row and column indices of the
element in parentheses. To address an entire column or row, a colon can be substituted for either
index. For example:
for i =1:n
x(i)=iˆ2;
end
while relation
statements
end
The statements will be repeatedly executed as long as the relation remains true. For example, for a
given number a, the following will compute the smallest nonnegative integer n such that 2n ≥ a:
n= 0;
while 2ˆn < a
n=n+1;
end
if n < 0
x(n)=0;
elseif rem(n,2)==0
x(n)=2;
else
x(n)=1;
end
Note that ”=” is used in an assignment statement while ”==” is used in a relation. Relations may
be connected or quantified by the logical operators
& and; — or; ∼ not;
The plot command creates linear x-y plots;if x and y are vectors of the same length, the command
plot(x,y) open a graph window and draws an x-y plot of the elements of x versus the elements of y.
You can, for example, draw the graph of the sine function over the interval -4 to 4 with the following
commands:
x= -4:.01:4;
y=sin(x);
plot(x,y)
A hardcopy of the graph window can be obtained with the MATLAB command print. The command
print filename saves the current graph window in PostScript form to the designated filename, over-
writing it if it already exists. The filename extension ”.ps” is appended to the filename if an extension
is not supplied.
mesh
Three dimensional mesh surface plots are drawn with the function mesh. The command mesh(z)
creates a three-dimensional plot of the elements of the matrix z. The mesh surface is defined by the
z-coordinates of points above a rectangular grid in the x-y plane.
M-files
MATLAB can execute a sequence of statement stored on diskfiles. Such files are called ”M-files”
because they must have ”.m” as extension name (e.g. filename.m). A M-files consists of a sequence of
normal MATLAB statements. If the file has the filename, say, test.m, then the MATLAB command
test will cause the statements in the file to be executed. Variables in a M-file are global and will
change the value of the environment.
In an M-file the user can be prompted to interactively enter input data with the function input.
When, for example, the statement
is encountered, the prompt message is displayed and execution pauses while the user keys in the input
data. Upon pressing the return key, the data is assigned to the variable iter and execution resumes.
Text strings can be displayed with the function disp. For example:
All editing tools on Unix machine, such as vi and emac, can be used to edit M-file.