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

    # 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 * x - 2.0
    def df(x): return 2 * x
    x0 = 1.0

    print "Newton's method to solve x*x - 2 = 0"
    print "Answer should be x = %f\n" % sqrt( 2.0 )

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

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