Matlab Examples

  1. here Very quick intro [U. Utah]
  2. here Quick Reference [U.S. Naval Academy]
  3. here Slightly more detailed intro [U. Florida]
  4. here Very systematic tutorial [MathWorks]
  5. here Control Toolbox (specialized) tutorial [U. Mich]
Also, you can go to the Math 696 syllabus and explore the Matlab links, as well as Prof. Peter Howard's Intro

Creating an array of values and plotting it

t = 0:0.01:5;           % 500 values, spaced 0.01 apart, on [0,5]
y = sin(4*t);           % function to be plotted
plot(t,y);              % 2D plot command (t,y are both vectors)!
print -deps plot1.eps;  % print current graphic to postscript file

Note the use of the "%" as a comment delimiter!

Printing to a File

The following code creates a surface graphic (using a builtin function called peaks) then prints it to a color encapsulated postscript file called "peaks.eps"

print -depsc peaks.eps

If you want to change this to a gif format (for inclusion in a web page) use the unix command "ps2gif"

If you are running under windows, you can output to jpeg or png format. (Type "help print" at the Matlab prompt).

Using Matlab's numerical ode solver

The following code creates "inline" functions g1 and g2, returning 2x1 column vectors. Matlab's builtin runge-kutta solver returns a 2x1 vector. These are plotted and compared.

g1 = inline('[x(2);-sin(x(1))]','t','x');
tseq = linspace(0,20,101);
[t,x] = ode45(g1,tseq,[0;1]);
g2 = inline('[x(2);-x(1)]','t','x');
[t,y] = ode45(g2,tseq,[0;1]);
axis equal;
% print -depsc graphic
graphic = ''; 
print( gcf, '-dps', graphic )

"inline" vs "script" functions

To create an inline function for use within a Matlab session, we do the following:

f = inline('sin(x)','x');

If we want the same file available through an m-file (or script) we create a function called y.m (in the same directory) with the lines

function y = f(x)
y = sin(x)

Note the file must be the same name as the function name, with an ".m" extension.