%
% linesrch - perform a simple line search
%
%   xend = linesrch( func, xstart, istep, fplot, ...
%                        [ finc, fdec, [ minstep ]] )
%
% where  func    = name of objective function
%        xstart  = starting point vector
%        istep   = initial step size vector
%        fplot   = plot flag
%        finc    = step acceleration factor
%                  (default = 1, that is none)
%        fdec    = step decceleration factor
%                  (default = 1, that is none)
%        minstep = minimum step size (if deccelerating)
%
% global variable "stats" contains:
%        stats(1) = number of function evaluations
%        stats(2) = number of linear steps
%        stats(3) = number of linear reversals
%        stats(4) = number of linear searches
%
% Bob Newell, April 1996
%
%-----------------------------------------------------------
%
function xend = linesrch( func, xstart, istep, ...
                            fplot, finc, fdec, minstep )
%
global stats
%
%  default parameters
%
if nargin < 6,
  finc = 1;
  fdec = 1;
end;
if nargin < 8,
  minstep = abs( istep ) / 1000;
end;
if isempty(stats),	  
  stats = zeros( 1, 8 );
end;
%
% evaluate the starting point
%
xold = xstart;
fold = feval( func, xold );
stats(1) = stats(1) + 1;
fconv = abs( fold ) / 1000;
step = istep;
%
% start axes searches
%
n = length( xstart );
while 1,
  for i = 1:n,
%
% set up search direction
%
    dir = zeros( size( xstart ) );
    dir(i) = 1;
%
% do a linear search
%
    xnew = search( func, xold, dir, step, ...
                        fplot, finc, fdec, minstep );
%	[ dir xnew ]
%
% keep going in each direction
%
    xold = xnew;
  end;
  stats(4) = stats(4) + 1;
%
% now check for convergence
%
  fnew = feval( func, xnew );
  stats(1) = stats(1) + 1;
%  [ fold fnew fconv ]
  if abs( fnew - fold ) < fconv,
    xend = xnew;
	return;
  else,
    fold = fnew;
    step = step * fdec;
  end;
end;
%----------------------------------------------------------
%
