# How to keep track of order eigenvalue in eig or eigs?

20 views (last 30 days)
Luqman Saleem on 13 Apr 2020
Commented: Ameer Hamza on 14 Apr 2020
I have a matrix equation defined at each point of variable k. The equation is where
and is a 6-by-6 matrix.
Once I solve my matrix and I plot the eigenvalues using eig (and eigs) results, I get the following:
But the correct results are 3 two-fold lines of the following form (here the x-axis is little shifted and in some other units, but essentially it is same as my x-axis).
I am trying to figure out what I am doing wrong. My solution looks someone close to the correct solution but that mess in between the lines is annoying.
May be the the order in which eig() (or eigs()) function return eigenvalues is my problem. I am have used both eig(M_k) and eigs(M_k) function. They both give almost identical results.

Ameer Hamza on 13 Apr 2020
The order of eigenvalues and eigenvectors is an issue with the MATLAB's eig() function. John's submission on FEX provides a workaround: https://www.mathworks.com/matlabcentral/fileexchange/22885-eigenshuffle

Luqman Saleem on 13 Apr 2020
haa! solved. thank you
Ameer Hamza on 14 Apr 2020

Christine Tobler on 13 Apr 2020
The eigenvalues returned by EIG are not sorted in any particular order generally (for some special types of matrices (e.g. exactly symmetric), they might often be sorted in the same order, but we make no guarantees that this is always the case). For EIGS the eigenvalues are sorted in the order given by option sigma (e.g. 'largestabs', ...).
To sort the eigenvalues returned, just call sort after computing EIG:
d = eig(A);
d = sort(A);
or, for eigenvectors too,
[U, D] = eig(A);
[~, ind] = sort(diag(D));
D = D(ind, ind);
U = U(:, ind);
Since you are computing EIG of multiple matrices, if the lines describing each eigenvalue in your plot cross, this will not show continuous lines. It's not possible for each individual computation of eigenvalues to know about the connection to another, very similar, matrix A and its eigenvalues. In this case, the File Exchange package eigenshuffle that Ameer recommends may help you.

Translated by