Home : Map : Chapter 3 : Java : Tech : Physics :
Demo: Newton Method
JavaTech
Course Map
Chapter 3

Introduction
Class Definition
Data Fields
Methods
Constructors
Instantiation
  
Demo 1
  Demo 2
Static Members
  Demo 3

Value&Reference
  Demo 4
Overloading
   Demo 5
Wrappers 
  Demo 6
Autobox/Unbox
   Demo 7
Arrays
  Demo 8
Exceptions
Exercises

    Supplements
MoreAboutObjects
Creating Types

Classes,Objs&JVM

Java OOP vs C++
Garbage Collection
     About JavaTech
     Codes List
     Exercises
     Feedback
     References
     Resources
     Tips
     Topic Index
     Course Guide
     What's New

This demo applies the Newton method to the projectile intercept problem used earlier with the Bisection method. It illustrates that we must use some prior knowledge of the function for the method to work correctly.

Since Newton's method relies on the derivative, we must make sure that we are in the region of the ballistic parabola where the projectile is heading downward. Otherwise, it will head back towards the cannon and find the root at the point where the parabola started.

So the code below shows that we start by making big steps in the postive direction until we are sure that we are past the point where the curve turns downward. (A smarter way of doing this is quite possible!) Then we start the Newton algorithm. This code also includes a second method with the derivative of the function.

Newton_Applet5.java
(Output goes to browser's Java console.)

public class Newton_Applet5 extends java.applet.Applet
{
 // Put property data between these lines
 //------------------------------------------------
 // Data for a ballistic trajectory at a given angle

 double tan = Math.tan(Math.PI / 4.0);
 double cos = Math.cos(Math.PI / 4.0);
 double g = 9.8; // m/s^2
 double v0 = 100; // m/s
 //------------------------------------------------

 public void init()
 {   
   // Put code between this line
   //--------------------------------------------------

     double double xl = 0.0;
    
double xh = 1.0;
    
double step = 1.0;
    
double dif = 1.0;

     // Constants
    
double maxSteps = 50000;
    
double tol = 1.0E-2;

     int numSteps = 0;
     int numStepsNewton = 0;

     // First look for the "turn over" of the parabola
     while (f_method(xl) * f_method(xh) < 0 )
     {
         xl += step;
         xh += step;
         numSteps++;
         // Give up if gone too far
         if( numSteps > maxSteps)
         {
           System.out.println("No turn over found");
           return;
         }

     }

     // Now that we are on the "down" side,
     // use Newton's method to find the intercept.

     xl = xh;
     do
     {
       double deriv = fp_method(xl);
       if( deriv == 0.0)
       {
         System.out.println("Derivative = 0.0");
         return;
       }
       xh = xl - (f_method(xl)/deriv);// Newton formula
       dif = Math.abs(xh-xl);
       xl = xh;

       numStepsNewton++;
     } while ( dif > tol && numStepsNewton < maxSteps);

     System.out.println("After steps ="+numSteps);
     System.out.println(" & Newton steps = "+numStepsNewton);
     System.out.println("Root x = "+xm+
                        " within tolerance ="+dif);

    //--------------------------------------------------
 
  // and this line.
  }

  double f_method(double x)
  {
   // Put code between this line
   //--------------------------------------------------

     double val = x*tan - (0.5*g*x*x)/(v0*v0*cos*cos); ;
     return val;

   //--------------------------------------------------
 
 // and this line.
  }

  double fp_method(double x)
  {
   // Put code between this line
   //--------------------------------------------------

     double val = tan - (g*x)/(v0*v0*cos*cos); ;
     return val;

   //--------------------------------------------------
 
 // and this line.
  }

  // Paint message in Applet window.
  public void paint(java.awt.Graphics g)
  {
g.drawString("Newton_Applet5",20,20); }

  // Can also run this program as an application.
  // No windowing needed so just run the applets
  // init() function.

  public static void main(String [] args)
  {
    Newton_Applet5 obj = new Newton_Applet5();
    obj.init();
  }

}

 

Once the Newton method starts, it finds the root in only a few steps (compare the results to the bisection method):

After steps = 51
  & Newton steps = 13
Root x = 1020.4081632734085 within tolerance = 0.0028753744192

 

Latest update: Dec.15.2003

           Tech
OOP in Tech Apps
Complex Number
Histogram Class
  Demo
More Wrappers

           Physics
OOP in Physics
Particle Class
Root Finding
  Demo 1
Newton Methods
  Demo 2
Exercises

  Part I Part II Part III
Java Core 1  2  3  4  5  6  7  8  9  10  11  12 13 14 15 16 17
18 19 20
21
22 23 24
Supplements

1  2  3  4  5  6  7  8  9  10  11  12

Tech 1  2  3  4  5  6  7  8  9  10  11  12
Physics 1  2  3  4  5  6  7  8  9  10  11  12

Java is a trademark of Sun Microsystems, Inc.