% bisection.m
% Bisection method
% function: fcn must be a string (in single quotes)
% starting interval: [a,b]=[1,3]
% tolerance: tol
% K. Ming Leung, 02/03/03
clear all; format long;
a=1; b=3; tol=1e-3; fcn='cubeRoot2';
fa=feval(fcn,a); result=[]; loop=0;
while abs(b-a)>tol
m = a+(b-a)/2; fm=feval(fcn,m);
if sign(fa)==sign(fm)
a=m; fa=fm;
else b=m;
end;
loop=loop+1;
result=[result ;[loop a fa fm]];
end
disp(' loop a f(a) f(m)');
disp(result)