Apk
Apk
This document teaches the user how to create Matlab matrices, learn about
Matlab plots and printing,
Document may not be resold to, used by, nor licensed to third parties without
written permission from User Services, Rice University.2
Table of Contents
What is Pro-
Matlab? ................................................................................................ 3
Mathematical
Functions ........................................................................................... 4
Polynomials.......................................................................................................
5
M-
files ....................................................................................................................
.. 6
For Loops................................................................................................... 7
While.......................................................................................................... 7
If Then ....................................................................................................... 7
Function
Functions................................................................................................... 7
Problems or
Questions ............................................................................................. 8
Undergraduates: ...............................................................................................
.8
comments or
suggestions about
them to
problem@rice.edu
What is Pro-Matlab?
There are two ways to start Matlab on the Rice Unix Facility. (These methods
will also work for Information Systems
% 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
quit
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
a=3
v=[3 5 7]
Matrix definition follows the same form as vector definition. The matrix rows
are separated by semicolons, or by
returns.
ml=[1 2 3; 4 5 6]
or
m2=[1 2 3
4 5 6]
To define character variables we use the equal sign and single quotes.
c=’abc’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.
.S‘..Graphing
Matlab has a wide variety of built in higher math functions. We won't be able
to cover all of them here, but for further
information you can consult the on-line demo, and the tutorial and reference
sections of the Math-Works Pro-MATLAB manual.
Polynomials
roots(coeff)
Polynomials are also useful in data fitting. Given two vectors of experimental
points we can fit different orders of
vectors xdata and ydata in the workspace test1. Compare the ydata values
and the yexpected values for several orders
of polynomial.
3x1 +5x2=6
4x1 - 8x2=1
we would define
mat=[3 5; 4 -8]
vec=[6 1]
vec=vec’
mat \vec
and solve the equations.
Graphing
The basic graphics command in Matlab is plot. Plot will take multiple
arguments, always in x,y pairs. For instance:
plot(x,y,x,z,w,d)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 argument, 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.
automatically when you leave Matlab. If you kill the graphics window and
then try to plot again, Matlab will
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
Matlab window. Matlab will automatically select the default laser printer. On
some systems you may be charged for
this printing.
If you would rather save your plot to a file, the command of interest is meta.
Simply type meta in the Matlab window,
and your plot will be saved to a file called matlab.met. If this file already
exists, your plot will be appended to the end
of it. To print this file you will need to convert into a filetype that your printer
of choice can interpret. Matlab comes
packaged with a graphics post processor for just this purpose. Typing !gpp in
the matlab window will give a fairly self
Note: ! is the shell escape for matlab. Thus, if you would prefer, the gpp stuff
can be done in an xterminal.
M-files
Matlab commands exactly as you would type them interactively in the Matlab
window. The file may have any legal
unix name, and should end with a .m extension. These files may be placed in
your root directory, or a directory named
There are two types of m-files in matlab. One is called a script. This is simply
a list of Matlab commands with no
header. The other type is a function. Functions have a header line that may
look something like:
function y=fun1(x)
Functions may be passed arguments, and may return results. To invoke a
script or a function you simply type the filename (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 Function Functions
Control Flow
Matlab does have flow control statements. Although these may be used in an
interactive fashion, they are frequently
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
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
Finding Zeros
There are two routines for finding the zeros of functions. These are fmin, for 1
variable functions, and fmins, for multivariable functions. Fmin has the same
syntax as quad and quad8. Since fmins uses a simplex routine it needs only a
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:
(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.
day a year and can page appropriate administrators for major network or
computing problems.