% Newton iteration (quadratic convergence)
% See example 5.10 on p.230 of Heath
% K. Ming Leung, 02/13/03

clear all; format long;
tol=eps;    % set tolerance to machine epsilon
x=3;        % initial guess for the root
dx=1;       % change in x
result=[];  % to store all intermediate results
loop=1;     % loop count
while abs(dx)> tol;
    fx=x*x-4*sin(x);    % function of interest
    fp=2*x-4*cos(x);    % derivative of the function
    dx=-fx/fp;          % change in x
    result=[result; [loop x fx fp dx]];
    x=x+dx;             % the next value of x
    loop=loop+1;        % increment loop counter
end;
display(result);        % display final result


