function [a,f] = brent(func,x0,dx,a1,a,a2,f1,f,f2,varargin)
% one-dimensional minimization by parabolic interpolation & golden
% section (does not use the gradient)
%
% [a,f] = brent(func,x0,dx,a1,a,a2,f1,f,f2,varargin)
%
% func = string name of objective function
% x0 = starting point of linesearch
% dx = direction of linesearch
% a1,a,a2 = bracketing triplet of steplengths (a1a)|(a2tol1 % construct a trial parabolic fit
r = (a-w)*(f-fv);
q = (a-v)*(f-fw);
p = (a-v)*q-(a-w)*r;
q = 2*(q-r);
if q>0, p = -p; end
q = abs(q);
etemp = e;
e = d;
% check acceptability of parabolic fit
ok = ~(abs(p)>=abs(0.5*q*etemp) | p<=q*(a1-a) | p>=q*(a2-a));
if ok % take parabolic step
d = p/q;
u = a+d;
if (u-a1)=am
e = a1-a;
else
e = a2-a;
end
d = gold*e;
end
else % take golden section step
if a>=am
e = a1-a;
else
e = a2-a;
end
d = gold*e;
end
% arrive here with d computed either from
% parabolic fit or else from golden section
if abs(d)>=tol1
u = a+d;
else
u = a+sign(d)*tol1;
end
fu = feval(func,x0+u*dx,varargin{:}); % one function evaluation per iteration
if fu<=f
if u>=a
a1 = a;
else
a2 = a;
end
v = w; fv = fw;
w = a; fw = f;
a = u; f = fu;
else
if u