As much as we've talked about different methods of integration, and as many types of functions as we've learned to deal with, the fact remains that there are many functions that simply won't yield to any of the methods that we've discussed. For example, \[\int_2^{12} e^{x^2} \ dx\] isn't doable by any of the analytical methods we've got in our toolbox. There are massive tables of integrals that can be consulted for some of these (although that is becoming less and less common in the modern age), but we can also tackle this with numerical methods.

The basic idea hearkens back to what you learned when you first saw integrals: if we want to find the area under a curve, we can approximate this area by adding up the areas of a bunch of rectangles (called a Riemann sum).

The Riemann sum is typically written \[\sum_{i=1}^n f(x_i) \ \Delta x,\] where \(i\) is the index that keeps of the rectangles, \(n\) is the number of rectangles we draw, \(f(x_i)\) is the height of the i-th rectangle, and \(\Delta x\) is the width of each rectangle (they all have the same width).

Of course, in Calculus I, you probably saw this and then discovered that as you increase the number of rectangles, the approximation to the area under the curve gets better and better, so as \(n \to \infty\), this sum changes to an integral and becomes the *exact* area under the curve.

If, however, we don't let the number of rectangles increase to infinity (but rather to some large, manageable number), we can stick with an approximation, and there's nothing we can't handle! So if we want to integrate \(e^{x^2}\), we can't do so analytically, but we can get a good approximation for the definite integral between any two limits that we like. Of course, the more rectangles we use, the better the approximation but the more tedious it is to do. Fortunately, computers are perfectly suited to performing simple, repetitive tasks, so we can get approximations that are indistinguishable from the exact values.

This only works for definite integrals, but since we'll be dealing with definite integrals whenever we see any kind of application, it is still a tremendously powerful tool.

We'll look at two variations for approximating integrals; there are others, but these will serve to illustrate the point. We'll do the same example with both of them; we'll approximate \[\int_2^4 x^2 \ dx\] using 4 subintervals (our approximation won't be that great, but we want to keep the number small enough that we can do it by hand).

The Midpoint Rule is a simple variation on the Riemann sum shown above; instead of using one end of the rectangles to define their height, we can use the middle of each rectangle, as shown in this figure:

Approximate \(\displaystyle\int_2^4 x^2 \ dx\) using the Midpoint Rule with 4 subintervals.

Since we're using 4 subintervals to cover the distance from 2 to 4, the width of our rectangles will be \(\Delta x = 0.5\). Thus, the first rectangle will start at 2 and go to 2.5 (with the first midpoint at 2.25), and so on.

Adding up these rectangles to get our approximation,

\[\begin{align} \int_2^4 x^2 \ dx &\approx f(2.25) \Delta x + f(2.75) \Delta x + f(3.25) \Delta x + f(3.75) \Delta x\\ &= (2.25)^2 \cdot 0.5 + (2.75)^2 \cdot 0.5 + (3.25)^2 \cdot 0.5 + (3.75)^2 \cdot 0.5\\ &= 18.625. \end{align}\] Therefore, \[\ans{\int_2^4 x^2 \ dx \approx 18.625,}\] according to the Midpoint Rule.Of course, with the example we picked, we *know* what the exact answer is, which is valuable because it gives us a chance to see how good this approximation is. The exact value, of course, is \[\int_2^4 x^2 \ dx = \dfrac{1}{3}x^3 \bigg|_2^4 = \dfrac{1}{3}\left(4^3-2^3\right) = \dfrac{56}{3} \approx 18.6667.\]

First of all, then, we can talk about the **absolute error**, which is how far our approximation was from the truth. This is the difference between the two numbers, and we take the absolute value of that difference so that the error is a positive number (which matches our intuition about it being a distance):
\[\begin{align}
\text{Absolute Error } &= |\text{Exact Answer } - \text{ Approximate Answer}|\\
&= \left|\dfrac{56}{3} - 18.625\right| \approx 0.042
\end{align}\]

A better measure, though, is the **relative error**, which puts that number into context by converting it into a percentage. To do this, divide the absolute error by the exact answer and translate that answer into a percentage:
\[\begin{align}
\text{Relative Error } &= \left|\dfrac{\text{Exact Answer } - \text{ Approximate Answer}}{\text{Exact Answer}}\right|\\
&\approx \left|\dfrac{18.6667 - 18.625}{18.6667}\right|\\
&\approx 0.00223 = 0.223\%
\end{align}\]
It turns out that this Midpoint approximation is actually very good.

With the Trapezoidal Rule, as the name suggests, we approximate the area under the curve by adding up the areas of trapezoids instead of rectangles, as shown here.

All we need, then, is the formula for the area of a trapezoid. It can be found by chopping the trapezoid into a rectangle and a triangle, but the easiest way to remember it is simply as the area of a rectangle whose height is the average height of the trapezoid. The average height of the trapezoid is \[\dfrac{h_1 + h_2}{2}\] where \(h_1\) and \(h_2\) are the distances from the flat base of the trapezoid to each of its peaks. Therefore, the area of a trapezoid with base \(b\) is give by \[\dfrac{1}{2}(h_1+h_2)b.\]

Approximate \(\displaystyle\int_2^4 x^2 \ dx\) using the Trapezoidal Rule with 4 subintervals.

The width of our trapezoids will still be \(\Delta x = 0.5\). Thus, the first trapezoid will start at 2 and go to 2.5, and so on.

Adding up these trapezoids to get our approximation,

\[\begin{align} \int_2^4 x^2 \ dx &\approx \dfrac{1}{2}(f(2)+f(2.5)) \Delta x + \dfrac{1}{2}(f(2.5)+f(3)) \Delta x + \dfrac{1}{2}(f(3)+f(3.5)) \Delta x + \dfrac{1}{2}(f(3.5)+f(4)) \Delta x\\ &= \dfrac{1}{2}(2^2+2.5^2) 0.5 + \dfrac{1}{2}(2.5^2+3^2) 0.5 + \dfrac{1}{2}(3^2+3.5^2) 0.5 + \dfrac{1}{2}(3.5^2+5^2) 0.5\\ &= 18.75. \end{align}\] Therefore, \[\ans{\int_2^4 x^2 \ dx \approx 18.75,}\] according to the Trapezoidal Rule.Here again we can calculate the absolute and relative errors:

\[\begin{align} \text{Absolute Error } &= |\text{Exact Answer } - \text{ Approximate Answer}|\\ &= \left|\dfrac{56}{3} - 18.75\right| \approx 0.0833\\ \\ \text{Relative Error } &= \left|\dfrac{\text{Exact Answer } - \text{ Approximate Answer}}{\text{Exact Answer}}\right|\\ &\approx \left|\dfrac{18.6667 - 18.75}{18.6667}\right|\\ &\approx 0.00446 = 0.446\% \end{align}\]Notice that, although in the picture the approximate area is nearly indistinguishable from the exact area (and the Trapezoidal approximation is still very good), the Midpoint Rule actually ended up getting a better approximation in this case. Without going into detail, it turns out that this is not an unusual result: for simple, smooth functions like \(x^2\), the pieces above and below the curve that represent errors in the Midpoint Rule tend to cancel each other out, so the Midpoint Rule outperforms the Trapezoidal Rule.

With more complicated functions, though, the Trapezoidal Rule oftens outperforms the Midpoint Rule, and of course there are other numerical methods that can do better than either of these two, but usually at the sacrifice of simplicity and thus performance.