function [x,nit,er,res]=gradcon(A,b,toll,nmax,P)
  if nargin < 5
    P=eye(length(A));
  end
  x=zeros(size(b));
  nit=0;
  er='';
  r=b-A*x;
  z=P\r;
  p=z;
  res=norm(r);
  C=norm(b);
  tollC=C*toll;    
  while nit < nmax & res > tollC
    nit=nit+1;
    alpha=(p'*r)/(p'*A*p);
    x=x+alpha*p;
    r=r-alpha*A*p;
    z=P\r;
    beta=(z'*A*p)/(p'*A*p);
    p=z-beta*p;
    res=norm(r);
  end
  if res > tollC | isnan(res)
    er='Test di arresto non soddisfato';
  end
