# SuanShu, a Java numerical and statistical library

com.numericalmethod.suanshu.analysis.integration.univariate.riemann.newtoncotes

## Class NewtonCotes

• java.lang.Object
• com.numericalmethod.suanshu.analysis.integration.univariate.riemann.newtoncotes.NewtonCotes
• All Implemented Interfaces:
Integrator, IterativeIntegrator
Direct Known Subclasses:
Midpoint, Trapezoidal

public class NewtonCotes
extends Object
implements IterativeIntegrator
The Newton-Cotes formulae, also called the Newton-Cotes quadrature rules or simply Newton-Cotes rules, are a group of formulae for numerical integration (also called quadrature) based on evaluating the integrand at equally-spaced points. A number of standard numerical quadrature methods are special cases of the more general Newton-Cotes formula, e.g., the Trapezoidal rule (rate = 2), the Midpoint method (rate = 2 and specifying OPEN formula).

This implementation is based on the Euler-Maclaurin formula.

• ### Nested Class Summary

Nested Classes
Modifier and Type Class and Description
static class  NewtonCotes.Type
There are two types of the Newton-Cotes method: OPEN and CLOSED.
• ### Constructor Summary

Constructors
Constructor and Description
NewtonCotes(int rate, NewtonCotes.Type type, double precision, int maxIterations)
Construct an instance of the Newton-Cotes quadrature.
• ### Method Summary

All Methods
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\! f(x)\, dx$
double next(int iter, UnivariateRealFunction f, double a, double b, double sum0)
Compute a refined sum for the integral.
• ### Methods inherited from class java.lang.Object

clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
• ### Constructor Detail

• #### NewtonCotes

public NewtonCotes(int rate,
NewtonCotes.Type type,
double precision,
int maxIterations)
Construct an instance of the Newton-Cotes quadrature.
Parameters:
rate - the rate of further sub-dividing an integral interval. For example, when rate = 2, we divide [xi, xi+1] into two equal length intervals. This is equivalent to the Trapezoidal rule.
type - specifying whether to use CLOSED or OPEN formula
precision - the precision required, e.g., 1e-8
maxIterations - the maximum number of iterations
• ### Method Detail

• #### integrate

public double integrate(UnivariateRealFunction f,
double a,
double b)
Description copied from interface: Integrator
Integrate function f from a to b, $\int_a^b\! f(x)\, dx$
Specified by:
integrate in interface Integrator
Parameters:
f - a univariate function
a - the lower limit
b - the upper limit
Returns:
$$\int_a^b\! f(x)\, dx$$
• #### next

public double next(int iter,
UnivariateRealFunction f,
double a,
double b,
double sum0)
Description copied from interface: IterativeIntegrator
Compute a refined sum for the integral.
Specified by:
next in interface IterativeIntegrator
Parameters:
iter - the index/count for the current iteration, counting from 1
f - the integrand
a - the lower limit
b - the upper limit
sum0 - the last sum
Returns:
a refined sum
• #### h

public double h()
Description copied from interface: IterativeIntegrator
Get the discretization size for the current iteration.
Specified by:
h in interface IterativeIntegrator
Returns:
the discretization size
• #### getMaxIterations

public int getMaxIterations()
Description copied from interface: IterativeIntegrator
Get the maximum number of iterations for this iterative procedure. For those integrals that do not converge, we need to put a bound on the number of iterations to avoid infinite looping.
Specified by:
getMaxIterations in interface IterativeIntegrator
Returns:
the maximum number of iterations
• #### getPrecision

public double getPrecision()
Description copied from interface: Integrator
Get the convergence threshold. The usage depends on the specific integrator. For example, for an IterativeIntegrator, the integral is considered converged if the relative error of two successive sums is less than the threshold.
Specified by:
getPrecision in interface Integrator
Returns:
the precision