MATLAB: [Solved] Power method, eigenvalues.

eigenvaluesMATLABmethodpower

function l = ww(A,E)
n = length(A);
y = [];
x = [];
for i = 1:n % starting vector
x(i) = A(i,1);
end;
l = 0;
blad = E; % starting value of error
while blad>=E
for i = 1:n % A*x
y(i) = 0;
for j = 1:n
y(i) = y(i) + A(i,j)*x(j);
end;
end;
blad = l;
l = 0; % Rayleigh
m = 0;
for i = 1:n
l = l + x(i)*y(i);
m = m + x(i)*x(i);
end;
l = l/m; % eigenvalue
blad = abs(l - blad); % error
x = y;
end;
end
That's how I've tried to compute eigenvalues. It works for some matrices, but for:
A =
0 -0.3333 -0.3333
-0.3333 0 0.3333
0.6000 0.2000 0
it doesn't work. How can I fix that?

Best Answer

Simple power iteration only works when there is a single dominant eigenvalue. The matrix
A =[ 0 -0.3333 -0.3333
-0.3333 0 0.3333
0.6000 0.2000 0];
has 3 eigenvalues,
-0.3333
0.1667 + 0.3249i
0.1667 - 0.3249i
with absolute values:
0.3333
0.3651
0.3651
As you can see, the dominant eigenvalue is not unique. That is why your algorithm fails to converge.
One way to fix this is by using shifts (you can read all about it on Google).
But why not just use MATLAB's built in eigevnalue solver, EIG?
eig(A)