Quick introduction to MATLAB
- MATLAB is available in most computer labs, as well as the classrooms Sci2 306 and Sci2 308.
- To start MATLAB on the machines in Sci2 302:
- Click on START in the lower left corner of the screen
- Go to PROGRAMS > MATH-STATISTICS > MATLAB 7.0.4
- Click on MATLAB 7.0.4 to start the program
- To enter a matrix type the entries row by row in square brackets, with entries separated by blanks, and rows separated by semicolons.
- Example: Type A = [2 -3 -1; 2 4 0] to store this simple 2x3 matrix
under the name A.
- All rows must have the same number of entries.
- MATLAB is case sensitive; for example C and c are different matrices.
- MATLAB displays entries that are not integers with 5 digits precision.
- To display all entries as fractions type format rat.
- To change format back to decimal type format short
(for 5 digits precision) or format long (15 digits precision).
- There are a few standard matrices that are built into MATLAB already:
- zeros(m,n) is the mxn matrix with all entries being 0.
- ones(m,n) is the mxn matrix with all entries being 1.
- eye(n) is the nxn identity matrix.
- Example: B = 4*ones(2,3) stores the 2x3 matrix all of whose entries are 4 into B.
- Accessing a matrix:
- size(A) gives the size (# of rows, # of columns) of the matrix A.
- To access/display a matrix just type its name.
- To access/display the entry in row i and column j of matrix A type A(i,j)
- To access/display row i type A(i,:)
- To access/display column j type A(:,j)
- To change an entry/row/column of matrix A set the above access command equal to whatever you want to change the entries to:
- Example: A(:,2)= [1 3] changes the entries in the second column of A to 1 and 3.
- Matrix Algebra:
- To add matrices A and B type A+B
- To subtract matrices A and B type A-B
- To multiply matrix A by the scalar c type c*A
- To multiply matrices A and B type A*B
- The kth power of the matrix A is A^k
- The transpose of the matrix A is A'
- To find the reduced row echelon form of matrix A type rref(A)
- To find the inverse of the n x n matrix A you can:
- Proceed as on page 55, which means you type rref([A eye(size(A))])
or just rref([A eye(n)])
- Type inv(A)
- Type A^-1
- MATLAB uses different approaches to execute these procedures,
but if the matrix is invertible the results should be the same!
- To find the determinant of matrix A type det(A)
- Vectors in Rn are just n x 1 matrices, so that all operations
for vector arithmetic are the same as those for matrices shown above.
- To find the dot product of vectors u and v type dot(u,v)
or u'*v
- To find the norm of vector u type norm(u)
- To find the projection of vector u onto the space generated by vector v
type (dot(u,v)/norm(v)^2)*v
- If W is the column space of A, then the projection of a vector b onto W can be found by
- typing A (A'A)^-1 A' b if the columns of A are linearly independent
- in general, solving (A'A) x= A'b for x (that is, find a least squares solution to Ax=b) and then typing A x
- To find the characteristic polynomial of matrix A type poly(A)
- To find the eigenvalues of matrix A type roots(poly(A)) or
eig(A).
- To find the eigenvectors and eigenvalues of matrix A type
[V,D]=eig(A), where the columns of V are the eigenvectors, and the corresponding eigenvalues are on the diagonal of D.