function x=GaussSeidel2(A,b,x,NumIters) % Runs the Gauss-Seidel method for solving Ax=b, starting with x and % running a maximum of NumIters iterations. % % In this case, we run the method as a loop instead of in matrix form. We % will also only output the last iteration. n=length(x); for m=1:NumIters for i=1:n temp1=0; for j=1:i-1 temp1=temp1+A(i,j)*x(j); end temp2=0; for j=i+1:n temp2=temp2+A(i,j)*x(j); end x(i)=(-1/A(i,i))*(temp1+temp2-b(i)); end end