A=[9 4 1;
1 6 0;
1 -2 -6];
C=[-17;
4;
14];
x=GaussSeidelWithCounter(A,C);
x=PointJacobiWithCounter(A,C);
function x=GaussSeidelWithCounter(A,C)
x=zeros(size(C));
[n,n]=size(A);
residual=A*x-C;
counter=0;
while max(abs(residual)) > 1.0e-8
for i=1:n
sum=0.0;
for j=1:n
if j~=i
sum=sum-A(i,j)*x(j);
end
end
x(i)=(sum+C(i))/A(i,i);
end
residual=A*x-C;
counter=counter+1;
end
disp(counter)
end
function x=PointJacobiWithCounter(A,C)
xold=ones(size(C));
x=xold;
[n,n]=size(A);
residual=A*x-C;
counter=0;
while max(abs(residual)) > 1.0e-8
for i=1:n
sum=0.0;
for j=1:n
if j~=i
sum=sum-A(i,j)*xold(j);
end
end
x(i)=(sum+C(i))/A(i,i);
end
xold=x;
residual=A*x-C;
counter=counter+1;
end
disp(counter)
end