Sie sind auf Seite 1von 9

Clif Pottberg Arron Phillips MATLAB Project 4

function [m] = mult(A,ev1,ev2) lambda=eig(A); [i,j] = size(lambda); m1 = 0; m2 = 0; row = 1; tol = 10^(-7); while row<=i difference1 = lambda(row,1) - ev1; difference2 = lambda(row,1) - ev2; if abs(difference1) < tol m1 = m1+1; elseif abs(difference2) < tol m2 = m2+1; end row = row+1; end disp('multiplicity of eigenvalue 1:') disp(m1) disp('multiplicity of eigenmulvalue 2:') disp(m2) end This function utilizes the built in matlab function eig(A) to output the eigenvalues of A as a column vector, and then compares input eigenvalues with the eigenvalues of the input matrix to test their respective multiplicities.

function [d] = disteig(A) lambda=eig(A); [i,j] = size(lambda); L=logical(ones(i)); rowcurrent = 1; tol = 10^(-7); while rowcurrent<=i ev=lambda(rowcurrent,1); for rowcompare=rowcurrent+1:i if abs(ev-lambda(rowcompare,1))<tol L(rowcompare)=0; end end rowcurrent = rowcurrent+1; end disp('distinct eigenvalues of input matrix:') d=lambda(L(:,1)); end This function finds the distinct eigenvalues of A and outputs them in a column vector. It accomplishes this by utilizing the skeletal structure of the last program in conjunction with a logical array of ones. The while loop with contained nested for loop compares the current eigenvalue with other eigenvalues, but not itself. If they are equal to another eigenvalue within the specified tolerance, their corresponding entry in the logical array is set equal to zero. Thus the distinct eigenvalues are pulled from the output column vector corresponding to the logical array entries which are still one.

function [P] = eigbasis(A) lambda = eig(A); [i,j] = size(lambda); P = zeros(i,i); counter1=1; counter2=0; tol=10^(-7); while counter1<=i ev = lambda(counter1,1); B = newnullbase(A-eye(i,i)*ev); for counter2=0:i-1 if abs(B(:,1)-P(:,i))<tol; error('The input matrix is not diagonalizable. Eigenvectors are not linearly independent.') elseif abs(B(:,1)-P(:,i))>tol; end end P(:,counter1) = B(:,1); counter1=counter1+1; counter2=1; end if det(P)==0 error('The input matrix is not diagnalizable. P not invertible.') end end This program outputs a matrix whose columns are the eigenspace of the input matrix. A relatively similar structure to the other two, except for this program a simple matrix of zeros has been utilized for direct replacement of entries rather than representation by a logical array. In the while loop with nested for loop, the newnullbase program is utilized as a shortcut method to solving the homogeneous equation [A-Lambda*I]*X=0 and then looping through all eigenvalues given for the input matrix by eig(A) to find their corresponding eigenvectors. The nested for loop within the while loop simply compares the current eigenvector with the previously inputted eigenvectors and assures that they are not equal, with respect to the specified tolerance. This step assures linear independence of the columns of the eigenvector matrix P. If the eigenvector is linearly independent, it replaces the current corresponding row in the zeros matrix with the eigenvector. Once the while loop is complete, the program performs one more check to make sure that the matrix P is an invertible matrix.

>> A=[2 2 3; 0 3 -1; 0 2 0]


A= 2 0 0 2 3 2 3 -1 0

>> mult(A,0,2) multiplicity of eigenvalue 1: 0 multiplicity of eigenmulvalue 2: 2 >> disteig(A) distinct eigenvalues of input matrix: ans = 2 1 >> eigbasis(A) ??? Error using ==> eigbasis at 38 The input matrix is not diagnalizable. P not invertible.

>> B=[3 2; -2 3]
B= 3 -2 2 3

>> mult(B,0,2) multiplicity of eigenvalue 1: 0 multiplicity of eigenmulvalue 2: 0 >> disteig(B) distinct eigenvalues of input matrix: ans = 3.0000 + 2.0000i 3.0000 - 2.0000i >> eigbasis(B) ans = 0 - 1.0000i 1.0 0 + 1.0000i 1.0000

>> C = magic(6)
C= 35 1 3 32 31 9 8 28 30 5 4 36 6 26 19 24 7 21 23 25 2 22 27 20 33 17 10 15 34 12 14 16 29 13 18 11

>> mult(C,0,2) multiplicity of eigenvalue 1: 1 multiplicity of eigenmulvalue 2: 0 >> disteig(C) distinct eigenvalues of input matrix: ans = 111.0000 27.0000 -27.0000 9.7980 -0.0000 -9.7980 >> eigbasis(C) ans = 1.0000 -1.0000 -1.0000 8.8298 2.0000 -0.6747 1.0000 2.0000 -1.0000 24.0853 2.0000 -0.6985 1.0000 -1.0000 -1.0000 -15.2482 -1.0000 0.1223 1.0000 1.0000 1.0000 25.0780 -2.0000 0.2031 1.0000 -2.0000 1.0000 -43.7449 -2.0000 0.0478 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000

>> D = reshape(1:25,5,5)
D= 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

>> mult(D,0,2) multiplicity of eigenvalue 1: 3 multiplicity of eigenmulvalue 2: 0 >> disteig(D) distinct eigenvalues of input matrix: ans = 68.6421 -3.6421 0.0000 >> eigbasis(D) ??? Error using ==> eigbasis at 38 The input matrix is not diagonalizable. Eigenvectors are not linearly independent.

>> E = rand(4,4)
E= 0.2769 0.0462 0.0971 0.8235 0.6948 0.3171 0.9502 0.0344 0.4387 0.3816 0.7655 0.7952 0.1869 0.4898 0.4456 0.6463

>> mult(E,0,2) multiplicity of eigenvalue 1: 0 multiplicity of eigenmulvalue 2: 0 >> disteig(E) distinct eigenvalues of input matrix: ans = 1.8891 -0.0021 + 0.4886i -0.0021 - 0.4886i 0.1210 >> eigbasis(E) ans = Columns 1 through 3 0.6026 0.5513 0.9150 1.0000 Column 4 0.9830 0.5864 -1.7039 1.0000 -0.2663 + 0.4144i -0.2663 - 0.4144i -0.1159 - 0.4839i -0.1159 + 0.4839i -0.5346 + 0.2063i -0.5346 - 0.2063i 1.0000 1.0000

>> F = diag([1 0 0 0],1)+diag([2 2 2 4 4])


F= 2 0 0 0 0 1 2 0 0 0 0 0 2 0 0 0 0 0 4 0 0 0 0 0 4

>> mult(F,0,2) multiplicity of eigenvalue 1: 0 multiplicity of eigenmulvalue 2: 3 >> disteig(F) distinct eigenvalues of input matrix: ans = 2 4 >> eigbasis(F) ??? Error using ==> eigbasis at 38 The input matrix is not diagnalizable. P not invertible.

Das könnte Ihnen auch gefallen