function [xnew,gradnew,alpha,pnew] = lincg(fnhess,fngrad,fn,gradold,xold,p)
% usage: [xnew,gradnew,alpha,pnew] = lincg(fnhess,fngrad,fn,gradold,xold,p)
% description: solves the quadratic model problem:
% min m(p) = fk + gradfk'*p+0.5*p'*B*p 
% Input parameters:
% fngrad: function grad name
% fn: function name
% gradold: gradient at starting point
% xold: starting point
% p: current search direction

% global variables:
global parms;

% no bulletproofing or safeguarding
A = feval(fnhess,xold); 
alpha = gradold'*gradold/(p'*A*p);
xnew = xold + alpha*p;
gradnew = feval(fngrad,xnew);
pnew = -gradnew + (gradnew'*gradnew)/(gradold'*gradold)*p;
