## Newton-Cotes Integration Formulas

The Newton-Cotes Integration formulas are one of the most traditional and well-known numerical integration methods. The basis of these formulas is to replace the complicated function with the approximating function that is easy to integrate.

“n” is the polynomial’s order in the above equation. In other words, the users can choose a value of “n” and use that “n” order approximation to approximate a tricky integral. For instance, the graph below uses a first-order approximation or straight line to reach an integral between two points, a and b.

Similarly, they can also use a parabola for the exact purpose. However, it is beneficial to employ multiple straight lines or parabolas applied piece-wise to the constant length function in some scenarios.

### Newton-Cote Forms

Newton-Cote formulas have both closed and open forms.

#### Open-Forms

The forms in which the user does not know the integration limits are called open forms. In these cases, the boundaries extend beyond the data range. These forms of formulas are generally not used to find definite integrals.

#### Closed-Form

The forms in which the user knows the data points of the start and end of the integration limits are called closed-form.

*Trapezoidal Rule is a well-known Newton-Cote’s closed-form method. The users utilize this Rule when the polynomial is of first-order in the above equation, which makes the equation:*

Where “a” and “b” marks the interval between which the equation has to calculate the approximate integral.

As the users can represent the straight line as:

The users can estimate f(x)’s integral between a and b limit by measuring the area under this straight line, as discussed earlier. Therefore, they can write the equation as:

Applying integration on the above equation gives:

**It is known as the Trapezoidal Rule.**

Furthermore, the users can consider the trapezoidal Rule equivalent to calculating the trapezoid’s area under the line segment connecting two points. That is why the users can also represent the integral estimate as:

I ~= ( b – a) X average height

Where **b – a** is the width of the trapezoid and the average height is the **[ ( f (a) + f (b) ) / 2 ] **as mentioned in the trapezoidal Rule**.**

### Trapezoidal Rule Error

As the trapezoidal Rule uses a straight line to join intervals to find the integral approximation, it can incur error while trying to find the approximation under a curve. It happens because a line segment cannot cover the whole area under the curve. The estimation of local truncation error of a single point is:

This equation indicates that the trapezoidal Rule can give accurate linear equations results, Producing some error for curved functions or higher-order derivatives. Therefore, to reduce the error, the users employ multiple trapezoidal rule applications, meaning draw multiple lines to cover the exact area under the curves.

## Multiple Application Trapezoidal Rule

The multiple application trapezoidal rule improves the result’s accuracy by dividing the interval into smaller intervals and applying the trapezoidal Rule on the shorter intervals. Doing so flattens the curve because the curves are also the tiny points joined together, as are the lines. Therefore, more division means more accuracy in the results.

In applying it, the users take n+1 points, which are equally spaced (x0, x1, x2, x3, x4, x5, …, xn), and represent the a and b as x0 and xn. After doing so, they apply the trapezoidal Rule to each interval. The compact formula of the multiple application trapezoidal rule is:

Where width “h” is **[ ( b – a ) / n ] **. Therefore, the users can calculate “h” by using the values of x0 and xn as a and b, respectively. By putting the value of h, the equation becomes:

### Multiple Application Trapezoidal Rule Error

The users can calculate the error for multiple application trapezoid by calculating the individual segments’ error and summing them to get the final value. However, the general equation for the reduced error obtained from the multiple application is:

The equation above shows that by doubling the number of points “n,” the users can reduce the error by a quarter, which means that the error value has an inverse relation with the number of segments. The more the segments, the less is the approximate error of the integral.

### Multiple Application Trapezoidal Rule in C++

While programming the trapezoidal Rule in any language, the users must take care of the data types because even if the inputs are integers, the results can still be in the decimals. Therefore, while writing the function Trapm(), it is better to define the return type as double.

The C++ implementation of the above equation is given below:

double Trapm(double h, int n, double f[]) { double sum = f[0]; for (int i = 1; i <= n-1; i++) { sum = sum + (2 * f[i]); } sum += f[n]; return (h * (sum / 2)); }

The function Trapm() takes the width “h,” the number of points “n,” and the function points “f []” as its input. The users can find the trapezoid’s width using h = [ ( b – a ) / n ] or write the program to calculate it. As the zeroth index store the function’s value at point x0’s, the user stores it in a variable named “sum.” Additionally, the “for loop” multiplies the function’s values at the points between x0 and xn by two, calculates their sum, and adds the sum with f(x0). After coming out of the loop, the function adds the f (n) in the previously calculated sum to calculate the average height. Lastly, it multiplies the width “h” with the final sum to calculate the approximate interval and returns its value to the main.

#### Main

The program’s main function has an array of a double data type, which contains the function’s values at n points. The double variable “h” stores the already calculated trapezoid’s width. Moreover, the integer n has the number of the “x” points stored in the function array. As the array’s index starts from zero, the user counts the points starting from zero. Finally, he calls the above function Trapm() and passes these values as its arguments; and prints the returned value on the screen.

Note: the users can use the system (“pause”) in the windows machine to pause the program to show the program’s output to the users.

int main() { //--------Multiple appliation trapezoidal rule-------// double f[] = {0.2, 2.456, 0.232}; double h = 0.4; int n1 = 2; cout << Trapm(h, n1, f); //...............................// system(“pause”); return 0; }