<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">#!/usr/bin/env python

from math import *
from findroot import newton

#------------------------------------------------------------------------------
#------------------------------------------------------------------------------
#------------------------------------------------------------------------------

if __name__ == "__main__":
    """
    J. R. Senning &lt;jonathan.senning@gordon.edu&gt;
    Gordon College
    Python Version: May 22, 2008

    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).
    """

    # Maximum number of iterations

    maxiter = 50

    # Tolerance for all algorithms.  This value determines how accurate
    # the final estimate of the root will be.

    tol = 1e-5

    # ---------- Define f(x), df(x) and the interval to search -------------

    def  f(x): return ( x + 2 ) * x + 1
    def df(x): return 2 * ( x + 1 )
    x0 = 0.0

    print "Newton's method to solve x*x + 2*x + 1 = 0"
    print "Answer should be x = -1"

    x, rate = newton( f, df, x0, tol, maxiter )

    print "Root = ", x
    print "Estimated convergence rate = %5.2f" % rate
</pre></body></html>