I have this code for the Gauss-Seidel iteraton method, but I do not understand what it is computing. Could anyone please explain this code?
Show older comments
%% set random pos def matrix A with large diagonal entry randn('seed', 1); n=50; B=randn(n, n); [Q, R]=qr(B); A=0.5*n*eye(n)+R'*R;
%% set rhs b corresponding to y as the solution y=1:n; y=y(:); b=A*y;
%% attempt to get solution using gauss seidel method x=zeros(n, 1); itmax=100*n; tol=1e-8; done=0; for it=1:itmax
xold=x;
for i=1:n
t=x;
t(i)=0;
x(i)=(b(i)-A(i, :)*t)/A(i,i);
end
er=norm(x-xold);
fprintf('%5d er=%12.4e\n', it, er);
if er<tol
fprintf('..now less than %e\n', tol);
done=1;
break;
end
end
if done==0 fprintf('..failed to converge with itmax=%d, tol=%e\n', itmax, tol); else fprintf('y, x, y-x follows\n'); [y, x, y-x] end
1 Comment
Alberto
on 10 Apr 2014
Gauss Seidel is an iterative method to solve a system of linear equations. It doesn't converges in every system, it doesnt matter if it has solution or not. The coefficient matrix should be diagonally dominant.
Answers (0)
Categories
Find more on Numerical Integration and Differential Equations 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!