Home : Map : Chapter 5 : Java : Tech : Physics :
Demo 2: Simpson's Rule Integration
JavaTech
Course Map
Chapter 5
File/Package/Import
  Demo 1
  Demo 2
Access/Visibility
final & Constants
Static Import
Jar Files
  Demo 3
Applet Directories
3rd Party Packages
CLASSPATH
javadoc
CodingConventions
Exercises

    Supplements
Scope
Debug Techniques
Java Runtime
Class Class
JVM Instructions 2
JVM Processing
pack200

     About JavaTech
     Codes List
     Exercises
     Feedback
     References
     Resources
     Tips
     Topic Index
     Course Guide
     What's New

We will use the same Integrate interface used in the previous demo to create class to carry out the Simpson's rule integration.

SimpsonInteg.java

/** Integrate a 1-D function using the Simpson Rule. **/
class SimpsonInteg implements Integrator
{
  /** Integrate the function between lo and hi with n slices. **/
  public double integrate (double lo, double hi,
                            int n, Function f)  {
    // Check data.
    if ( hi <= lo || n <= 0 || f == null) return 0.0;

    double dx =  (hi-lo)/n;
    dx = dx/2.0; // Use half widths for Simpson rule
    double area = 0.0;

    double x_lo = lo;
    double x_md = lo + dx;
    double x_hi = lo + 2.0 * dx;

    for (int i=0; i < n; i++) {
      double left_side = f.valueAt (x_lo);
      double middle    = f.valueAt (x_md);
      double rite_side = f.valueAt (x_hi);
      area += dx * (left_side + 4.0 * middle + rite_side)/3.0;
      x_lo = lo + 2.0 * i * dx;
      x_md = x_lo + dx;
      x_hi = x_lo + 2.0 * dx;
    }

    return area;
  } // integrate

} // SimpsonInteg

 

Then we use the following applet to calculate the integral of a function and compare the value to that obtained from the other two methods:

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

/**
  * This class illustrates use of Simpson's Rule for integration.
  * Compares to Rectangular and Trapezoidal methods.
  *
  *
  *   Integrator.java - interface
  *     SimpsonInteg.java   - implements Integrator
  *     TravezoidInteg.java - implements Integrator
  *     RectInteg.java      - implements Integrator
  *
  *   Function.java - interface
  *     PolyFunc.class - see below
  *
 **/
public class SimpsonApplet extends java.applet.Applet
{
  public void init () {

    SineOverX2Func sinf = new SineOverX2Func ();

    RectInteg ri = new RectInteg ();
    double integ = ri.integrate (0.01, Math.PI, 1000, sinf);
    System.out.println ("Rectangle integ = " + integ);

    TrapezoidInteg ti = new TrapezoidInteg ();
    integ = ti.integrate (0.01, Math.PI, 1000, sinf);
    System.out.println ("Trapezoid integ = " + integ);


    SimpsonInteg si = new SimpsonInteg ();
    integ = si.integrate (0.01,Math.PI,1000,sinf);
    System.out.println ("Simpson integ = " + integ);

  } // init

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

  /** This program can also run as an application. No graphics needed.
    * so it just runs the applet's init () function.
   **/
  public static void main (String [] args) {
    SimpsonApplet obj = new SimpsonApplet ();
    obj.init ();
  } // main

} // SimpsonApplet

/** An example 1-D function. **/
class SineOverX2Func implements Function
{
   public double valueAt (double x) {
     return Math.sin (x)/ (x*x);
   }
} // SineOverX2Func

 

The output shows:

Rectangle integ = 5.579456553713102
Trapezoid integ = 5.3855384529691195
Simpson integ = 5.374091546132844

 

Exercise: Try larger numbers of slices to find if the three methods agreement improves.

 

Most recent update: Oct. 21, 2005

            Tech
DecimalFormat
  Demo 1
  Demo 2

System.out.printf
  Demo 3
CJ Format Class
  Demo 4   Demo 5
Other Format Tools

Immutable Complex
Exercises

           Physics
Interpolation
Integration
  Demo 1
Simpson Rule
  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.