% example of an external function routine using the Newton-Raphson method
% for finding one root of a function f(x) for which we know the analytical
% expression of the first derivative f'(x)

% INPUT
% f      function f(x) defined as an anonymous function 
%        (or an external function)
% fp     first derivative f'(x)
% x0     initial guess of the zero
% err    required precision for knowing the function root

% OUTPUT
% xzero  zero of the function
% fzero  value of the function at xzero
% iter   number of iterations required to find the zero with the accuracy
%        err


function [xzero, fzero, iter] = NewtonRaphson( f, fp, x0, err )
i=0;
while abs(f(x0))>2*err
    x1=x0-f(x0)/fp(x0);
    x0=x1;
    i=i+1;
end
xzero=x0;
fzero=f(xzero);
iter=i;
end