Just a small error in your code. Here's the correction:
function [x] = jacobi(A,b,x0,n_iter)
n=length(x0);
x = zeros(size(x0));
for k=1:n_iter
for i=1:n
temp=0;
for j=1:n
temp=temp+A(i,j)*x0(j); %you should use x0(j) here, not x(j)
end
temp=temp-A(i,i)*x0(i);
x(i)=(1/A(i,i))*(b(i)-temp);
end
x0=x;
end
disp(x)
end
However, you could also do it like this:
function [x] = jacobi(A,b,x0,n_iter)
n=length(x0);
x = zeros(size(x0));
for k=1:n_iter
for i=1:n
temp=0;
inds = [1:i-1, i+1:n];
temp = A(i,inds)*x0(inds);
x(i)=(1/A(i,i))*(b(i)-temp);
end
x0=x;
end
disp(x)
end