% Newton's method for scalar equations

x = 2.0;       % startgissning
TOL = 1.0e-14; % feltolerans

d = 1.0;       %nu vad som helst större än TOl, senare inkrement i x
fprintf('      x               f(x)\n');
%disp('x, f(x)')
while(abs(d)>TOL)
    %f  = exp(-x) - x;
    %df = -exp(-x) - 1;
    f=x*x-2;
    df=2*x;
    %f = x*x;
    %df = 2*x;
    
    d = -f/df;
    x = x + d;
    
    fprintf('%.10f     %.15f\n', x,f);
    %disp([x,f])
end