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);
fprintf('Vlastni cisla %g, %g, %g, %g  jsou vsechna kladna \n',vlast)
fprintf('Matice je symetricka pozitivne definitni, konvergence zarucena \n')
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)
hold = [0;0;0;0];
it = 0;
gnew = b - a*xnew;
n = norm(gnew);
fprintf('norma (b - a x) = %g pro pocatecni odhad x \n',n)
while n > prec*bnorm 
   if norm(hold) == 0
       hnew = gnew;
   else
       cit = -gnew'*(a*hold);
       jm = hold'*(a*hold);
       gam = cit/jm;
       hnew = gnew + gam*hold; 
   end
   cit = gnew'*hnew;
   jm = hnew'*(a*hnew);
   alf = cit/jm;
   xold = xnew;
   it = it + 1;
   xnew = xold + alf*hnew;
   hold = hnew;
   gnew = b - a*xnew;
   n = norm(gnew);
   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)
