MATLAB Handout
We will be making intense use of the mathematical program MATLAB. This is a powerful program heavily used in the "real world" for linear algebra calculations (among other things). MATLAB is a command line program, which is implemented at CC in the Windows environment. Although it has some nice built-in graphics capabilities and a built-in programming language, we will for the most part use only its basic linear algebra computation commands. If you are interested, you can examine some of the programming in action in some code that occurs in both of our textbooks. (Actually, I hope that at least some of you will know more about MATLAB than I do, by the end of the two blocks of this course!)
One important observation to make is that MATLAB does its computations in floating point approximate arithmetic, and does not (for the most part) do the symbolic, exact computations of a program like DERIVE. We will occasionally encounter consequences of this fact, which is good, since in the real world problems with round-off error is a really important consideration in applied linear algebra.
This handout provides the rudiments of MATLAB. This program is available on the network; a convenient place for us is the machines in the MATHLAB, in room 14 in Palmer Hall (in the sub-basement!). To access the program, merely look at Science Software, and then Math & Statistics Software, under the Start button.
To enter a matrix into the program, we could type
A=[1 2 3;2 1 5;3 4 5]
This would assign a 3 by 3 matrix to the variable name A, and would display it on the screen. Note that the elements in the rows are separated by spaces, and semicolons separate the rows.
If we wish to transpose A, we would type A’; typing B=A’ would assign the transpose the variable name B. If we wish to invert A, we would type inv(A). Note that MATLAB will give us an error message if the matrix is not square, and will tell you if a matrix is singular (does not have an inverse). Just typing inv(A) will assign the value inv(A) to the generic MATLAB variable ans; give a quantity a name if you are likely to want it later!
We can also do our calculus III computations in MATLAB. Thus, if v and w are vectors, dot(v,w) gives the dot product of the two vectors; this works for any two vectors of the same size. Note that we could have arrived at the same result with the computation v*w’ (why?). If v and w are 3-vectors, then cross(v,w) will give the cross product. The length of a vector v is given by norm(v).
We can build big matrices in more complicated ways, using blocks. For example, if we type A=[1 2;3 4], B=[A A] and C=[A;A], we have obtained a 2 by 4 matrix B and a 4 by 2 matrix C.
Contrariwise, we can pick out pieces of large matrices too. For a given matrix A, the command A(1,3) will return the entry in A in the first row and third column, while A(:,1) will give its first column, and A(2,:) will give its second row. The command A(1:3,2:4) would give the 3 by 3 matrix whose entries come from the first three rows and the second, third and fourth columns of A. And we can use these commands to change parts of previously defined matrices; thus, if we type A(1,3)=7, this will change A only in its 1,3 place.
Note that we can use multi-symbol variable names; also, MATLAB is case-sensitive. If you are confused about which variables you have assigned values to, you can type the command whos, which also tells us the variable type.
To multiply matrices A and B, you type A*B; to add or subtract, type A+B or A-B. MATLAB will complain if the sizes of these matrices are not suitable. If you wish to perform multiplication term-by-term, you can type A .*B instead.
We can enter many matrices of particular types quickly using such commands as eye(n) (giving an n by n identity matrix), zeros(n,m) (giving an n by m all-zero matrix, rand(n) (an n by n matrix whose entries are random numbers chosen between 0 and 1), etc.
Note that in addition to the standard MATLAB commands, we have a good number of macro commands built by the authors of the ATLAST book. (The code for these commands is at the end of that book.) (These commands are in the Marlow subdirectory; this subdirectory should be in the Path used by MATLAB).
In particular, we can perform the Gaussian elimination steps rowswap, rowcomb, and rowscale. For example, rowswap(A,1,3) exchanges rows 1 and 3 of matrix A; rowcomb(A,1,3,.4) adds .4 times the first row of 1 to the third row of matrix A. The command rref(A) (which is a MATLAB command) gives the reduced row echelon form of the matrix A; that is, this is the result of carrying out Gauss-Jordan elimination on A.
The command lu(a) gives two values; namely, the two matrices L and U, in the PA=LU factorization. You should type [l,u]=lu(a) to give the names l and u (or other variable names of your choice) to these pieces. When you play with this command, you will discover that the L given is often not lower triangular! However, there exists a permutation matrix P which will make it lower triangular, as you can easily see. Note furthermore that MATLAB will often introduce a permutation P when we (working by hand) would not. This is because MATLAB picks its pivots more carefully than we do, in order to minimize round-off error.
We also have in the Marlow directory some commands specific to the Strang book. The most important of these is slu(a), which does the LU factorization without doing any row exchanges.
The command det(A) returns the determinant of a (necessarily square) matrix.
To solve a matrix equation of the form Ax=b, you can type A\b. MATLAB actually carries this out by using the LU factorization.
Here are a couple of websites about MATLAB you might find of interest:
MATLAB FAQs:
http://www.isr.umd.edu/~austin/ence202.d/matlab-faq.htmlLinear Algebra at MIT (where Strang teaches):
http://web.mit.edu/18.06/wwwReturn to The Linear Algebra Course Page
Return to Marlow Anderson's Course Page