function [xx,fmin] = bisectionsearch(a,b,c,nstep)
% Bisection search for 1-dimensional function
%   inputs are
%      a,b,c : signature minimum starting values
%      nstep : number of iteration steps
%   outputs are
%      xx    : critical point
%      fmin  : minimum value found
k=0; fa=fx(a); fb=fx(b); fc=fx(c);

% check inputs
if a<b & b<c & fb<=fa & fb<=fc,
fprintf('Bisection search in 1 dimension ...\n\n')
else, error('Oops.. no signature minimum for these input values!'), end
if rem(nstep,2), error('Oops.. number of iterations must be even!'), end

% look at left and right intervals in turn
for step = 1:nstep/2;
k=k+1; d=0.5*(a+b); fd=fx(d); % left-side point
itntable(k,a,b,c,d,fa,fb,fc,fd);
   if fd >= fb, a=d; fa=fd;
   else c=b; fc=fb; b=d; fb=fd; end
k=k+1; e=0.5*(b+c); fe=fx(e); % right-side point
itntable(k,a,b,c,e,fa,fb,fc,fe);
   if fe >= fb, c=e; fc=fe;
   else a=b; fa=fb; b=e; fb=fe; end
end % interval loop
xx=b; fmin=fb;
fprintf('done \n')

function itntable(k,a,b,c,m,fa,fb,fc,fm)
if k==1; % print header
fprintf('  k    a      f(a)   b     f(b)    c     f(c)    m     f(m)\n')
end
fprintf(' %2g  %6.4f %6.4f %6.4f %6.4f %6.4f %6.4f %6.4f %6.4f\n', ...
          k,a,fa,b,fb,c,fc,m,fm), pause(0.1)
return
