function [ root ] = newton(f,df,guess)
%NEWTON newton(f,df,guess) computes a root of f using Newton's method

% Example usage:
%   newton(@(x) x.^2 - 10, @(x) 2.*x, 3)
% (which solves x^2 = 10, resulting in sqrt(10).
%

    % define the function g to iterate
    g = @(x) x - f(x)./df(x);
    
    n = 0;  % this will count (and limit) the number of iterations
    
    oldx = guess;
    x = g(oldx);
    
    % loop until max # of steps, or a fixed point is reached 
    while n < 100 && abs(x - oldx) > eps(x)
        n = n + 1;
        oldx = x;
        x = g(oldx);
        fprintf('%d %.20g\n',n,x);  % display n and x_n
    end
    
    root = x;
end

