Department of Mathematics




Math 312 - Applied Mathematics: Social Sciences
Spring 2005


Some Notes on Using MATLAB

Some of the final projects will require working with fairly large matrices. These brief notes show how you can use MATLAB for this.

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 blanks 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.

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