Introduction to Matlab

Open matlab by going to the folder "ClusterApplications" and double-click on the matlab icon. (On a unix or linux machine, just type matlab.)

The matlab prompt looks like >>. You might start by going to the helpdesk:

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

If you type

>> diary MatlabIntro
then matlab will save your matlab session --- all the commands you type and most of the output in a text file. You can then open this text file in an editor like Word or Emacs and add text to put together a report using the results of your matlab session. When you have finished type
>> diary off
to end the text record of your matlab session. Try
>> help diary 
for more information.

A note about File Handling: You have to tell matlab where to keep your saved files and where to look for files that 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 button to look for your H: drive. Find it, click on it to select it. Go to the File menu and Save Path. Then exit the Browser. You will have to do this every time you open Matlab on a cluster machine.

Some sample Matlab commands

Try the following to see how to generate a simple plot in matlab.
>> x = linspace(0, 2*pi, 21)
>> y = sin(x)
>> plot(x,y)
You might not want to look at the list of points created by these commands. If instead you type
>> x = linspace(0,2*pi, 21);
ending your command with a semicolon, matlab does not print the computed points on your screen. For more information about these commands you could type
>> help linspace
>> help plot
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. So to define the matrix that appeared in our temperature problem:
>> 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 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.

Defining your own functions -- M-files

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. To do this you must put the matlab commands in an m-file. 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.