This worksheet introduces linear algebra in Matlab. It requires no programming experience, but some familiarity with Matlab is recomended.
Matlab stands for "MATrix LABoratory" and the reason for this is that it is built around the use of matrices. This makes Matlab a great tool for any computations involving linear algebra. Since linear algebra is an essential tool in studying ordinary differential equations and in numerical solutions of partial differential equations, Matlab can be used for a great variety of scientific problems.
The worksheet first presents table where the fundamental linear algebraic operations are given, followed by a few exercises focusing on the use of linear algebra in studying ordinary differential equations.
| Operation: | Matlab command: |
| Dot product between vectors u and v | dot(u,v) |
| Transpose AND complex conjugation | ' |
| Transpose WITHOUT complex conjugation | .' |
| Get ij:th element in matrix A | A(i,j) |
| Get i:th row in matrix A | A(i,:) |
| Get j:th column in matrix A | A(:,j) |
| Determinant of matrix A | det(A) |
| Eigenvalues and eigenvectors of matrix A (see example below) | eig(A) |
| The LU-decomposition of a matrix A | lu(A) |
| The QR-decomposition of a matrix A | qr(A) |
| The SVD-decomposition of a matrix A | svd(A) |
| The 2-norm of a matrix A | norm(A) |
| The 1-norm of a matrix A | norm(A,1) |
| The infinity norm of a matrix A | norm(A,inf) |
| The Frobenius norm of a matrix A | norm(A,'fro') |
The command eig(A) returns only the eigenvalues or both eigenvalues and a set of eigenvectors depending on how you use the command. This is illustrated in the example below.
Example 1.
|
A = [ 1 2 ; 2 1 ] e = eig(A) [X D] = eig(A); X D B = [1 i ; 2 3] B' B.' |
The example above illustrates two ways of using the command eig. The first time it is used, it simply returns all eigenvalues in a vector. The second time eig is used, it returns a set of eigenvectors as columns in the matrix X and the eigenvalues as diagonal elements in the matrix D.
|
Exercise 1. Verify that the columns of X in the example above indeed are eigenvectors to the matrix A. In other words, multiply (using Matlab) the matrix A to the columns (one at a time) of X and check that they are eigenvectors corresponding to the eigenvalues. |
Matlab has a very powerful method for solving system of equations. The syntax is simple. The following example solves the systems Ax=y using a very reliable algoritm.
Example 2.
|
x = A\y |
Example 3.
|
A=[ 1 2 ; 3 1 ] [L U]=lu(A) [Q R]=qr(A) B= [ 1 2 ; 3 4 ; 5 6] [U S V]=svd(B) |
|
Exercise 2. Define the matrices A=[ 17 2 3 4 ; 5 6 7 8 ; 9 10 11 12 ; 13 14 15 16 ]Note the transpose ' on the y-vector which makes y a column vector. a) Compute AB and BA. Is matrix multiplication commutative? b) Compute AC. Why do you get an error message? c) Solve the following system of equations: 17x1+2x2+3x3+4x4 = 4Once you have found the solution x verify that it indeed solves the system. (This is easy once you have formulated the system as a matrix equation.) |
|
Exercise 3. Find the eigenvalues and a set of eigenvectors to the matrix A defined above. |
|
Exercise 4. a) Solve x''+x'+x=0 by hand (using the characteristic polynomial). b) Next re-write this ODE as a system of first order equations. Find the eigenvalues of the assoicated matrix. c) What is the relationship between the roots of the characteristic polynomial you found in a) and the eigenvalues? (This relationship is not a coincidence! It will be true for all linear ODEs.) |
|
Exercise 5. Using the observation in Exercise 4c) above, solve x'''+6x''+11x'+6x = 0 by writing it as a system of equations and then finding the eigenvalues (using Matlab). |
|
Exercise 7. Find the singular values of the matrix A=[ 1 3 ; 2 5 ; 6 7 ]. Check that the matrices U and V that Matlab returns according to Example 3 indeed are unitary matrices. |