% LINESEARCHMIN   Search the minimum for the steepest descent method
%             
%  [lambda]=linesearchmin(f,xk,maxstep)
%  Outputs the minimum value of lambda, used for 
%  x(k+1)=x(k)+lambda grad(x(k))
%  Calling sequence: linesearchmin(f,xk)
%
%  INPUTS
%  f-> function, see xyfunc for an example
%  xk-> current search point.
%  maxstep-> denotes the maximum size of lambda, lambda between
%  [0,maxstep], standard set to 2
%
%
%  OUPUT
%  the lambda, for which f(x(k+1)) is minimized
%
%
%   Software written by Raf Vandebril
%   Raf.Vandebril-at-cs.kuleuven.be
%   Revision Date: 10/05/2010



function [lambda]=linesearchmin(f,xk,maxstep)
  
  % Maximum stepsize is standard set to 2
  if (nargin<3)
    maxstep=2;
  end
  
  
  % The bounds between which we search are bounda and boundb
  bounda=0;
  boundb=maxstep;
  tol=10^(-10);
  
  % For constructing the function to be minimized, we need the gradient
  [func,grad]=feval(f,xk); 
  
  % minimize now the function, between the bounds 
  lambda=fminbnd(@(lambda)feval(f,xk-lambda*(grad/norm(grad))),bounda,boundb, ...
                 optimset('TolX',tol));
  
  % Output warning if lambda is too large
  if ((lambda==bounda)|(lambda==boundb))
    disp('Warning maximum step length for alpha has been reached')
  end
  
