HMC Homepage      CS Home

Using Matlab

Intro

Matlab is an interactive package for numerical analysis, matrix computation, control system design and linear system analysis and design. In addition to all that fancy stuff, it has a wide array of features that are useful for the core math classes like graphing, differentiating, and solving linear systems and DE's.


Getting Started

We have two different licenses. One for classroom use (this includes doing homework) and one for research. Information about the differences between the two can be found here.

You should use the classroom license when you are doing homework. The research license must be used for all non-classroom (and non-homework) use. Yes, this is a bit silly, but it's part of our contract with the makers of Matlab.

On the Macintosh computers in the labs, Matlab is installed in /Network/Applications/MATLAB. Go to this folder in the Finder and open "Start MATLAB". It will launch X11 for you and start up Matlab. Note that as of version 7.0.4, Matlab runs under the X Window System, rather than Mac OS X's native windowing environment. While the makers have Matlab have tried to make it look somewhat like a normal Mac OS X application, it's quite obvious that it is not. For example, it has menubars in each window and many things don't work the same way as in other programs.

If you want to ssh in from your dorm room and use matlab, you can use the copies installed on our Linux servers (knuth and wilkes). These are called "matlab-classroom" and "matlab-research" .. be sure to run the correct one. (If you forget and you try to run "matlab" you'll get a message reminding you of the names of the two versions.) If you have X11 on your computer and you have used SSH's option for tunnelling X11, then you will be able to see graphs and such. Otherwise, it will be text only.

In the Matlab window you will be presented with an introductory message and a ">>" prompt. Pretty decent online help can be obtained at this point by typing "helpwin" or "help" (helpwin brings up an interactive window while help just uses the command line).

You can also type "demo" to access demos on some common functions and the various Toolboxes we have installed. Demos of special interest to those at here at Mudd include most of the items in the Matrices submenu, as well as the 2-D and 3-D plotting examples in the Visualization submenu.


Defining Variables

In Matlab there are three basic types of variables: scalars, vectors, and matrices. Varables are definited by typing the name of the variable followed by an equal sign and the value of the variable. Vectors and matrices are denoted by enclosing the values in square brackets and rows within a matrix are separated by semicolons. For example:

A = 1                       let A = 1

B = [1 2 3]                 let B = the vector [1 2 3]
                                    
                                    1 2 3
c = [1 2 3; 4 5 6; 7 8 9]   let c = 4 5 6
                                    7 8 9
Keep in mind that variable names are case sensitive, so if it looks like something isn't getting assigned correctly check to make sure you got your cases right.

Frequently Used Commands

Graphing Functions

Graphing functions in 2 dimensions is accomplished using the plot command. To graph functions in 3 dimension use plot3 or one of its variants as enumerated below.

  • plot (X,Y)

    plot (X,Y) plots the (X,Y) pairs that you specify. Generally X will be a vector of numbers in a range which you specify and Y a vector of numbers obtained by applying a function to X. For example, suppose you want to graph y = sin (x) on x=-1..1. To do this, first define your vector by typing X=-1:0.01:1;. This tells maple to construct a vector containing values starting at -1 and ending at 1, incremented by 0.01. Next obtain you Y vector by typing Y=sin(X);. This builds a vector containg the sin of the corresponding entry in the X vector. Now you can plot the function by typing plot (x,y);.

  • plot3 (X,Y,Z), surf (X,Y,Z), mesh (X,Y,Z)

    The 3 functions named above all take two vectors X and Y and a matrix Z and graph in some form the values found in Z using X and Y as indices. plot3 plots a bunch of lines, surf yields a 3D surface and mesh gives a 3D mesh of lines.

    The trick with plotting is to get your Z matrix to correspond to some function of the values in the X and Y matrices, and the syntax for this isn't exactly the most obvious thing. For example, suppose I want to plot z=x*y on y = -1..1 and x = -1..1. The first thing you need to do is make a matrix with the correctly varying values of X and Y in each column. This is done using the following command: [X,Y] = meshgrid ([-1:0.1:1],[-1:0.1:1]), which creates a matrix with X and Y values such that the X values are constant as you move between rows and the Y values are constant as you move between columns.

    Once this is accomplished you need to build your Z matrix. For our function the command Z = X.*Y will compute the correct Z matrix. You should note that the * was proceeded by ., which instructs matlab to perform the operation pairwise on the relevant matrix entries (type help arith for more information on pairwise operators). Now all you have to do is type plot3 (X,Y,Z) or surf (X,Y,Z) or mesh (X,Y,Z) to get your final result.

Matrix Operations

The following matrix operations are available in Matlab:

Operator Operation Usage
+ addition A+B
* multiplication A*B
' transpose A'
/ right division b/A
Operator Operation Usage
- subtraction A-B
^ exponentiation A^2
\ left division A\b


You should be familiar with most of these operations if you have taken a course in linear algebra, but a couple of them (namely left and right division) bear some explanation. A\b returns the matrix x which is a solution to Ax = b. Similarily, b/A returns the matrix x which is a solution to xA = b. These two operators let you solve systems of equations easily assuming you know your A and b.

In addition to these operations Matlab also supports some simple functions on matrices. For example det(A) will compute the determinant of A while inv(A) will calculate its inverse. For an exhaustive list of all the available matrix function type help matfun.

Miscellaneous Commands

  • diary < filename > - Start session transcript

    The diary command starts logging the current Matlab session using file filename. Typing "diary off" suspends logging and "diary on" resumes logging after it has been turned off.

  • save < filename > [variables] - Save current values of all variables to disk

    The save command saves all workspace variables in filename.mat. The variables may then be retrieved later by typing "load filename" You can also save only the variables you specify by typing "save filename [your variables here], i.e. "save foo X Y Z" will save only variables X, Y, and Z in a file names foo.mat.

  • clear [variables] - Clear variables from the workspace.

    clear by itself nukes all the variables in the workspace, in effect giving you a clean slate. clear [your variables here] resets only the variables you select, i.e. "clear X" resets variable X but leaves the rest of the workspace alone.

  • ! shell command - execute a shell command

    ! executes a shell command from within matlab. For example, "! ls" will give you a directory listing from the current directory. To enter a bonafide shell from within Matlab type "! < shell of your choice >".


Matlab Toolkits Available

In addition to the standard Matlab libraries we have available several special purpose toolkits.

  • SIMULINK

    SIMULINK is a library for system construction and anaylsis. The operation of this package is outside the scope of this document. For information on using simulink type help simulink, or to see demos type help simdemos.

  • System Identificawtion Toolbox

    The System Identification Toolbox is an analysis package that contains tools for byuilding mathematical models of dynamical systems, based upon obvserved input-output data. This toolbox allows for both parametric and non-parametric models. For help on this package type help ident.

  • Neural Network Toolbox

    This toolkit allows for the construction and training of neural networks. This is another complex toolkit whose operation is outside the scope of this document. Type help nnet for information on using the neural network toolbox and help nndemos for neural network demos.

  • Optimization Toolbox

    This package provides commands for nonlinear minimization of function and matrices. For information on the commands provided by this package type help optim.

  • Signal Processing Toolbox

    This package provides lots and lots of functions for signal processing. This includes waveform generation, filter analysis and implementation, linear system tranformations, and a whole bunch of other nice tools. For information on this toolbox type help signal.


Copyright (c) HMC Computer Science Department. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.1 or any later version published by the Free Software Foundation; with the no Invariant Sections, with no Front-Cover Texts, and with no Back-Cover Texts. A copy of the license is included in the section entitled ``GNU Free Documentation License.''

HMC Computer Science Department
Contact Information
Last Modified Wednesday, 17-May-2006 19:18:43 PDT