MATLAB: How to stop (for) loop that produce matrix column in each iteration by (if) condition (column i+1 == column i)

if condition gauss seidel method numerical

I made the code but the loop does not stop when the (if condition) accusers (column i+1=column i) but it stop the loop after several iteration of column equals . . it is must stop after 5 iteration but stop after 15 . .
X1=[0;0;0];
for i=1:inf
X1(1,i+1)=(7.85+0.1*X1(2,i)+0.2*X1(3,i))/3;
X1(2,i+1)=(-19.3-0.1*X1(1,i)+0.3*X1(3,i))/7;
X1(3,i+1)=(71.4-0.3*X1(1,i)+0.2*X1(2,i))/10;
if ( X1(:,i+1) == X1(:,i))
break
end
end

Best Answer

Use a tolerance for floating point errors,
if norm( X1(:,i+1) - X1(:,i))<=TolX
Related Question