%   hybrid9a.m -- Globalized Iterative Method
%   Written Sep 14, 2005 by Eric Olson for Mathematics 466
%
%   Solve f(x)=0 by iterating x=phi(x) while maintaining the 
%   bracket [a,b].  If phi(x) lands outside the current bracket, 
%   then take a bisection step.
%
%   x=hybrid9a(f,phi,a,b,erel)
%
%   For more information see the lecture notes at
%   http://fractal.math.unr.edu/~ejolson/466/notes/lect09.html

function x=hybrid9a(f,phi,a,b,erel)
    fa=feval(f,a);
    fb=feval(f,b);
    if fa*fb>=0 || b<=a
        disp('Not a good initial bracket!');
    end
    c=(a+b)/2;
    for n=1:3000
        fc=feval(f,c);
        if fc==0.0
            x=c;
            return
        end
        if fc*fa>0
            a=c;
        else
            b=c;
        end
        x=c;
        c=feval(phi,x);
        if c<a || c>b
            c=(a+b)/2;
        end
        if abs(c-x)<=erel*c
            break
        end
    end
    x=c;
