net.sf.doodleproject.numerics4j.integration
Class TrapezoidalIntegrator

java.lang.Object
  extended by net.sf.doodleproject.numerics4j.IterativeMethod
      extended by net.sf.doodleproject.numerics4j.integration.TrapezoidalIntegrator

public class TrapezoidalIntegrator
extends IterativeMethod

The extended trapezoidal rule for numerically integrating functions.

For example, to evaluate definite integrals for sine, first a Function is defined:

 Function sine = new Function() {
    public double evaluate(double x) {
        return Math.sin(x);
    }}
 };
 

Then, a trapezoidal integrator is created with the above function:

 TrapezoidalIntegrator integrator = new TrapezoidalIntegrator(sine);
 

Lastly, evaluating definite integrals is accomplished using the integrate(double, double) method:

 // integrate sine from 0 to Pi.
 double two = integrator.integrate(0.0, Math.PI);
 
 // integrate sine from Pi/2 to 2 Pi.
 double one = integrator.integrate(Math.PI / 2.0, Math.PI);
 

References:

  1. Eric W. Weisstein. "Newton-Cotes Formulas." From MathWorld--A Wolfram Web Resource. http://mathworld.wolfram.com/Newton-CotesFormulas.html
  2. Eric W. Weisstein. "Trapezoidal Rule." From MathWorld--A Wolfram Web Resource. http://mathworld.wolfram.com/TrapezoidalRule.html

Since:
1.1
Version:
$Revision: 1.2 $ $Date: 2007/10/25 04:44:16 $

Nested Class Summary
(package private) static class TrapezoidalIntegrator.IterativeState
          The internal state used during root finding.
 
Constructor Summary
TrapezoidalIntegrator(Function f)
          Create an integrator for the given function.
TrapezoidalIntegrator(Function f, int iterations, double error)
          Create an integrator for the given function.
 
Method Summary
 Function getFunction()
          Access the target function.
 double integrate(double a, double b)
          Evaluate the definite integral from a to b.
 void setFunction(Function f)
          Modify the target function.
 
Methods inherited from class net.sf.doodleproject.numerics4j.IterativeMethod
getMaximumIterations, getMaximumRelativeError, iterate, setMaximumIterations, setMaximumRelativeError
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

TrapezoidalIntegrator

public TrapezoidalIntegrator(Function f)
Create an integrator for the given function.

Parameters:
f - the target function.

TrapezoidalIntegrator

public TrapezoidalIntegrator(Function f,
                             int iterations,
                             double error)
Create an integrator for the given function.

Parameters:
f - the target function.
iterations - maximum number of iterations.
error - maximum relative error.
Method Detail

getFunction

public Function getFunction()
Access the target function.

Returns:
the target function.

integrate

public double integrate(double a,
                        double b)
                 throws NumericException
Evaluate the definite integral from a to b.

Parameters:
a - the lower limit of integration.
b - the upper limit of integration.
Returns:
the definite integral from a to b.
Throws:
NumericException - if the integral can not be evaluated.

setFunction

public void setFunction(Function f)
Modify the target function.

Parameters:
f - the new target function.


Copyright 2004-2004-2007 DoodleProject. All Rights Reserved.