function hybrid_method()
 
c = 0.05;
b = 2;
a = 3;
fa=f(a);
fb=f(b);
fc=f(c);
if sign(fb) == sign (fc)
    fprintf('The root is not bracketed.\n');
    exit;
end
% b,c bracket the root
% a == x_{n-1}, b == x_n
%
tol=1.0e-14; %tolerance
%
% figure set up
x=linspace(c, b, 200);
figure;
plot(x,f(x),'LineWidth',2);
hold on;
grid
plot(a,fa,'r.','Markersize',30);
plot(b,fb,'b.','Markersize',30);
plot(c,fc,'k.','Markersize',30);

niter=0; % n=0
% print initial a, b, c 
fprintf('iter #%d: %.14f\t bracket:[%.14f\t%.14f]\n',niter,a,b,c);
% print tolerance 
fprintf('tol = %.14f\n',tol);
%% start iterations

while 1 %intinite cycle as 1 == true
    if abs(fc) < abs(fb) %swap b and c and set a = c
        t=c; c=b; b=t; 
        t=fc; fc=fb; fb=t;
        a=c; fa = fc; 
    end
    if abs(b-c) <= tol % break cycle if bracket size is less than tol
        fprintf('iter #%d: %.14f\t bracket:[%.14f\t%.14f]\t delta = %.14f\n',...
            niter,a,b,c,abs(b-c));
        break;
    end

    dm=0.5*(c-b); %  0.5*(b + c) == b + dm
    df=fa-fb; % change in f
    if df == 0  % If fa=fb slope is zero. Then
        ds=dm;
    else
        ds=-fb*(a-b)/df;  % secant method step
    end
    if sign(ds) ~= sign(dm) | abs(ds) > abs(dm) % if the secant step 
        % is in wrong direction or  too long
        dd=dm; % accept the midpoint as the next approximation
        flag='m'; % flag indicates which methos is used 
    else 
        dd=ds; % accept the secant step as the next approximation  
        flag='s';
    end
    if abs(dd) < tol % if the step is too small
        dd=0.5*sign(dm)*tol;
        flag='a';
    end
    d=b+dd;
    fd=f(d);
    niter=niter+1;
    if flag == 'm'
        plot(d,fd,'m','Markersize',30);
    else if flag == 's'
        plot([a b d],[fa fb fd],'LineWidth',1,'color','r');
        end
    end
    if fd == 0 % we got the exact root 
        b=d; c=d; fb=fd; fc=fd;
        fprintf('iter #%d: %.14f\t bracket:[%.14f\t%.14f]\t flag = %c\n',niter,a,b,c,flag);
        break;
    end
    a=b; b=d; % move a to b and b to d
    fa=fb; fb=fd;
    if sign(fb) == sign(fc)  % id fb and fc are of the same sign set c = a
        c=a; fc=fa;
    end
    plot(a,fa,'r.','Markersize',30);
    plot(b,fb,'b.','Markersize',30);
    plot(c,fc,'k.','Markersize',30);
    fprintf('iter #%d: %.14f\t bracket:[%.14f\t%.14f]\t  flag = %c\n',niter,a,b,c,flag);
end
end
%%
function y=f(x)
y=-1+0.1./x;
end
%

