% bisection1.m
% Bisection method
% function: f(x)=x^2-4*sin(x)
% starting interval: [a,b]=[1,3]
% tolerance: tol=1e-6
% K. Ming Leung, 02/01/03
% This version uses inline function definition
clear all; format long;
fcn = inline('x.*x-4*sin(x)'); % define function inline
a=1; b=3; tol=1e-6; %disp(log2((b-a)/tol));
fa=a*a-4*sin(a);
fa=feval(fcn,a);
result=[]; loop=0;
while abs(b-a)>tol
m =a+(b-a)/2;
fm=m*m-4*sin(m);
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
display(result);