Materials for Matlab Tutorial

Remember to direct Matlab to where you've saved your m-files. Otherwise
you won't be able to run the scripts.

If you forget how a function works, you can use the help or doc commands
to remind you. For instance if you don't remember all the options for the
plot function, just type help plot into the Matlab command window and
press the enter key. One more helpful command to know is ctrl-c. That is,
hold the control key and press c. This will abort whatever program Matlab is
trying to run. So if you write an infinite loop into a program and want to stop
it, use ctrl-c.

There are many online tutorials for Matlab, and Mathworks has extensive
documentation available online. This page has many sample files; to
download all of them at once, click here.


m-files for examples of simple plotting methods: Simple Plots and 3-D Plots.

m-file for generating and plotting sample paths for Brownian Motion.

To solve an ODE like y' = f(t,y), Matlab has several built-in ODE solvers.
Here are some files that use the solver ode45, which requires a separate file
that defines the vector field f(t,y), so the following examples come in pairs.
(1-D ODE example , 1-D vector field)
(2-D ODE example , 2-D vector field)
(Forced Pendulum , vector field for forced pendulum)

It is often useful to have plots of the direction field for an ODE or the phase
plane portrait. The Math Department computers have the programs dfield
and pplane installed for building these types of visualizations.


It is useful to be able to save a workspace or just some of the variables in
the current workspace. See this website for details on how to use save.

A typical example to save as an ASCII text file would be
             save -ascii -double filename.txt variable_1 variable_2
If the variables are not listed, then all variables are saved. If -ascii is not
indicated then the data will be saved as a Matlab binary file (.mat). The
option -double saves the data in 16-digit format.

The save command will overwrite an existing file if you give it an existing
filename. If you want to append an existing file use
             save -append -ascii ...

Remote Access

You can access the department computers using a terminal. (Built-in on
Linux, and Mac. Use putty if you run Windows.) You just need to pick
a host name, so for example to connect to lab3, you could type
             ssh       or possibly
You can check the lab computer status here. You can also ssh onto a
department computer using this.

The downside to this is that you cannot use the visualization tools when
accessing the computers via terminal. M. Zou made a webpage explaining
how to use VNC for Windows, which will allow you to use graphics tools
(like plotting in Matlab). Information for Mac users can be found here.

There is a convenient command called screen that will allow you to run
a program and then "detach" from it, so that you can do other things while
it runs. For instance, if you have a program that takes an hour to run, you
can use screen to run the program, you can log-out and come back an hour
later to see the results. An example would look like
             screen -RR     <enter>
matlab -nodisplay    <enter>
run_program    <enter>
Ctrl-a d
The first line creates a screen where you can run a program (the screen will
look just like a regular terminal window). The second line opens Matlab in
the screen (-nodisplay is optional). The third line executes the program named
"run_program". While "run_program" is running, you can detach from the
screen with the command on the fourth line "Ctrl-a d" (that is press and hold
the control key and press a, then release control and press d). This will take
you to your original terminal screen and you can run other programs or you can
log-out. To return to the screen, simply type
             screen -RR     <enter>
and you will see Matlab again, running (or maybe finished with) "run_program".