Introduction to MATLAB

Getting Started:  Matlab is available on the cluster machines, or if you prefer, you may purchase the student version of matlab for about $100 at the bookstore.  To open matlab on a cluster machine, go to
the folder "Cluster Applications" and double-click on the matlab icon.  On a unix or linux machine, just
type matlab.

The matlab prompt looks like  ">>" and you might start by going to the helpdesk:

>> helpdesk
Look for the link "Getting Started" for a gentle tutorial.

Saving Your Work:   Before you can really get started you need to take care of some technical details like how to save your work.  There are several possibilities, but first you must tell matlab where to keep your saved files and where to look for files you want to open.  Type

>> pwd
>> dir

to see the directory or folder matlab is currently using and the files currently available to matlab. If you are working on a cluster PC, you need to save all your personal files in the H: drive.  To tell matlab about this, go into the  File menu and select the Set Path option.  The current directory will be displayed and you should click on the Browse buttion to look for your H: drive.  Find it, click on it to select it and then exit the Browser.  You will need to do this every time you want to save your matlab session or open a file that you created earlier.

Perhaps the simplest way to save the results of your work with matlab is to cut and paste between your matlab window and your favorite text editor.  This works quite well.  Another possiblility is the "diary" command.   For example, if you type

>> diary hw1
then matlab will save all the commands you type and most of the output in a text file named "hw1".
When you are finished, just type
>> diary off
to end the text record of your matlab session.  You can then open this text file in an editor like Word or Emacs and edit it to put together a report to turn in.  For more information try
>> help diary

Some sample MATLAB commands

The basic object in matlab is a matrix, a rectangular array of numbers.  These are used in sometimes surprising ways in matlab, but the best way to get started is just to try a few simple examples to learn
the basics.

To plot a function of one variable:  Suppose for example you wish to plot a function.  For example we plot f(x) = sin(x) on the interval [0,10].  We make a list of x-values distributed across this interval, then we compute the corresponding y-values and finally make the plot:
 

>> x = [ 0 : 1 : 10 ]
>> y = sin(x)
>> plot(x,y)
The result is a rather crude plot of the sine function.  The first command uses the colon operator to make the vector x.  Essentially, we start at 0 and go up in steps of 1 until we reach 10.  Then we compute the corresponding y values and finally, we plot them.    We can get a better plot by using more points.  For example
>> x = [ 0 : 0.1: 10 ];
>> y = sin(x);
>> plot(x,y)
Notice here we use a semicolon at the end of the command to tell matlab not to print the computed points on the screen.

Defining Matrices:  Defining matrices is pretty simple. You can separate the entries in a row by space(s) or by commas or by a mixture of these. Matlab is not picky. ROWS must be separated by semicolons.   For example, try

>> M = [ 4  -1  -1   0  100;
        -1   4   0  -1  200;
        -1   0   4  -1    0;
         0  -1  -1   4  100]
Matlab will put it into reduced row echelon form for us:
>> R = rref(M)
We can pick out rows or columns or individual entries or submatrices:
>> R(1,5)
>> R(1,:)
>> R(:,1)
>> R(1:2, 3:4)
To take the transpose of a matrix --- convert the rows to columns --- you use ' (single quote)
>> M'
So we can make a matrix whose rows and columns represent the interior temperatures A,B,C D from the example we did earlier:
>> T = [R(1:2,5)'; R(3:4,5)']
We can also plot the matrix. What does the picture you get from the following command represent?
>> imagesc(T)
The diary command just keeps a text record of your session. You cannot reload it into matlab. So if you are working on a big problem you might want to save your matlab session so it can be reopened. To do this type >> save temperatures (Remember -- you must set the path as described above to make sure your files are saved in the H: drive.) Then all your matlab definitions will be saved in a file called "temperatures". You can then quit from matlab and load this file back in to continue (again, after you set the path to tell matlab where to look). Just type
>> load temperatures
and then type
>> whos
>> M
to remind yourself of the definitions you made when you were working before. Again, type
>> help save
to get more information.

Another way to solve the system we got in the temperature problem is to use the matlab operator "\". To illustrate:

>> A = M(1:4,1:4)
>> b = M(1:4,5)
>> t = A\b
(we will talk more about this operator later)

Now let's look at another example. Consider the system

        x1 + x2 = 5
        x1 - x2 = 3
       2x1 + x2 = 1
In matrix form we have Ax=b where:
>> A = [1 1; 1 -1; 2 1]
>> b = [5;3;1]
A and b were numbers instead of matrices we would solve the equation Ax = b by dividing by A (unless A = 0). One way to solve
 
>> x = A\b
OK, now we solve a different way. Form the augmented matrix for the system, which looks like [A|b]. We can't put in the vertical separator, but we can put A and b together:
>> C = [A b]
and row reduce it:
>> rref(C)
Notice that our two methods gave different results. Based on rref(C) we see that the original system was INCONSISTENT since the last row of the reduced matrix is [ 0 0 1 ]. So what does the answer we got from the other method mean? We will talk about this more later --- the answer we got using "\" is not really a solution to the system. Matlab has solved the least squares problem instead. We will discuss this in detail later in the course --- for now the moral of the story is watch out using "\" to solve an inconsistent system because Matlab will try to construct an approximate solution if no actual solution exists.

You can define your own functions in matlab, save them in special files and load them in when you need them. For example, suppose you want to row reduce a matrix. You could use rref, but then you cannot see any of the intermediate steps. You can write commands to do row operations on the matrix M:

>> B = M
>> B(2,:) = 0.25*B(1,:) + B(2,:)
>> B(3,:) = 0.25*B(1,:) + B(3,:)
>> B(1,:) = 0.25*B(1,:)
>> B(2,:) = B(2,:)/3.75
>> B(1,:) = 0.25*B(2,:) + B(1,:)
and so on...

If this is a process you will do often, it might make sense to set up your own commands that do row operations on a matrix. You can see how to do this on pages 212-213 of the ATLAST text. To create such a file (assuming the path is set properly) go into the file menu and select New, then M-file. Type in the commands for your new function, say rowscale:

   function B = rowscale(A,i,c)
   [m,n] = size(A);
   if i < 1 | i > m
      error('Index out of range')
   end
   B = A;
   B(i,:) = c*A(i,:);
Save it as rowscale.m and exit the text editor. Now if you type
>> R =  rowscale(M, 1, 0.25)
Matlab will multiply row 1 of M by the scalar 0.25 and save it as R. On pages 212-213 of the ATLAST text, you will find definitions for the other row operations (rowcomb and rowswap). As an exercise, type these in and use them to row reduce a simple matrix.