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