Eigenvalues in MATLAB

倾然丶 夕夏残阳落幕 提交于 2019-11-30 19:38:01
gnovice

You just have to find the index of the largest eigenvalue in D, which can easily be done using the function DIAG to extract the main diagonal and the function MAX to get the maximum eigenvalue and the index where it occurs:

[V,D] = eig(a);
[maxValue,index] = max(diag(D));  %# The maximum eigenvalue and its index
maxVector = V(:,index);           %# The associated eigenvector in V

NOTE: As woodchips points out, you can have complex eigenvalues for non-symmetric matrices. When operating on a complex input X, the MAX function uses the magnitude of the complex number max(abs(X)). In the case of equal magnitude elements, the phase angle max(angle(X)) is used.

What I usually do is:

[V D] = eig(a);
[D order] = sort(diag(D),'descend');  %# sort eigenvalues in descending order
V = V(:,order);

Note that non-symmetric matrices tend to have complex eigenvalues.

eig(rand(7))
ans =
       3.2957              
     -0.22966 +    0.58374i
     -0.22966 -    0.58374i
     -0.38576              
      0.49064              
      0.17144 +    0.27968i
      0.17144 -    0.27968i

Also note that eig does not explicitly return sorted eigenvalues (although the underlying algorithm tends to produce them in a nearly sorted order, based on the magnitude of the eigenvalue), but even if you do do a sort, you need to understand how sort works on complex vectors.

sort(rand(5,1) + i*rand(5,1))
ans =
      0.42343 +    0.51539i
    0.0098208 +    0.76145i
      0.20348 +    0.88695i
      0.43595 +    0.83893i
       0.8225 +    0.91264i

Sort, when applied to complex inputs, works on the magnitude of the complex number.

If you only care for the eigenvector associated with the largest eigenvalue, isn't it better to use eigs?

[V, D] = eigs( a, 1, 'lm' ); %// get first eigenvector with largest eigenvalue magnitude.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!