Power Method in MATLAB

I would like to implement the Power Method to determine the dominant eigenvalue and matrix eigenvector in MATLAB.

Here is what I have written 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

When comparing with 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

The eigenvalue is the same, but the eigenvector should approach [1/3 1 1/3]. Here I get:

 y_final

y_final =

    0.5000
    1.0000
    0.5000

Is it acceptable to see this inaccuracy, or am I mistaken?

+4
source share
1 answer

, . . , , , , , . , [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

, :

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

m =

     2


y_final =

    0.3333
    1.0000
    0.3333

eig, MATLAB, , , . , . , V, , , 1, Power :

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

ans =

    0.3333
    1.0000
    0.3333

, .

+4

All Articles