function [x,nit]=gradcon(A,b,x0,toll,nmax)
x=x0;
res=b-A*x;
d=res;
toll=toll*norm(b);
for nit=1:nmax
    alpha=d'*res/(d'*A*d);
    x=x+alpha*d;
    res=res-alpha*A*d;
    if norm(res)<toll, return, end
    beta=-(d'*A*res)/(d'*A*d);
    d=res+beta*d;
end