eig() gives eigenvalues in the "wrong" order

6 views (last 30 days)
I am ploting the eigenvalues for a 2x2 matrix, variating one of the elements of such matrix from 2 to 6.
a=-2:0.01:6; %Define range
for i=1:length(a)
A2(:,:,i)= eig([0 1; -a(1,i) -1]); %Get eigenvalues from matrix
end
E_real=squeeze(real(A2)) %Get rid of extra dimensions
E_real = 2×801
1.0000 0.9967 0.9933 0.9900 0.9866 0.9832 0.9799 0.9765 0.9731 0.9697 0.9663 0.9629 0.9595 0.9560 0.9526 0.9491 0.9457 0.9422 0.9387 0.9353 0.9318 0.9283 0.9248 0.9213 0.9177 0.9142 0.9107 0.9071 0.9036 0.9000 -2.0000 -1.9967 -1.9933 -1.9900 -1.9866 -1.9832 -1.9799 -1.9765 -1.9731 -1.9697 -1.9663 -1.9629 -1.9595 -1.9560 -1.9526 -1.9491 -1.9457 -1.9422 -1.9387 -1.9353 -1.9318 -1.9283 -1.9248 -1.9213 -1.9177 -1.9142 -1.9107 -1.9071 -1.9036 -1.9000
E_imag=squeeze(imag(A2)) %Get rid of extra dimensions
E_imag = 2×801
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
hold on
grid on
plot(E_real(1,:),E_imag(1,:)) %plot eig1
plot(E_real(2,:),E_imag(2,:)) %plot eig2
But, when I run this code the graphic didnt look right. When I looked into it, it turns out that for the value 101 of a, the results are swaped, givin origin to the "jump" that the graphic seems to make. Is there any way to avoid this? have anyone experimented something like this?
Another thing I would like is to label each point in the graphic such that when I click it it would give me the value of a for the point (sorta like nyquist() does for values of w).
Thanks for your answers!

Accepted Answer

Matt J
Matt J on 4 Jan 2023
Edited: Matt J on 4 Jan 2023
eigenshuffle from this FEX post might be applicable,

More Answers (1)

John D'Errico
John D'Errico on 4 Jan 2023
Edited: John D'Errico on 4 Jan 2023
(You should accept @Matt J's answer. I'm just expanding on it, but if I make this as a comment on Matt's answer, the comments tend to be lost after other comments are made.)
The problem is the eigenvalues of a matix are a pair of roots of a quadratic polynomial. But eig does not care that your matrix is one of a sequence of matrices, where the roots of the characteristic polynomials change as the matrix changes due to a parameter. Well, it is not that eig cares about anything. Code is just code. :)
Hmm. How should I explain this? Think of an eigenvalue as the protagonist in a b-movie. At some point in time, the hero makes a decision that will fundamentally change his or her life. It all comes down to an arbitrary choice, but a choice will be made. Perhaps an example might help here.
syms x z0
Q = expand((x-1+z0)*(x+1-z0))
Q = 
Consider the function Q(x). It is parameterized by the variable z0. Now, imagine that z0 changes, and we want to trace the roots of the quadratic polynomial Q, as a function of z0.
We might do this:
qcoeff = @(z) [1, 0, -z^2+2*z-1];
qroots = @(z) roots(qcoeff(z));
Now we see that we can compute the roots as a function of z. For example:
qroots(-2)
ans = 2×1
3 -3
But which root is returned first is not really clear. So I'll plot the two roots.
nz = 100;
zlevels = linspace(-5,5,nz)';
Rz = zeros(nz,2);
for i = 1:nz
Rz(i,:) = qroots(zlevels(i));
end
plot(zlevels,Rz(:,1),'r-',zlevels,Rz(:,2),'b-')
xlabel z
ylabel qroots(z)
legend('1st root','2nd root')
The line drawn in red is the first root returned, the line drawn in blue was the second root.
What you should see is that at z == 1, this was the point where the protagonist had to make a choice. Which path do I follow?
Anyway, the trick is to use a tool like my eigenshuffle code. It shuffles the roots around to then follow a consistent path.

Categories

Find more on Linear Algebra in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!