How to add tolerance to iteration with if statement
Show older comments
I have a Gauss-seidel iteration code, which works to 99 iterations with a relaxation factor. However, when I put an if statement in (no other changes), to say if the different between iteration a^x+1-a^x is less than a certain tolerance say 0.1 then stop the iteration sequence before the specified 99 iterations.
Any help to spot any issues would be appreciated!
a2(1,1)=0;
b2(1,1)=0;
c2(1,1)=0;
R2=0.3;
for i=1:99
if a2(i+1,1)-a2(i,1)<0.1
a2(i+1,1)=(1-R2)*a2(i,1)+R2*(4-b2(i,1)-c2(i,1));
b2(i+1,1)=(1-R2)*b2(i,1)+R2*(2*a2(i+1,1)+4*c2(i,1)-33)/3;
c2(i+1,1)=(1-R2)*c2(i,1)+R2*(3*a2(i+1,1)-2*b2(i+1,1)-2)/2;
end
end
figure(5);
plot(a1);
hold on
plot (b1);
plot (c1);
Accepted Answer
More Answers (1)
Chunru
on 27 Oct 2022
% if a2(i+1,1)-a2(i,1)<0.1
% change the above to:
if abs(a2(i+1,1)-a2(i,1))<0.1
3 Comments
Bethany Sinclair
on 27 Oct 2022
Chunru
on 27 Oct 2022
Not sure what algo you want to implement. Can you describe it?
Bethany Sinclair
on 27 Oct 2022
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!