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