
Section1.11Numerical Integration

By now the reader will have come to appreciate that integration is generally quite a bit more difficult than differentiation. There are a great many simple-looking integrals, such as $\int e^{-x^2}\dee{x}\text{,}$ that are either very difficult or even impossible to express in terms of standard functions  1 We apologise for being a little sloppy here — but we just want to say that it can be very hard or even impossible to write some integrals as some finite sized expression involving polynomials, exponentials, logarithms and trigonometric functions. We don't want to get into a discussion of computability, though that is a very interesting topic.. Such integrals are not merely mathematical curiosities, but arise very naturally in many contexts. For example, the error function

\begin{gather*} \mathrm{erf}(x) = \frac{2}{\sqrt{\pi}}\int_0^x e^{-t^2}\dee{t} \end{gather*}

is extremely important in many areas of mathematics, and also in many practical applications of statistics.

In such applications we need to be able to evaluate this integral (and many others) at a given numerical value of $x\text{.}$ In this section we turn to the problem of how to find (approximate) numerical values for integrals, without having to evaluate them algebraically. To develop these methods we return to Riemann sums and our geometric interpretation of the definite integral as the signed area.

We start by describing (and applying) three simple algorithms for generating, numerically, approximate values for the definite integral $\int_a^b f(x)\,\dee{x}\text{.}$ In each algorithm, we begin in much the same way as we approached Riemann sums.

• We first select an integer $n \gt 0\text{,}$ called the “number of steps”.
• We then divide the interval of integration, $a\le x\le b\text{,}$ into $n$ equal subintervals, each of length $\De x=\frac{b-a}{n}\text{.}$ The first subinterval runs from $x_0=a$ to $x_1=a+\De x\text{.}$ The second runs from $x_1$ to $x_2=a+2\De x\text{,}$ and so on. The last runs from $x_{n-1}=b-\De x$ to $x_n=b\text{.}$

This splits the original integral into $n$ pieces:

\begin{equation*} \int_a^b f(x)\,\dee{x} =\int_{x_0}^{x_1} f(x)\,\dee{x} +\int_{x_1}^{x_2} f(x)\,\dee{x} +\cdots +\int_{x_{n-1}}^{x_n} f(x)\,\dee{x} \end{equation*}

Each subintegral $\int_{x_{j-1}}^{x_j} f(x)\,\dee{x}$ is approximated by the area of a simple geometric figure. The three algorithms we consider approximate the area by rectangles, trapezoids and parabolas (respectively).

We will explain these rules in detail below, but we give a brief overview here:

1. The midpoint rule approximates each subintegral by the area of a rectangle of height given by the value of the function at the midpoint of the subinterval \begin{align*} \int_{x_{j-1}}^{x_{j}} f(x) \dee{x} & \approx f\left( \frac{x_{j-1}+x_{j}}{2} \right) \De x \end{align*} This is illustrated in the leftmost figure above.
2. The trapezoidal rule approximates each subintegral by the area of a trapezoid with vertices at $(x_{j-1},0), (x_{j-1},f(x_{j-1})), (x_{j},f(x_{j})), (x_{j},0)\text{:}$ \begin{align*} \int_{x_{j-1}}^{x_{j}} f(x) \dee{x} & \approx \frac{1}{2} \left(f(x_{j-1})+f(x_j \right) \De x \end{align*} The trapezoid is illustrated in the middle figure above. We shall derive the formula for the area shortly.
3. Simpson's rule approximates two adjacent subintegrals by the area under a parabola that passes through the points $(x_{j-1},f(x_{j-1}))\text{,}$ $(x_{j},f(x_{j}))$ and $(x_{j+1},f(x_{j+1}))\text{:}$ \begin{align*} \int_{x_{j-1}}^{x_{j+1}} f(x) \dee{x} & \approx \frac{1}{3} \left(f(x_{j-1})+4f(x_j)+f(x_{j+1} \right) \De x \end{align*} The parabola is illustrated in the right hand figure above. We shall derive the formula for the area shortly.
Definition1.11.1Midpoints

In what follows we need to refer to the midpoint between $x_{j-1}$ and $x_j$ very frequently. To save on writing (and typing) we introduce the notation

\begin{align*} \bar x_j &= \frac{1}{2} \left(x_{j-1}+x_j \right). \end{align*}