A = [10 -1 2 0;-1 11 -1 3;2 -1 10 -1;0 3 -1 8];
b = [6;25;-11;15];
%b = [6 25 -11 15]';
x = [0 0 0 0]';
tolerance = 0.001;

max_it = 20;
n = length(b)
output = [0 x'];

for k=1:max_it
for i=1:n
xnew(i) = (b(i) - A(i,1:i-1)*x(1:i-1) - A(i,i+1:n)*x(i+1:n))/A(i,i);
%
% SAME AS: xnew(i) = (b(i) - A(i,1:n)*x(1:n))/A(i,i) + x(i);
%
end
norm1 = max(abs(xnew-x'));
norm2 = max(abs(xnew));
if ((norm1/norm2)<tolerance)
output = [output; k xnew];
break
end
output = [output; k xnew];
x=xnew';
end

plot(output(:,1),output(:,2),'r');
hold on;
plot(output(:,1),output(:,3),'g');
hold on;
plot(output(:,1),output(:,4),'b');
hold on;
plot(output(:,1),output(:,5),'m');
hold on;

title('Gauss Jacobi for example 1');
xlabel('Iteration');
ylabel('x-value');
legend('x1','x2','x3','x4')