where is the error in code of Shifted Power Method?

1 次查看(过去 30 天)
Ash=[3 -1 0;-2 6 -2;0 -1 3];
v=zeros(3,10);
vT=zeros(10,3);
v(:,1)=[0;1;0];
vT(1,:)=[0;1;0];
l=6.9996;
Av=zeros(3,10);
AvT=zeros(10,3);
m=zeros(10,1);
error=zeros(10,1);
display('-------------------------------------------------------------------------------------------')
display(' k [v^k]^T [Ashifited v^k]^T mk+1 |error|')
display('-------------------------------------------------------------------------------------------')
for k=1:10
if error(k,1)<0.0005
Ash=Ash-l*eye(3);
Av(:,k)=Ash*v(:,k);
AvT(k,:)=(Av(:,k))';
m(k+1,1)=Av(1,k);
vT(k,:)=(v(:,k))';
error(k+1,1)=abs(m(k+1,1)-m(k,1));
v(:,k+1)=Av(:,k)/m(k+1,1);
l=m(k+1,1);
end
end
  1 个评论
Jeffrey Clark
Jeffrey Clark 2022-6-30
@sadiqah aldahan, you should not use MATLAB commands (e.g., error) as your variable names. See Variable Names and use exist to check. Also don't use display where disp should be used. But your real issue is that when k==3 v(:,k+1)=Av(:,k)/m(k+1,1) divides 0 by 0 which results in NaN being stored in v which in turn results in plenty of NaN in almost every variable.

请先登录,再进行评论。

回答(1 个)

Amanjeet Pani
Amanjeet Pani 2022-7-13
Do not use MATLAB keywords (e.g., error) as your variable names.
To display on the matlab command window use disp().
Also when k==3, v(:,k+1)=Av(:,k)/m(k+1,1) divides 0 by 0 which results in NaN being stored in v which in turn results in plenty of NaN in almost every variable.

类别

Help CenterFile Exchange 中查找有关 Scripts 的更多信息

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by