A related problem to interpolation is when we need to calculate the integral of a function
$$I = \int_{x_1}^{x_N} f(x) dx$$
given values of the function at a discrete set of points $x_1\dots x_N$, which we'll write as $f_i\equiv f(x_i)$. For simplicity here we'll assume that the spacing between points on the grid is constant $x_{i+1}-x_i = \Delta x$, but it's straighforward to generalize to non-uniform sampling if you need to.
To estimate the value of the integral, we need to model how the function behaves in each interval $x_i<x<x_{i+1}$.Aswithinterpolation,wecantake$f(x)$ineachintervaltobeapolynomial,includingadditionaltermsinthepolynomialtoincreasetheaccuracyofourapproximation.
**Rectangle rule**
The first approximation is to assume that $f(x)$ is a constant $f(x)=f_i$ in the interval $x_i\leq x\leq x_{i+1}$. The total integral is then
$$\int_{x_i}^{x_{i+1}} f(x) dx = \int_0^{\Delta x} f(x_i+y) dy \approx f_i \Delta x + (f_{i+1}-f_i){\Delta x\over 2} = {f_i+f_{i+1}\over 2}\Delta x.$$
This is known as the *trapezoidal rule* because it corresponds to the area of the trapezoid formed by connecting the points $(x_i,f_i)$ and $(x_{i+1}, f_{i+1})$ by a straight line.
When we sum over all intervals, each point gets counted twice except for the left and right boundaries, so
The reason for considering the double interval from $x_{i-1}$ to $x_{i+1}$ is that the linear term is odd in this interval (antisymmetric about $x_i$) and so does not contribute to the integral, i.e.
where we need the total number of points $N$ to be an odd number (so we can divide the domain into a set of double intervals).
```{admonition} Exercise: Newton-Cotes
Implement these three methods to integrate a function $f(x)$ of your choice. How does the error in each method scale with the number of points $N$? What kind of functions are integrated exactly (to machine precision) for each method?