Jacobi
Jacobi
Owen Blair
University of Idaho
%}
function [x] = Jacobi(A,b,x_i,tol)
[~,n]=size(A);
disp(n);
N=1;
Const_jac=0;
error_jac=zeros(1,1);
xk=0;
xk1=0;
xk2=0;
x=x_i*ones(1,n);
while 1
%Debug for loop
%fprintf('Iteration %d\n', N);
xold=x;
for i=1:n
sigma=0;
for j=1:n
if j~=i
sigma=sigma+A(i,j)*x(j);
end
end
x(i)=(1/A(i,i))*(b(i)-sigma);
end
error_jac(N)=max(max(abs(xold-x)));
N=N+1;
if max(max(abs(xold-x))) < tol
break;
end
%Save the last two numbers
xk2=xk1;
xk1=xk;
xk=x;
%Calculate Asymptotic error constant
Const_jac=(max(abs(xk - xk1)))/(max(abs(xk1 - xk2)));
end
fprintf('Error limit reached at step %d\nThe asymptotic error constant is
%d',N,Const_jac);
semilogy(error_jac);
%disp(error_jac);
%disp(Const_jac);
end