i have made a matlab code of gauss-siedel but there's a slight problem . i need to make the output print 20 iteration but i only get 18. any advice?

조회 수: 1 (최근 30일)
%6x-y-z=19
%3x+4y+z=26
%x+2y+6z=22
%initial values for y and z is 0
%x=19+y+z/6
%y=26-3x-z/4
%z=22-x-2y/6
clear;clc;format('long','g');
i=1;
y(i)=0;
z(i)=0;
error_x(i)=1;
%gauss-siedel opperation begin
while error_x(i) >= 0.00000000000000001
x(i+1)=((19 + y(i) +z(i) )/6);
y(i+1)=((26 - (3*x(i+1))- z(i)) /4);
z(i+1)=((22 - x(i+1)- 2*y(i+1)) /6);
error_x(i+1)=abs((x(i+1)-x(i))/x(i+1))*100;
error_y(i+1)=abs((y(i+1)-y(i))/y(i+1))*100;
error_z(i+1)=abs((z(i+1)-z(i))/z(i+1))*100;
i=i+1;
end
%print out the value
disp(' x error(%)');
disp([x', error_x'])
disp(' y error(%)');
disp([y', error_y'])
disp(' z error(%)');
disp([z', error_z'])

채택된 답변

Cris LaPierre
Cris LaPierre 2021년 11월 19일
If you need a specific number of iteractions, use a for loop instead of a while loop.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by