function [x, e] = solutions_hw7_p2(f, x0)
%SOLUTIONS HW7 P2 Summary of this function goes here
%   Detailed explanation goes here

    tol = 1e-6;
    
    e = abs(f(x0));
    x = x0;
    iter = 0;
    while abs(e(end))>tol && iter<1000
        fp = der(f, x);
        x = x - f(x)/fp;
        e(end+1) = abs(f(x));
        iter = iter+1;
    end
    
    if iter>=1000
        x = NaN;
    end
    
end

function [fp] = der(f, x)
    h = 1e-6;
    
    fp = (f(x+h)-f(x-h))./(2.0*h);
    
end

