function [res, noiter] = mincg(f, derf, ftau, x, tol)
global p1 d1
n = size(x);
noiter = 0;
df = feval(derf, x);
while norm(df) > tol
   noiter = noiter + 1;
   df = feval(derf, x);
   d1 = -df;
   for inner = 1:n
      p1 = x;
      %tau = fmin(ftau, -10, 10, [0 .00000005]);
      tau = fminbnd(ftau, -10, 10);
      x1 = x + tau*d1;
      dfp = df;
      df = feval(derf, x1);
      d = d1;
      x = x1;
      beta = (df'*df)/(dfp'*dfp);
      d1 = -df + beta*d;
   end;
   disp('approximate solution: '); disp(x1);
end;
res = x1;
disp('Number of iterations = '); disp(noiter);
disp('Solution'); disp(x1);
disp('Gradient'); disp(df);