#!/usr/bin/octave -q

% ----------------------------------------------------------------------
% J. R. Senning <jonathan.senning@gordon.edu>
% Gordon College
%
% This is designed to contrast the "bad_newton" program.  The function
% f(x) used in this program has a nonzero derivative at the root so
% quadratic convergence is expected.
% ----------------------------------------------------------------------

f  = @(x) x*x - 2.0;
df = @(x) 2*x;

maxIter = 50;
tol = 1e-6;
x0 = 1.0;

fprintf( 'Newton''s method to solve x*x - 2 = 0\n' );
fprintf( 'Answer should be x = %18.16f\n', sqrt( 2.0 ) );

[x, rate] = newton( f, df, x0, tol, maxIter, true );

fprintf( 'Root = %18.16f\n', x );
fprintf( 'Estimated convergence rate = %5.2f\n', rate );
