% Root finding using bisection method
% function:             fcn (string)
% INPUT:
% starting interval:    given by a and b
% tolerance:            tol
% bisectionF.m
% OUTPUT:
% result = [loop a fa fm]
% K. Ming Leung, 02/01/03
% This version uses inline function definition

clear all; format short e;
fcn = inline('x.*x-4*sin(x)');  % define function inline
%fcn = inline('x.*(x.*x-2)-5');
a=2; b=3; tol=1e-6; %disp(log2((b-a)/tol));
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 b fa fm]];
end
display(result);



