MATLAB中的幂法

dat*_*ili 4 matlab matrix linear-algebra eigenvector numerical-methods

我想在MATLAB中实现用于确定矩阵的主导特征值和特征向量的幂方法

到目前为止,这是我写的内容:

%function to implement power method to compute dominant
%eigenvalue/eigenevctor
function [m,y_final]=power_method(A,x);
m=0;
n=length(x);
y_final=zeros(n,1);
y_final=x;
tol=1e-3;
while(1)
    mold=m;
 y_final=A*y_final;
 m=max(y_final);
 y_final=y_final/m;
 if (m-mold)<tol
     break;
 end
end
end
Run Code Online (Sandbox Code Playgroud)

使用上面的代码,这是一个数字示例:

 A=[1 1 -2;-1 2 1; 0 1 -1]

A =

     1     1    -2
    -1     2     1
     0     1    -1

>> x=[1 1 1];
>> x=x';
>> [m,y_final]=power_method(A,x);
>> A*x

ans =

     0
     2
     0
Run Code Online (Sandbox Code Playgroud)

与MATLAB中上述矩阵的特征值和特征向量进行比较时,我做到了:

[V,D]=eig(A)

V =

    0.3015   -0.8018    0.7071
    0.9045   -0.5345    0.0000
    0.3015   -0.2673    0.7071


D =

    2.0000         0         0
         0    1.0000         0
         0         0   -1.0000
Run Code Online (Sandbox Code Playgroud)

特征值重合,但特征向量应接近[1/3 1 1/3]。在这里,我得到:

 y_final

y_final =

    0.5000
    1.0000
    0.5000
Run Code Online (Sandbox Code Playgroud)

看到这种错误是可以接受的,还是我犯了一些错误?

ray*_*ica 5

你有正确的执行,但你不检查收敛的特征向量和特征值。您只需检查特征值是否收敛。幂方法同时估计突出的特征向量和特征值,因此检查两者是否收敛可能是个好主意。当我这样做时,我设法得到了[1/3 1 1/3]。这是我修改您的代码以实现此目的的方式:

function [m,y_final]=power_method(A,x)
m=0;
n=length(x);
y_final=x;
tol=1e-10; %// Change - make tolerance more small to ensure convergence
while(1)
     mold = m;
     y_old=y_final; %// Change - Save old eigenvector
     y_final=A*y_final;
     m=max(y_final);
     y_final=y_final/m;
     if abs(m-mold) < tol && norm(y_final-y_old,2) < tol %// Change - Check for both
         break;
     end
end
end
Run Code Online (Sandbox Code Playgroud)

当使用您的示例输入运行上述代码时,我得到:

>> [m,y_final]=power_method(A,x)

m =

     2


y_final =

    0.3333
    1.0000
    0.3333
Run Code Online (Sandbox Code Playgroud)

关于方面eig,MATLAB最有可能使用另一个范数来缩放特征向量。请记住,特征向量不是唯一的 ,并且在按比例缩放时都是准确的。如果您想确定的话,只需取V与优势特征向量重合的的第一列,然后除以最大值,这样我们就可以将一个分量以1的值归一化,就像幂方法:

>> [V,D] = eig(A);
>> V(:,1) / max(abs(V(:,1)))

ans =

    0.3333
    1.0000
    0.3333
Run Code Online (Sandbox Code Playgroud)

这与您所观察到的一致。