# Different eigenvectors when using eig and null functions

6 views (last 30 days)
Mohammed Raslan on 8 Jun 2020
Commented: Mohammed Raslan on 9 Jun 2020
Hello,
I'm running a simple script to calculate the eigenvalues and eigenvectors of a given matrix using two different MATLAB functions. The eigenvalues from the two functions are matching. However, the eigenvectors are different. Below is the script:
A = [3 2 ; 7 -1];
syms x;
a2a = sym(A);
polya2a = charpoly(a2a,x);
eigenvalues = solve(polya2a);
eig1 = eigenvalues(1,1);
eig2 = eigenvalues(2,1);
v1 = null(A-eig1*eye(2))
v2 = null(A-eig2*eye(2))
[eig1_m, eig2_m] = eig(A);
v1 and v2 that are using the null function is not matching the eigenvectors obtained from eig function. I appreicate your help. Thanks.

David Goodmanson on 8 Jun 2020
Edited: David Goodmanson on 8 Jun 2020
Hi Mohammed,
I tossed in some 'double' functions to make the comparison easier
A = [3 2 ; 7 -1];
syms x;
a2a = sym(A);
polya2a = charpoly(a2a,x);
eigenvalues = solve(polya2a);
eig1 = double(eigenvalues(1,1))
v1 = double(null(A-eig1*eye(2)))
eig2 = double(eigenvalues(2,1))
v2 = double(null(A-eig2*eye(2)))
[w, lambda] = eig(A) % rename some stuff
%-----
eig1 = -3.2426
v1 = 0.3051
-0.9523
eig2 = 5.2426
v2 = 0.6656
0.7463
lambda = 5.2426 0
0 -3.2426
w = 0.6656 -0.3051
0.7463 0.9523
Compared to the first way of doing things, the second way has the order of the eigenvalues reversed. So the first column of w, which corresponds to eigenvalue lambda = 5.2426, should be compared to v2. And the second column of w, which corresponds to eigenvalue lambda = -3.2426, should be compared to v1. Both cases agree.
There is no requirement on the overall scaling of eigenvectors and there can be a constant of proportionality between w(:,1) and v2 for example. In that case the constant equals 1, and it's -1 for w(:,2) vs v1.

Mohammed Raslan on 8 Jun 2020
David Goodmanson on 8 Jun 2020
Hi Mohammed,
I don't know what to say, because I just copied the code back into the editor and it works fine. I'm using 2019b. And the v1 and v2 calculations are your expressions preceded by 'double'. What do you get without using double?
Mohammed Raslan on 9 Jun 2020
Hi David,
Thanks for your response. In my previous comments, I was trying 2019b. Now I attempted to run the same code on 2020a and it finally matches your numbers. I'm not intrigued to know the reason why it worked in 2020 but not 2019.
Thank you very much for your help.