% matrix_alg_demo.m by Andy Ruina 10/2/96 % This is a demo of some of MATLABS abilities with % matrices: Multiplying, solving equations, creating % identity, finding inverse, finding eigen-values % and vectors, etc. % What is here should mostly get you by in 293 for % matrix skills. Understanding what these commands do % (not necessarily how they do it) is most of what you need to know % about matrices. % If you execute this file it will create a bunch of output in the % command window. Look at it to help you understand the commands. % See Pratap pgs 41-58 and 64-67. % Try online help with any of the command names you % see below (eg. type >help eye), % or >help punct for help with , ; ' etc. % A = [ 1 2; % CREATING A MATRIX pi 4 ] B = [4 5; 6 7] % CREATE ANOTHER MATRIX C = A' % TRANSPOSE A MATRIX b = [ 1 3 ]' % CREATE A COLUMN VECTOR C = A * B % MULTIPLY ONE MATRIX BY ANOTHER % (by the rules of matrix multiplication, % ie, c(1,2) = a(1,1)*b(1,2) + a(1,2)*b(2,2) y = A * b % MULTIPLY A MATRIX AND A COLUMN VECTOR D = A .* B % MULTIPLY CORRESPONDING COMPONENTS % (ie, d(1,2) = a(1,2) * b(1,2) ) comp = D(2,1) % GET A COMPONENT OF A MATRIX col = D(:,2) % GET A COLUMN OF A MATRIX (2nd col in this case) row = D(1,:) % GET A ROW (1st row in this case) gee = diag(D) % GET DIAGONAL, make a column vector out of % the diagonal elements of a square matrix whiz = diag(gee) % MAKE DIAGONAL, make a diagonal matrix (all % zeros off the diagonal) out of a column vector identity = eye(4) % MAKE AN IDENTITY MATRIX % (one on the diagonals, zero elsewhere). %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Here are some really powerfull cool commands... % ... the commands that put the MAT in MATLAB. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % (By the way, all of these work fine even if compex numbers are involved.) Adet = det(A) % DETERMINANT calculation, less useful than some claim Ainv = inv (A) % Finds the INVERSE of A (if it exists) Ainv = A^(-1) % Also finds the INVERSE of A. A_times_inverse = A*Ainv % CHECK. Should give identity matrix. x = Ainv*b % SOLVES A*x=b for a given A and b (find Ainv first) x = A\b % SOLVES A*x=b for a given A and b more efficiently % It the columns of A are not 'independent' the % command tells you that your matrix is 'singular' % and doesn't give you the solution (even if there % is one). A*x - b % CHECKs the answer above. Since A*x is b this % gives zero. [VECT, VAL] = eig(A) % EIGENVECTORS and EIGENVALUES. VECT is a matrix % where each column is an eigenvector of A. VAL % is a diagonal matrix where each element is the % corresponding eigenvalue. % (An 'eigenvector' of a matrix A is a vector v so that % A*v is proportional to v. The proportionality % constant is the 'eigenvalue' for that eigenvector.) values = eig(A) % EIGENVALUES of a matrix listed in a column vector % So, for example, VAL (above) is the same as diag(eig(A)). A * VECT(:,1) - VAL(1,1)*VECT(:,1) % This checks the first eigenvector and value. % If you have followed up to here you should be able % to follow why this command gives two zeros (or very % nearly so --- note the 1e-15). A*VECT - VECT*VAL % Check all eigenvectors and values at once. (Should % get very close to 4 zeros for a 2x2 matrix.) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % If this all makes sense to you than you are doing great. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%