public class Simpson extends Object implements IterativeIntegrator
Trapezoidal rule.
Simpson's rule is often an accurate integration rule.
Simpson's is expected to improve on the trapezoidal rule for functions which are twice continuously differentiable.
However for rougher functions the trapezoidal rule is likely to be more preferable.
Supply a function that can be evaluated at the finite interval's endpoints
and interior grid points. precision compares successive integral
estimates by relative error; maxIterations limits grid refinement,
not the number of function evaluations. Refinement increases evaluation
cost quickly. The returned estimate does not include an error certificate;
compare refinements for sensitive calculations. The integrator retains
iteration state, so use separate instances for concurrent calculations.
import dev.nm.analysis.function.rn2r1.univariate.AbstractUnivariateRealFunction;
import dev.nm.analysis.integration.univariate.riemann.newtoncotes.Simpson;
AbstractUnivariateRealFunction square = new AbstractUnivariateRealFunction() {
public double evaluate(double x) { return x * x; }
};
double integral = new Simpson(1e-10, 10).integrate(square, 0.0, 1.0);
assert Math.abs(integral - 1.0 / 3.0) < 1e-10;
| Constructor and Description |
|---|
Simpson(double precision,
int maxIterations)
Construct an integrator that implements Simpson's rule.
|
| Modifier and Type | Method and Description |
|---|---|
int |
getMaxIterations()
Get the maximum number of iterations for this iterative procedure.
|
double |
getPrecision()
Get the convergence threshold.
|
double |
h()
Get the discretization size for the current iteration.
|
double |
integrate(UnivariateRealFunction f,
double a,
double b)
Integrate function f from a to b,
\[
\int_a^b\!
|
double |
next(int iteration,
UnivariateRealFunction f,
double a,
double b,
double sum)
Compute a refined sum for the integral.
|
public Simpson(double precision,
int maxIterations)
precision - the convergence thresholdmaxIterations - the maximum number of iterationspublic double integrate(UnivariateRealFunction f, double a, double b)
Integratorintegrate in interface Integratorf - a univariate functiona - the lower limitb - the upper limitpublic double next(int iteration,
UnivariateRealFunction f,
double a,
double b,
double sum)
IterativeIntegratornext in interface IterativeIntegratoriteration - the index/count for the current iteration, counting from 1f - the integranda - the lower limitb - the upper limitsum - the last sumpublic double h()
IterativeIntegratorh in interface IterativeIntegratorpublic int getMaxIterations()
IterativeIntegratorgetMaxIterations in interface IterativeIntegratorpublic double getPrecision()
IntegratorIterativeIntegrator,
the integral is considered converged if the relative error of two successive sums is less than the threshold.getPrecision in interface IntegratorCopyright © 2010-2026 NM Group. All Rights Reserved.