function t = rlin(x,t0,d,fun,funp)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% function t = rlin(x,t0,d,fun,funp)
% Wolfe linear search for min_t f(x+td)
% t0 = initial guess
% fun = f(x)
% funp = gradf(x)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
a = 10; m1 = 1/4; m2 = 3/4;
tg = 0; td = 0; itmax = 100; 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
t = t0; 
q0 = feval(fun,x);
yp0 = feval(funp,x);
qp0 = yp0'*d;
if (qp0 > 0)
 disp('direction de montee dans rlin');
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for it = 1:itmax
 q = feval(fun,x+t*d);
 yp = feval(funp,x+t*d);
 qp = yp'*d;

 if ((q <= q0+m1*t*qp0) & (qp >= m2*qp0))
  break
 elseif  ((q <= q0+m1*t*qp0) & (qp < m2*qp0))
  tg = t;
 else
  td = t;
 end

 if (td == 0)
  t = a*t;
 else
  t = (tg+td)/2;
 end
end 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
