#!/usr/bin/octave -q

% ----------------------------------------------------------------------
% J. R. Senning <jonathan.senning@gordon.edu>
% Gordon College
%
% This is designed to show how Newton's method experiences only linear
% convergence if the derivative of f(x) is zero at the root of f(x).
% ----------------------------------------------------------------------

f  = @(x) (x + 2)*x + 1;
df = @(x) 2*(x + 1);

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

fprintf( 'Newton''s method to solve x*x + 2*x + 1 = 0\n' );
fprintf( 'Answer should be x = -1\n' );

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

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