The Newton-Cotes formulas, also called as the Newton-Cotes quadrature rules or simply the Newton-Cotes rules, are a group of formulas for numerical integration which are based on evaluating the integrand at equally spaced points. They are named after Isaac Newton and Roger Cotes respectively. Newton-Cotes formulas can be very useful if the values of the integrand at equally spaced points are available. If the set of points are not equally spaced, then other methods such as Gaussian quadrature are probably more accurate. There are many derivations of the Newton-Cotes formulas. The two most famous ones are the trapezoidal quadrature and the Simpson quadrature formulas.

The line equation passing through the two points and is

The area under the line is the area of the trapezoid, which is simply multiplying the sum of the bases which are the parallel lines by the height of the trapezoid, which is the perpendicular distance between the bases, and then divide by 2. We can also do a proper integration to get the same result. Now replacing with , we have

For a function and an interval , we can divide the interval into sub-intervals. Then we apply the trapezoidal rule to each sub-interval and then sum all of then up.

Let

The error form of trapezoidal rule is given by

for some .

The following example solves the problem in the above figure of the trapezoidal formula.

` ````
```val f: final UnivariateRealFunction f = new AbstractUnivariateRealFunction() {
override public double evaluate(double x) {
return -(x * x- 4 * x + 6); // -(x^2 -4x + 6)
}
};
//the limit
double a = 0, b = 4;
// an integrator using the trapezoidal rule
Integrator integrator = new Trapezoidal(1e-8, 20); // precision, max number of iterations
// the integration
double I = integrator.integrate(f, a, b);
System.out.println(String.format("S_[%.0f,%.0f] f(x) x = %f", a, b, I));

The output is:

` ````
```S_[0,4] f(x) dx = -13.333333

## The Simpson Quadrature Formula

The Simpson quadrature formula, instead of replacing the function by a line, replaces by a quadratic function or a parabola. teh interval is divided into two prtitions giving three points: and a mid-point . The quadratic function is given by

From a geometrical point of view, the Simpson formula calculates the area of the curved trapezoid surrounded by a parabola to approximate the area enclosed by .

Replacing with , we have

is the step size in each partition. Because of the in the formula, Simpson’s quadrature formula is also called as Simpson’s 1/3 rule.

For a function and an interval , we can divide the interval into sub-intervals. Then we can apply the Simpson rule to each of the subinterval and then sum them all up. We have

Simson’s rule is expected to improve on the trapezoidal rule for the functions which are twice continuously differentiable. However, for the rougher functions the trapezoidal rule is likely to be more preferable than the Simpson’s rule. The error term for the Simpson’s rule is given by

for some .

` ````
```val f: final UnivariateRealFunction f = new AbstractUnivariateRealFunction() {
override public double evaluate(double x) {
return -(x * x- 4 * x + 6); // -(x^2 -4x + 6)
}
};
//the limit
double a = 0, b = 4;
// an integrator using the Simpson rule
Integrator integrator = new Simpson(1e-8, 20); // precision, max number of iterations
// the integration
double I = integrator.integrate(f, a, b);
System.out.println(String.format("S_[%.0f,%.0f] f(x) x = %f", a, b, I));

The output is:

` ````
```S_[0,4] f(x) dx = -13.333333

## The Newton-Cotes Quadrature Formula

It is possible that we can divide the interval into more than 1 partitions, as in the case of trapezoidal rule or more than 2 partitions, as in the case of the Simpson’s rule. One can divide it into intervals. One can also use a closed formula where the endpoints an are used, or an open formula where the endpoints are not used because and may not exist. The Newton-Cotes formula is such a generalization. It divides the interval into equal partitions. For a closed formula, the equally spaced points are given by

For an open formula, the equally spaced points are

For the closed formula, an n-degree difference polynomial can be constructed,

where

Now replacing with , we have

where the coefficients

The above formula is called the Newton-Cotes formula. The coeffients are called the Newton-Cotes coefficients. To use the formuls, we need to calculate the coefficients . Substituting , we have

Let,

So, we have

From the above equation we can say that, is a constant that depends only on the interval and the number of positions , but not on the function. Hence, the Newton-Cotes coefficients can eb computed in advanced because of this particular reason.

The Trapezoidal and Simpson’s formula are special cases of the Newton-Cotes formula. When , The Newton-Cotes formula is given as

The above equation is the Trapezoidal Formula.

When , the Newton-Cotes formula becomes

This is the Simpson’s formula.

When , the Newrton-Cotes formula is

and

Note that the Newton-Cotes coefficienst depends on but not the integrand function iteself. For , the trapeoidal formulas is polynomial of degree 1. If in the Newton-Cotes formula is a polynomial with degree nop higher than , then the equal sign holds.

If is a polynomial of degree , the right hand side is only an approximation. the quadrature formula is said to have an degree algebraic accuracy.

For , the trapezoidal quadrature rule has thsi error and is said to have the primary algebraic precision.

For , the Simpson’s quadrature rule has this error and si said to have the cubic algebraic precisio.

For a general , the error term is

Assuming that the rounding error of where are the grid points is , , the error of the Newton-Cotes formula is given as,

The last term increases with . This will cause the eror of the approximation to increase, so the Newton-Cotes formula is nstable for big . therefore, in practice the Newton-Cotes formula is rarely used for .

Consider the following example of integrating the following function,

Using the trapezoid formula, we have

using the Simpson’s formula, we have

using the Newton-Cotes formula with , we have

` ````
```System.out.println("integrate using the Newton-Cotes formulas");
val f: final UnivariateRealFunction f = new AbstractUnivariateRealFunction() {
override public double evaluate(double x) {
return 4 / (1 + x * x); // 4/(1+x^2)
}
};
//the limit
double a = 0, b = 1;
Integrator integrator1 = new Trapezoidal(1e-8, 20); // using the trapezoidal rule
Integrator integrator2 = new Simpson(1e-8, 20); // using the Simpson rule
Integrator integrator3 = new NewtonCotes(3, Type.CLOSED, 1e-8, 20); // using the Newton-cotes rule
Integrator integrator4 = new NewtonCotes(3, Type.OPEN, 1e-8, 20); // using the Newton-cotes rule
// the integrations
double I1 = integrator1.integrate(f, a, b);
System.out.println(String.format("S_[%.0f,%.0f] f(x) x = %.16f", a, b, I1));
double I2 = integrator2.integrate(f, a, b);
System.out.println(String.format("S_[%.0f,%.0f] f(x) x = %.16f", a, b, I2));
double I3 = integrator3.integrate(f, a, b);
System.out.println(String.format("S_[%.0f,%.0f] f(x) x = %.16f", a, b, I3));
double I4 = integrator4.integrate(f, a, b);
System.out.println(String.format("S_[%.0f,%.0f] f(x) x = %.16f", a, b, I4));

The output is:

` ````
```S_[0,1] f(x) dx = 3.1415926436556850, using the trapezoidal rule
S_[0,1] f(x) dx = 3.1415926535528365, using the Simpson rule
S_[0,1] f(x) dx = 3.1415926497180000, using the Newton_Cotes closed rule
S_[0,1] f(x) dx = 3.1415926674370604, using the Newton_Cotes open rule

The accuracy and performance of an integration depends not only on which quadrature formula to use, but also on the integrand function itself. In general, the trapezoidal formula is not as accurate as the Simpson’s formula. The low-order Newton-Cotes formula are simple to compute, convenient to use and have high degree of accuracy. On the other hand, the higher-order Newton-Cotes formulas are not only complicated to calculate, but also have poor stability. So, they are rarely used. In practice, we in general prefer low-order formulas like the Simpson’s rule.