I am trying to code the inverse power method but the outcome of the eigenvector was half of the correct answer. Is there a way to fix it?

4 views (last 30 days)
clc,clear
format long
A = input('Please enter square matrix A: ');
u = input('Please enter initial guess vector u: ');
s = input('Please enter max number of steps s: ');
[row,column] = size(A);
I = 1;
k = 0;
while I <= column
if u(I) == 0
I = I+1;
elseif u(I) ~= 0
y = sign(u(I))*(u/norm(u));
I = 1;
break
end
end
while k <= s-1
t = rref([A,y]);
z = t(:,row+1);
while I <= column
if y(I)==0 & z(I)==0
I = I+1;
elseif y(I)~=0 & z(I)== 0
I = I+1;
elseif y(I)==0 & z(I)~=0
I = I+1;
elseif y(I)~= 0 & z(I)~=0
p = y(I)/z(I);
sig = sign(z(I));
y = sig*(z/norm(z));
k = k+1;
break
end
end
end
fprintf('The approximation for the eigenvalue of A with smallest absolute value is: %s\n',p)
fprintf('The approximation for the eigenvector of A with smallest absolute value is:\n')
fprintf(' %d\n',[y])
Here is the example for the code:
%The matrix of A is:
[4 1 0; 1 4 1; 0 1 4]
%The initial vector is:
[0.05346163504453; 0.52970019333516; 0.67114938407724]
%The number of steps is:
1000
%The supposed answer is:
[1; -sqrt(2); 1]
%The answer I got from the code is:
[0.5; -sqrt(2)/2; 0.5]

Accepted Answer

Walter Roberson
Walter Roberson on 15 Jun 2021
eigenvectors are direction but not magnitude. If v is an eigenvector of a matrix then so is m*v for all nonzero scalar m. Therefore you can choose to take the result you get and normalize it by dividing through by the first nonzero entry so that the first nonzero entry becomes 1.

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Tags

Products


Release

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!