Department of Mathematics

 

Math 312 - Applied Mathematics: Social Sciences
Spring 2006


Using MATLAB

MATLAB is available in the Math Department Computer Lab, in 201E McGregory. To run it, log into a computer and double-click on the MATLAB icon.

MATLAB is a command-line oriented program, meaning you type in commands, and MATLAB prints the results. Actually, MATLAB provides a complete programming language, but that is more than we need here.

The command prompt is ">>", which you will see in the main MATLAB window. This is where you type the commands. In the following examples, the commands that you type are shown in blue.

>> format compact
>> 2+2
ans =
     4
>> sin(12.34)
ans =
     -0.2244
>> x = 4.91
x =
     4.9100
>> exp(x)
ans =
     135.6394
>>

The first command, format compact, is not necessary, but it cuts down on the number of blank lines that MATLAB prints. The other commands show that MATLAB can be used as a simple calculator.

In the second-to-last command, a variable x is assigned a value, and then ex is computed in the last command. MATLAB uses exp for this function.

Here is an example of how MATLAB can work with matrices. I'll enter a 3x3 matrix A, compute A2, and then find the eigenvalues of A. The matrix is
A =
130
001
131
In the following, note how the matrix is entered in MATLAB, using square brackets to delimit the matrix, and semi-colons to separate the rows.

>> A = [1 3 0; 0 0 1; 1 3 1]
A =
     1     3     0
     0     0     1
     1     3     1
>> A^2
ans =
     1     3     3
     1     3     1
     2     6     4
>> eig(A)
ans =
    2.7321
    0.0000
   -0.7321
>>

The function eig(A) computes the eigenvalues of A. (In this example, the exact values happen to be 0, 1+sqrt(3), and 1-sqrt(3).)

The eig function can also be used find the eigenvectors of A, but the syntax is not obvious:

>> [V,D] = eig(A)
V =
    0.5116    0.9487    0.8133
    0.2953   -0.3162   -0.4695
    0.8069   -0.0000    0.3437
D =
    2.7321         0         0
         0    0.0000         0
         0         0   -0.7321
>>

When the eig function is used this way, the columns of V will be assigned the eigenvectors of A, and the diagonal elements of D will hold the eigenvalues.

Here is an example of accessing elements within a matrix. This will show the third eigenvalue and corresponding eigenvector:

>> D(3,3)
ans =
   -0.7321
>> V(1:3,3)
ans =
    0.8133
   -0.4695
    0.3437

In the last command, a range of rows was specified by using a colon ":". The expression V(1:3,3) returns the submatrix containing rows 1 through 3 in column 3 of V.

The help and doc commands can be used to access MATLAB's help files. The first prints the information in the command window, and the second opens up a new window containing the documentation. For example, doc eig will open up a new window containing documentation about the eig function.

MATLAB uses the symbol i to represent the square root of -1, so complex numbers look like you expect them to. For example,

>> sqrt(-1)
ans =
        0 + 1.0000i
>> B = [3 -2; 1 1]
B =
     3    -2
     1     1
>> eig(B)
ans =
   2.0000 + 1.0000i
   2.0000 - 1.0000i
In the above example, I told MATLAB to compute the square root of -1, and then found the eigenvalues of a matrix, which turned out to be 2 ± i.

You can find the magnitude of a complex number in MATLAB with the abs function. abs(z) gives the magnitude of the number z. If z is real, this is just the absolute value.

Often you will want to make a record of your MATLAB session. The diary command allows to save the input and output in a file. For example,

>> diary example.txt
>> A = [3 2 -1; 0 0 1; 7 7 0]
A =
     3     2    -1
     0     0     1
     7     7     0
>> [V,D] = eig(A)
V =
   0.0752 - 0.2718i   0.0752 + 0.2718i   0.4268          
  -0.3449 + 0.1525i  -0.3449 - 0.1525i  -0.5570          
  -0.8822            -0.8822             0.7124          
D =
   2.1395 + 0.9463i        0                  0          
        0             2.1395 - 0.9463i        0          
        0                  0            -1.2790          
>> abs(D(1,1))
ans =
    2.3394
>> diary off
>>

This creates a file called "example.txt" in your home directory.

When you use MATLAB in your homework or projects, you can use this feature to create a record of your calculations that you can hand in. However, please do not simply start a diary at the beginning of your MATLAB session and hand in every single bit of scratch work with all the mistakes and false starts. Experiment without the diary first, and figure what you want to do and how you will do it. When you have figured out a concise set of commands that will do what you want, turn on the diary and then enter your commands.

When you are done using MATLAB, you can enter the command exit. Be sure you log out of the computer before leaving the lab.


There are many guides and tutorials for MATLAB available on the web. For example,