Eigenvalues and eigenvectors: Eigenvalues and eigenvectors in MATLAB
Eigenvalues and eigenvectors
You can compute eigenvalues and eigenvectors in MATLAB with the function eig
, applied to both numerical and symbolic matrices. Below we give two examples: \[A=\matrix{1 & -3 & 3 \\ 3 & -5 & 3\\ 6 & -6 & 4}\quad\text{and}\quad \matrix{-1 & 1 & -1 \\ -7 & 5 & -1\\ 6 & -6 & -2}\] These two matrices have the same characteristic polynomial \((t+2)^2(t - 4)\), but \(A\) is diagonalizable while \(B\) is not. This is because the eigenspace for eigenvalue \(-2\) of matrix \(A\) has two eigenvectors which is not a multiple of each other, while this is not true in the case of matrix \(B\).
>> A = [1 -3 3; 3 -5 3; 6 -6 4]
A =
1 -3 3
3 -5 3
6 -6 4
>> B = [-3 1 -1; -7 5 -1; -6 6 -2]
B =
-3 1 -1
-7 5 -1
-6 6 -2
>> syms t; symA = sym(A); symB = sym(B);
>> charpoly(symA,t) % characteristic polynomial of A in t
ans =
t^3 - 12*t - 16
>> factor(ans) % factorisation
ans =
[ t - 4, t + 2, t + 2]
>> [V,D] = eig(symA) % eigenvectors and eigenvalues of A
V =
[ 1/2, 1, -1]
[ 1/2, 1, 0]
[ 1, 0, 1]
D =
[ 4, 0, 0]
[ 0, -2, 0]
[ 0, 0, -2]
>> % column vectors of V are eigenvectora
>> % diagonal elements of D are eigenvalues
>> symA*V - V*D % check
ans =
[ 0, 0, 0]
[ 0, 0, 0]
[ 0, 0, 0]
>> factor(charpoly(symB, t)) % factors of the characteristic polynomial of B in t
ans =
[ t - 4, t + 2, t + 2]
>> [V,D] = eig(symB) % eigenvects and eigenvalues of B
V =
[ 0, 1]
[ 1, 1]
[ 1, 0]
D =
[ 4, 0, 0]
[ 0, -2, 0]
[ 0, 0, -2]
>> symB*V - V*(D(1:2,1:2))
ans =
[ 0, 0]
[ 0, 0]
[ 0, 0]
Numerical computations are more efficient, but the output differs (in a non-essential sense).
>> [V,D] = eig(A) % eigenvectors and eigenvalues of A
V =
-0.4082 -0.8103 0.1933
-0.4082 -0.3185 -0.5904
-0.8165 0.4918 -0.7836
D =
4.0000 0 0
0 -2.0000 0
0 0 -2.0000
>> A*V-V*D
ans =
1.0e-14 *
-0.0888 0.1554 0.0333
-0.0444 0.1110 0.0444
-0.0888 0.0999 0.0444
>> [V,D] = eig(B) % eigenvectors and eigenvalues of B
V =
0.0000 -0.7071 0.7071
-0.7071 -0.7071 0.7071
-0.7071 0.0000 0.0000
D =
4.0000 0 0
0 -2.0000 0
0 0 -2.0000
>> B*V-V*D
ans =
1.0e-14 *
-0.0685 -0.1776 0.1332
0.0888 -0.0444 -0.0222
0.0444 -0.0540 -0.0792
In this case you must notice all by yourself that the last two column vectors of \(V\) are in fact multiples of each other and that the eigenspace of \(B\) for eigenvalue \(-2\) is spanned by one eigenvector.