disp('Gauss-Seidel')
fprintf('Zadana symetricka matice a\n')
a = [7 -1 1 3; -1 1 0 2; 1 0 3 2; 3 2 2 10];
vlast = eig(a);
b = [1;1;2;3];
fprintf('Zadana prava strana b = %g, %g, %g \n',b)
bnorm = norm(b);
prec = 1.e-6;
fprintf('zadana presnost %g \n',prec)
xnew = [1;1;1;1]
fprintf('pocatecni odhad reseni %g, %g, %g, %g \n',xnew)
it = 0;
dit = b - a*xnew;
n = norm(dit);
fprintf('norma (b - a x) = %g pro pocatecni odhad x \n',n)
while n > prec*bnorm
   xnew(1) = b(1);
   for i = 2:4
       xnew(1) = xnew(1) - a(1,i)*xnew(i);
   end
   xnew(1) = xnew(1)/a(1,1);
   for j = 2:3
       xnew(j) = b(j);
       for i=1:j-1
          xnew(j) = xnew(j) - a(j,i)*xnew(i);
       end
       for i=j+1:4      
          xnew(j) = xnew(j) - a(j,i)*xnew(i);
       end
       xnew(j) = xnew(j)/a(j,j);
   end    
   xnew(4) = b(4);     
   for i=1:3
          xnew(4) = xnew(4) - a(4,i)*xnew(i);
   end
   xnew(4) = xnew(4)/a(4,4); 
   it = it +1;
   dit = b - a*xnew;
   n = norm(dit);
   fprintf('Po %d iteraci je x = %g, %g, %g, %g \n',it,xnew)  
   fprintf('Po %d iteraci je norma (b - a x) = %g \n',it,n)  
end
fprintf('Reseni nalezeno po %d iteracich \n',it)
fprintf('Metoda nalezla reseni x = %g, %g, %g, %g \n',xnew)   
x = inv(a)*b;
fprintf('Presne reseni x = %g, %g, %g, %g \n',x)
