MATLAB: Soring varying and compex eigenvalues

complexeigenvaluesimaglinsysmatlab codermodel linearizersort

I have a linearized system "VariableKp" with a parameter variation "Kp". As the parameter vary, the eigenvalues of the system change. The objective is to see how the eigenvalues change as the parameter sweep is performed, meaning that the rows of "E" or "Q" should contain information about the same eigenvalue, and its development. The order of eigenvalues collected from the "eig" function is however random.
How can I sort these eigenvalues?
Simply using the "sort" command based on real/imag/abs does not work as the eigenvalues change during the parameter sweep.
The problem is illustrated in for example row 7/8 VS 11/12 in the last column in E.
[A,B,C,d] = ssdata(VariableKp); %112 order system.
for k=1:1:5 %Five variations of Kp
Q(:,k)=eig(VariableKp(:,:,k,1));
end
for k=1:1:5
[~,I]=sort(imag(Q(:,k)));
E(:,k) = Q(I,k);
end
E =
1.0e+03 *
-1.0594 - 1.9406i -1.0594 - 1.9406i -1.0594 - 1.9406i -1.0594 - 1.9406i -1.0594 - 1.9406i
-1.0625 - 1.9385i -1.0625 - 1.9390i -1.0625 - 1.9390i -1.0625 - 1.9390i -1.0625 - 1.9390i
-0.0451 - 0.7707i -0.0451 - 0.7707i -0.0451 - 0.7707i -0.0451 - 0.7707i 0.2732 - 0.8849i
-0.0434 - 0.7697i -0.0434 - 0.7702i -0.0434 - 0.7702i -0.0434 - 0.7702i 0.2732 - 0.8849i
-0.0390 - 0.7565i -0.0390 - 0.7565i -0.0390 - 0.7565i -0.0390 - 0.7565i 0.2732 - 0.8849i
-0.0371 - 0.7555i -0.0371 - 0.7560i -0.0371 - 0.7560i -0.0371 - 0.7560i 0.2732 - 0.8849i
-1.8911 - 0.6294i -1.8911 - 0.6294i -1.8911 - 0.6294i -1.8911 - 0.6294i -0.0451 - 0.7707i % 7
-1.8959 - 0.6249i -1.8959 - 0.6254i -1.8959 - 0.6254i -1.8959 - 0.6254i -0.0434 - 0.7702i % 8
-0.0697 - 0.0545i -0.0697 - 0.0545i -0.0697 - 0.0545i 0.0024 - 0.1147i -0.0390 - 0.7565i
-0.0697 - 0.0545i -0.0697 - 0.0545i -0.0697 - 0.0545i 0.0024 - 0.1147i -0.0371 - 0.7560i
-0.0698 - 0.0544i -0.0698 - 0.0544i -0.0698 - 0.0544i 0.0024 - 0.1147i -1.8911 - 0.6294i % should be 7
-0.0698 - 0.0544i -0.0698 - 0.0544i -0.0698 - 0.0544i 0.0024 - 0.1147i -1.8959 - 0.6254i % should be 8
....................................
Q =
1.0e+03 *
-1.0625 + 1.9385i -1.0625 + 1.9390i -1.0625 + 1.9390i -1.0625 + 1.9390i -1.0625 + 1.9390i
-1.0625 - 1.9385i -1.0625 - 1.9390i -1.0625 - 1.9390i -1.0625 - 1.9390i -1.0625 - 1.9390i
-1.0594 + 1.9406i -1.0594 + 1.9406i -1.0594 + 1.9406i -1.0594 + 1.9406i -1.0594 + 1.9406i
-1.0594 - 1.9406i -1.0594 - 1.9406i -1.0594 - 1.9406i -1.0594 - 1.9406i -1.0594 - 1.9406i
-1.8959 + 0.6249i -1.8959 + 0.6254i -1.8959 + 0.6254i -1.8959 + 0.6254i -1.8959 + 0.6254i
-1.8959 - 0.6249i -1.8959 - 0.6254i -1.8959 - 0.6254i -1.8959 - 0.6254i -1.8959 - 0.6254i
-1.8911 + 0.6294i -1.8911 + 0.6294i -1.8911 + 0.6294i -1.8911 + 0.6294i -1.8911 + 0.6294i
-1.8911 - 0.6294i -1.8911 - 0.6294i -1.8911 - 0.6294i -1.8911 - 0.6294i -1.8911 - 0.6294i
-0.0434 + 0.7697i -0.0434 + 0.7702i -0.0434 + 0.7702i -0.0434 + 0.7702i -0.0434 + 0.7702i
-0.0434 - 0.7697i -0.0434 - 0.7702i -0.0434 - 0.7702i -0.0434 - 0.7702i -0.0434 - 0.7702i
-0.0371 + 0.7555i -0.0371 + 0.7560i -0.0371 + 0.7560i -0.0371 + 0.7560i -1.5546 + 0.0000i
-0.0371 - 0.7555i -0.0371 - 0.7560i -0.0371 - 0.7560i -0.0371 - 0.7560i -1.5546 + 0.0000i
................................

Best Answer

This can't be done one the level of individual EIG calls, since it's necessary to track eigenvalues from one call to the next. Take a look at the File Exchange package Eigenshuffle, which takes a set of matrices and orders the eigenvalues for each matrix so that the match the eigenvalues of the previous matrix as closely as possible. Note that I think this relies on one parameter being varied, if you are varying several parameters you may have to get more creative.