function [solution,numiters] = NewtonMatlab(x0,tol,maxit)

DEF_MAX_ITERS = 1000;
DEF_TOL = 1e-6;

switch nargin
    case 1
        tol = DEF_TOL;
        maxit = DEF_MAX_ITERS;
        disp('Had to use 2 default values')
    case 2
        maxit = DEF_MAX_ITERS;
        disp('Had to use 1 default values')
    case 0
        x0 = 0;
        tol = DEF_TOL;
        maxit = DEF_MAX_ITERS;
        disp('Had to use 3 default values')        
    otherwise
        disp('Ignoring all arguments after the 3rd')
end

printwelcome();

iterations = 0;

x = x0;
while 1    
    x_old = x;
    
    [fx,dfdx] = calcfanddfdx(x);
    
    x = x - fx/dfdx;
    iterations = iterations + 1;
    difference = abs(x - x_old);
    
    if ((difference < tol) | (iterations == maxit))
      break;
    end
end

numiters = iterations;
solution = x;

function [fx,dfdx] = calcfanddfdx(x)
fx = (exp(-x) - sin(x));
dfdx = (-exp(-x) - cos(x));

function printwelcome()
disp('Welcome to this wonderful program')