Eigenvalues getting mixed up when looping through matrices in Matlab

6 replies · 3K views · last reply
member 428835
Hi PF!

I am looping through a linear system and each time I do I generate a new matrix, call this matrix ##A##. When finding the eigenvalues of ##A## in Matlab is use
Code:
[a,sigma2M] = eig(A);% a eigenvector and sigma2M matrix of eigenvalues
sigma2(:,ii) = sum(sigma2M);% create matrix with rows of eigenvalues for the iith system
where the ii is the for loop. The plot I get looks bad and you can see how some eigenvalues get mixed up (vertical axis are eigenvalues). Any ideas? I'd really appreciate it!
Line plot with starred markers of sigma squared against L/R from about 2 to 30, showing several eigenvalue branches that rise toward zero or settle near -0.2, -4, and 0, with erratic crossing segments at small L/R.
 
Orodruin said:
Sort the eigenvalues?
Doesn't it automatically do this for me? Is there a way to have this automated?
 
joshmccraney said:
Doesn't it automatically do this for me? Is there a way to have this automated?
If it did you would not get the result you are getting. Use the sort function.

I also recommend against using sum to get the eigenvalues out. The matrix contains the eigenvalues on the diagonal so use diag instead. For a square matrix it will give you a vector containing the diagonal elements.
 
  • Like
Likes   Reactions: member 428835
Orodruin said:
If it did you would not get the result you are getting. Use the sort function.

I also recommend against using sum to get the eigenvalues out. The matrix contains the eigenvalues on the diagonal so use diag instead. For a square matrix it will give you a vector containing the diagonal elements.
Good call on the diag function. I'd like the eigenvalues to remain paired with their corresponding eigenvectors. When I use sort on the eigenvalues, that only change their positions? Do you know of a way to have the eigenvectors align with the eigenvalues?
 
If you have the sort function output two return values, i.e.,
Code:
[B,I] = sort(A);
then B will contain the sorted list of eigenvalues and I will be a list of indices which is the order of the original indices. You can sort the eigenvectors by using the array I accordingly as an argument of the matrix containing the eigenvectors.
 
  • Like
Likes   Reactions: member 428835