function [a0, xk, yk, it] = linesch(x0, p, a0, C, y0, df0)
%LINESCH    line search with backtrack and sufficient decrease
%
%   [ak, xk, yk, it] = linesearch(x0, p, a0, C, y0, df0)
%
%       x0, y0: initial x and y=f(x)
%          df0: f'(x0)
%            p: descent direction
%            C: 0 < C < 1 is the constant in suff. decrease.
%           a0: initial step length
%           ak: final step length
%           it: the times that f is evaluated.
%
%   The suff. decrease condition is
%           f(x0+ak p) < f(x0) + C ak p^T f'(x0)

%   Ling Li
%   04/16/2001, Caltech

% Get f'(x0)
if nargin < 6,
    df0 = objective(x0, 1);
end
if nargin < 5,
    y0 = objective(x0, 0);
end
if nargin < 4,
    C = 0.5;    % just set a value for C
end

Cpdf0 = C * p' * df0;
if (Cpdf0 > 0)
    error('p is not a descent direction');
end

it = 0;
while 1,
    % use backtrack
    xk = x0 + a0 * p;
    yk = objective(xk, 0);
    it = it + 1;
    if yk <= y0 + a0 * Cpdf0,
        return;
    end
    a0 = a0 * 0.5;
end
