jacobi_aa1

function [x, iflag, itnum] = Jacobi(A,b,x0,delta,max_it)
%   x0: initial guess 
%   delta: error tolerance for the relative difference between two consecutive iterates
%   max_it: maximum number of iterations to be allowed
% %   iflag: 1 if a numerical solution satisfying the error
%   itnum: the number of iterations used to compute x
n = length(b);
iflag = 1;
k = 0;
diagA = diag(A);
A = A-diag(diag(A));
while k < max_it
k = k+1;
x = (b-A*x0)./diagA;
relerr = norm(x-x0,inf)/(norm(x,inf)+eps);
x0 = x;
if relerr < delta
break
end
end
itnum = k;
if (itnum == max_it)
iflag = -1
end

 

Posted in Uncategorized