% bisection_q13.m
% Bisection code
a = 1;
b = 2;
TOL = 1e-4;
f = inline('x^3 - x - 1', 'x');
FA = f(a);
n = 1;
while(1)
n
a
b
p = a + (b-a)/2
FP = f(p)
disp('----------');
if (FP == 0) | ((b-a)/2 < TOL)
disp('Success: error is less than tolerance');
break
end
if (FA*FP > 0)
a = p;
FA = FP;
else
b = p;
end
n = n + 1;
end
Copyright (C) 2009 Konstantin Kirillov