function [u,k,konvergHist] = metodaSdruzenychGradientu(A,b,epsil)

n = length(b);
u = zeros(n,1);
r = b-A*u;
p = r;
normr0 = sqrt(r'*A*r);
normr = normr0;
k = 0;
konvergHist(k+1) = normr/normr0;
Uk = u;
while normr/normr0 > epsil
    alfa = (r'*p) / (p'*A*p);
    u = u + alfa*p;
    Uk = [Uk,u];
    r = b - A*u;
    normr = sqrt(r'*A*r);
    beta = -(r'*A*p) / (p'*A*p);
    p = r + beta*p;
    k = k+1;
    konvergHist(k+1) = normr/normr0;
end

if n==2
    upresne = A\b;
    u1 = linspace(min(Uk(1,:)),max(Uk(1,:)),100);
    u2 = linspace(min(Uk(2,:)),max(Uk(2,:)),100);
    [U1,U2] = meshgrid(u1,u2);
    Phi = 0.5*(A(1,1)*U1.^2+A(1,2)*U1.*U2+A(2,1)*U2.*U1+A(2,2)*U2.^2) - b(1)*U1-b(2)*U2;
    contour(U1,U2,Phi);
    axis equal
    
    hold on
    plot(upresne(1),upresne(2),'r*')
    hold on; plot(Uk(1,:),Uk(2,:),'b')
end