我想在matlab中计算高斯siedel方法的迭代次数
这是我的代码
alpha=1;
A=zeros(30,30);
A(1,1)=-(2+alpha);
A(1,2)=1;
for i=2:29
A(i,i-1)=1;
A(i,i)=-(2+alpha);
A(i,i+1)=1;
end
A(30,29)=1;
A(30,30)=-(2+alpha);
D=diag(diag(A));
R=A-D;
x=zeros(30,1);
list=[];
count=0;
for k=1:150
xkk= inv(L)*(b-(U*x));
count=count+1;
list(count,:)=xk;
if(norm(xkk-x)<=10^-5)
break;
end
x=xkk;
end
count
当我运行它时,我得到cound=150
所以看起来计数效果不好
怎么纠正呢?
感谢
答案 0 :(得分:0)
像这样更改最后一部分:
xkk= inv(L)*(b-(U*x));
e=norm(xkk-x);
while(e>10^-5)
xkk= inv(L)*(b-(U*x));
count=count+1;
list(count,:)=xk;
e=norm(xkk-x)
x=xkk;
end