Unix 17
Unix 17
Introduction to Matlab
Basic Matlab Commands and Syntax
This document teaches the user how to create Matlab matrices, learn about Matlab plots and printing,
and discover how to use mathematical equations in Matlab, and to acquire rudimentary Matlab skills.
Graphing................................................................................................................... 5
Creating the Graph............................................................................................ 5
Printing the Graph............................................................................................. 6
M-files ...................................................................................................................... 6
Scripts and Functions........................................................................................ 6
Control Flow ..................................................................................................... 7
For Loops................................................................................................... 7
While.......................................................................................................... 7
If Then ....................................................................................................... 7
Function Functions................................................................................................... 7
Numerical Integration ....................................................................................... 7
Finding Zeros .................................................................................................... 8
Differential Equation Solving ........................................................................... 8
2
What is Pro-Matlab?
What is Pro-Matlab?
Pro-Matlab (Matlab) is an interactive software package from the Math Works Inc. Matlab stands for Matrix Labora-
tory. It will define and perform operations on both numbers and characters. There are a number of higher math func-
tions, such as filtering and numeric integration, built into the package. Matlab also has a graphics interface, and the
capablility for programming through M-files.
% matlab
in an xterm. Another is to use the right button menu option for Matlab. The test accounts used for this course have this
option. If you have a personal Unix account on RUF, IS, or Owlnet and you do not have Matlab as a menu option, you
can add the following line to your .twmrc file.
Try one of these methods to start Matlab. Exit, and try the other. Please do not run more than one copy of Matlab at
any given time on this system, as we only have a limited number of licenses, and if you run two another user may not
be able to run any.
3
Mathematical Functions
who This command lists the names of all currently defined variables.
Workspace Commands
save Saves all of the defined variables into a workspace called matlab.mat.
diary Saves everything you see in the Matlab window into a text file.
Mathematical Functions
Basic Math
* Performs multiplication.
^ Performs exponentiation.
For scalar-scalar or scalar-matrix operations these perform as we expect them to. For matrix-matrix operations Mat-
lab assumes it is attempting to perform matrix math operations. To perform element to element operations you must
place a . before the operator.
Note: if the left hand argument is an integer, there should be a space between the integer and the . operator, or else two
..’S.
4
Graphing
Polynomials
There are a number of operations we may perform involving polynomials. One of these is root finding. Matlab will
automatically find the roots of a polynomial with the roots function. The syntax is:
roots(coeff)
Polynomials are also useful in data fitting. Given two vectors of experimental points we can fit different orders of
polynomial to them using polyfit, and polyval. polyfit(x,y,n) returns the coefficients of a polynomial of order n that
fits x to y. polyval(c,x) returns the value of a polynomial in x with coefficients c. Try out polyfit and polyval on the
vectors xdata and ydata in the workspace test1. Compare the ydata values and the yexpected values for several orders
of polynomial.
To solve we need to divide. However, the matrix inner dimensions do not agree, so we can't. We need to transpose the
vector, using a single quote.
vec=vec’
Graphing
5
M-files
will plot 3 curves: y versus x, z versus x and d versus w. Each of the lines will be a different linetype. The axes will be
set to accomodate all 3 lines fully. Each new plot command clears the screen before plotting. If you wish to overlay
curves made from multiple plot commands you need to use the hold command. It may be used alone, as a toggle, or
with on and off to specify hold on, hold off. To plot points, instead of lines, you give the plot command an extra argu-
ment, that being the point style you wish to use. For instance:
plot(x,y,'o',x,z,'+')
will plot y versus x points as open circles, and z versus x points as +'s.
Warning: Matlab will automatically furnish you with a graphics window. Do NOT kill this window. It will exit
automatically when you leave Matlab. If you kill the graphics window and then try to plot again, Matlab will
spontaneously exit, taking all your hard work with it.
The graph may also be labeled. To label the x and y axes we use the xlabel and ylabel commands. For a title, we use
the title command.
M-files
function y=fun1(x)
Functions may be passed arguments, and may return results. To invoke a script or a function you simply type the file-
name (without the .m extension) into the Matlab window. You will also notice that m-files which you create are
included in the help listing. If you perform a help on a specific m-file, help will return any comments which appear
6
Function Functions
Control Flow
Matlab does have flow control statements. Although these may be used in an interactive fashion, they are frequently
less confusing and generally more useful in conjuntion with m-file programming. There are three main constructions.
For Loops
for i = 1:n,
v(i)=i+2
end
While
while n~=1
n=n/2
end
If Then
Matlab also has the conditional if, elseif, then statements. For example,
if i ==j,
a(i,j) = 2;
elseif (i-j) == 1,
a(i,j) = -1;
else
a(i,j) = 0;
end
Function Functions
There is a class of Matlab routines which require you to define a function to use them. Several of them are worth
examining.
Numerical Integration
There are two numerical integration functions built into Matlab. These are quad and quad 8. The syntax for both of
them is:
quad (‘f’,a,b)
Where a and b define the range to integrate the function over, and 'f’ comes from an m-file called f.m which returns
the value of f(x) .
7
Problems or Questions
Finding Zeros
There are two routines for finding the zeros of functions. These are fmin, for 1 variable functions, and fmins, for mul-
tivariable functions. Fmin has the same syntax as quad and quad8. Since fmins uses a simplex routine it needs only a
starting point, not a range to work in.
fmin ( ‘f’,a,b)
fmins (‘f’,x)
where yprime is an m-file containing the system of ode's, t0 and tf are the interval to solve over, and y0 are the initial
conditions to apply.
Problems or Questions
Undergraduates:
If you have a problem, contact your computing support representative by sending an e-mail message to prob-
[email protected] detailing your question. Your query is automatically assigned to your College Computing Associate
(CCA).
If you need immediate assistance during normal business hours, you can call the Consulting Center at 713.348.4983.
During the semester, the Consulting Center has limited evening and weekend hours as well.
To report emergencies, which are urgent system-wide problems (i.e.: all Wiess' network connections are down or all
the PCs in a lab are non-functional), contact the Operations Center at 713.348.4989. Staff work 24 hours a day, 365
day a year and can page appropriate administrators for major network or computing problems.
More information is available at http://www.rice.edu/Computer/student.html