% EX1  Root finding
%
% Script that finds the root of a function f(x) with several
% approximation methods:
%
%   1) Bisection-method
%   2) Newton-Raphson-methode (requires the first derative of f(x))
%   3) Secant-method
%   4) Matlab's fzero (extended bisection method)
%
% Michiel Geurts 26 Feb 96 (Fortran version)
% Tom Heskes 08 Aug 2002 (this Matlab version)

fname = 'fff';    % function name
tol = 1e-7;       % tolerance (absolute for bisection, relative for others)
precision = 15;   % decimals for displaying

lower = 0;  % interval
upper = 1;
root1 = bisect(fname,[lower, upper], tol);
disp(['Root found with bisection method at ', num2str(root1, precision)]);

x0 = 0;  % starting value
root2 = newton(fname, x0, tol);
disp(['Root found with Newton-Raphson at ', num2str(root2, precision)]);

x1 = 0;  % two starting values
x2 = 0.1;
root3 = secant(fname, [x1,x2], tol);
disp(['Root found with secant method at ', num2str(root3, precision)]);

x0 = 0;  % starting value
options = optimset('display', 'off', 'TolX', tol);
root4 = fzero(fname, x0, options);
disp(['Root found with Matlab''s fzero at ', num2str(root4, precision)]);
