Bisection 2

%bisection
%Author: Minster
%this program uses bisection to determine the root of an algorithms. The
%sign of F(a) and f(b) must be different

%f(x) = x^2 -1

f= inline(‘exp(-x)+1-2*x’);
a=0.5;
b=3;
%N=10; %number of iterations
delta = 0.001;
fa=f(a);
fb=f(b);
ncount = 0;

%for k=1:n
while abs(b-a) > 2*delta
c=(a+b)/2;
fc=f(c);
if sign(fc)== sign(fa),
a=c;
fa=fc;
else b=c;
fb=fc;
end
ncount = ncount +1;
end
a
b
root = (a+b)/2
froot = f(root)
ncount