MATLAB中的幂法 [英] Power Method in MATLAB

查看:490
本文介绍了MATLAB中的幂法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实施幂方法来确定主导特征值和MATLAB中矩阵的特征向量.

I would like to implement the Power Method for determining the dominant eigenvalue and eigenvector of a matrix in MATLAB.

这是我到目前为止写的:

Here's what I wrote so far:

%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

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

With the above code, here is a numerical example:

 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

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

When comparing with the eigenvalues and eigenvectors of the above matrix in MATLAB, I did:

[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

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

The eigenvalue coincides, but the eigenvector should be approaching [1/3 1 1/3]. Here, I get:

 y_final

y_final =

    0.5000
    1.0000
    0.5000

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

Is this acceptable to see this inaccuracy, or am I making some mistake?

推荐答案

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

You have the correct implementation, but you're not checking both the eigenvector and eigenvalue for convergence. You're only checking the eigenvalue for convergence. The power method estimates both the prominent eigenvector and eigenvalue, so it's probably a good idea to check to see if both converged. When I did that, I managed to get [1/3 1 1/3]. Here is how I modified your code to facilitate this:

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

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

When I run the above code with your example input, I get:

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

m =

     2


y_final =

    0.3333
    1.0000
    0.3333


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


On a side note with regards to eig, MATLAB most likely scaled that eigenvector using another norm. Remember that eigenvectors are not unique and are accurate up to scale. If you want to be sure, simply take the first column of V, which coincides with the dominant eigenvector, and divide by the largest value so that we can get one component to be normalized with the value of 1, just like the Power Method:

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

ans =

    0.3333
    1.0000
    0.3333

这与您所观察到的一致.

This agrees with what you have observed.

这篇关于MATLAB中的幂法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆