Numerical Methods
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,
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 where is the index that keeps of the rectangles, is the number of rectangles we draw, is the height of the -th rectangle, and 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 , this sum changes to an integral and becomes the exact area under the curve.
Not so fast: don't go to infinity
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 , 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 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). Of course, we can also find the exact answer for this one, which will be useful to see how close our approximations are.
Midpoint Rule
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:
Midpoint Rule
Approximate using the Midpoint Rule with 4 subintervals.
Solution
Since we're using four subintervals to cover the distance from to , the width of our rectangles will be . Thus, the first rectangle will start at and go to (with the first midpoint at ), and so on.
Add up the areas of these rectangles to approximate the integral:
Therefore, according to the Midpoint Rule.
How good is this approximation?
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 is
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):
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:
It turns out that this Midpoint approximation is actually very good, off by less than a quarter of one percent.
Trapezoidal Rule
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 where and are the distances from the flat base of the trapezoid to each of its peaks. Therefore, the area of a trapezoid with base is given by
Trapezoidal Rule
Approximate using the Trapezoidal Rule with 4 subintervals.
Solution
The width of our trapezoids will still be . Thus, the first trapezoid will start at and go to , and so on.
Add up the areas of these trapezoids:
Therefore, according to the Trapezoidal Rule.
How good is this approximation?
Here again we can calculate the absolute and relative errors:
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 , 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.