format long

%  set f to be a (mathematical) function
% (not the same as a MATLAB function...) 
f = @(x) exp(x)-4*x;  
% plot the function
fplot( f, [0,1] )
% MATLAB trick that adds a zero line to the graph...
hold on
fplot( @(x) zeros(size(x)), [0,1] )
hold off

% now we aim to solve exp(x)-4x == 0
% to 6 decimal places
zeta = 1e-7; 
a = 0.0; b = 1; 

while abs(b - a)/2 > zeta
   c = (a+b)/2; 
   if f(b)*f(c) > 0
        b = c; 
   else
        a = c;
   end
end

estRoot = (a+b)/2

% check that f(estRoot) is indeed small
fAtEstRoot = f(estRoot) 

% use built-in MATLAB solver to check
matlabRoot = fzero( f, 0.0 )
diff = estRoot - matlabRoot
