% Bisection Method to solve f(x) = 0.
clear all

a = 1.25; b = 1.5; m=100;

x = linspace(a,b,m);
y = myfunction(x);
hold on
plot(x,y,'-');

if myfunction(a)*myfunction(b) >= 0
    error('f(a) and f(b) must have different signs!');
end

TOL = 1.d-3; N0 = 20; eps = 1.d-10;

%step 1:
i=1; FA = myfunction(a);

%step 2:
while i<=N0
    %step 3:
    pause
    p = a + (b-a)/2;
    plot(p,myfunction(p),'rx');
    fprintf('iteration %d, p= %10.8f\n',i,p);
    FP = myfunction(p);
    %step 4:
    if ( abs(FP) < eps ) || ( (b-a)/2 < TOL )
        fprintf('solution is %10.8f\n',p);
        return;
    end
    %step 5:
    i = i+1;
    %step 6:
    if FA*FP > 0
        a = p;
        FA = FP;
    else
        b = p;
    end
end
%step 7:
fprintf('Method failed after %d iterations.\n',N0);
return
