function [x,iter]=konjgrad(A,b,toll,nmax)
[n,n]=size(A);
iter=0; r=b; rold=r; r0=norm(r); p=r; err=r0; x=zeros(n,1); 
while err>toll & iter<nmax
iter=iter+1;
alpha=(rold'*rold)/(p'*A*p);
x=x+alpha*p;
r=rold-alpha*A*p;
beta=r'*r/(rold'*rold);
p=r+beta*p;
rold=r;
err=norm(r)/r0;
end;
