 % Inputs
 %   A        (nxn matrix) matrix in system to solve
 %   x0       (nx1 vector) initial guess for solution
 %   b        (nx1 vector) right hand side in system to solve
 %   M        (nxn matrix) preconditioner
 %   tol      tolerance to stop iterations
 %   maxiter  maximum number of iterations
 % Outputs
 %   x        solution within tolerance to Ax=b
 %   res      (vector of length number of iterations)
 %            history of residuals
 %
 % TO BE COMPLETED
function [x,res] = mycg(A,x,b,M,tol,maxiter)
for n=1:maxiter,
 % update solution x

 % update residual
 res = [res,norm(r)]; % store residuals for later use
 if (norm(r)<tol) 
  fprintf('convergence to tol=%g in %d iter\n',tol,n);
  break; 
 end;
 % update direction p
end;
