Newton.java


Below is the syntax highlighted version of Newton.java from §3.6 Case Study: Purple America.



/*************************************************************************
 *  Compilation:  javac Newton.java
 *  Execution:    java Newton x
 *
 *************************************************************************/


public class Newton {

    // find root x such that f(x) = 0 using Newton's method, starting at x0
    public static double findRoot(DifferentiableFunction f, double x0) {
        double eps = 1e-15;
        double x = x0;
        double delta = f.eval(x) / f.diff(x);
        while (Math.abs(delta) > (eps / x)) {
            x = x - delta;
           delta = f.eval(x) / f.diff(x);
StdOut.println(x);
        }
        return x;
    }

   // sample client program
   public static void main(String[] args) { 
      DifferentiableFunction f = new Sqrt(2);
      StdOut.println(findRoot(f, 2.0));
   }

}


Copyright © 2007, Robert Sedgewick and Kevin Wayne.
Last updated: Tue Sep 29 16:17:41 EDT 2009.