Section 6 Numerical Integration
Subsection 6.1 Integration review
Before we begin looking at numerical calculus, it is useful to recall some of the basic notions. In particular, we'll be reexamining the introduction to calculus most students see in the second semester of the course.
Here is a question - what is \(\int_0^1 x^2 \, dx\text{?}\) Most people that have some experience with calculus will perform the following operation:
However, this computation both misses the point of what the integral represents and uses a technique that will largely be unavailable in practice.
As to the first point, the definite integral represents a measurement of the signed area between a graph and the \(x\)-axis. We say signed area because area above the axis and area below the axis are considered to have opposite signs. Again, a definite integral is an area. Below, we plot the region corresponding to \(\int_0^1 x^2 \, dx\text{.}\)
Now, the area of this region is certainly \(\frac{1}{3}\text{.}\) To arrive at that conclusion, we used one of the most important theorems of continuous mathematics, the fundamental theorem of calculus, which gives a connection between definite integrals, that is the signed area under a curve, with indefinite integrals, that is antiderivatives.
Theorem 6.1. Fundamental theorem of calculus, part II.
Suppose that \(f\) is a function on an interval \([a,b]\) with an antiderivative \(F\) such that \(F'(x) = f(x)\) for all \(x \in [a,b]\text{.}\) If \(f\) is Riemann integrable, then
One salient assumption present in the fundamental theorem of calculus is the existence of an antiderivative. Unfortunately, there are many functions that do not possess a (closed form) antiderivative, including some of the most useful functions in practice. For example, the normal distribution from statistics is essentially defined by the function \(f(x) = e^{-x^2}\text{.}\) A typical problem might wish to compute an integral like \(\int_{-1}^{2} e^{-x^2} \, dx\text{,}\) which is a simple area contained under a very nice curve, as shown below.
However, the fundamental theorem cannot be used to compute the area indicated by the definite integral because the function \(e^{-x^2}\) has no closed form antiderivative. So we need to approach the area finding problem with techniques related to the definition of definite integrals, which consist of breaking the area under functions up into approximating rectangles and then making the rectangles uniformly smaller in width.
The Riemann sum that approximates the signed area under \(f\) on \([a,b]\) is given by the following formula. Let \(n\) be the number of approximating rectangles, and the width of each rectangle be \(\Delta x = \frac{b - a}{n}.\) Then we can define a partition of \([a,b]\) by \(x_0 = a\text{,}\) \(x_i = x_0 + i \Delta x\text{,}\) and \(x_n = b\text{.}\) On each subinterval \([x_i, x_{i+1}]\text{,}\) we choose a point \(x_i^*\text{.}\) Then the area under \(f\) can be approximated by the expression
Those functions for which \(\lim_{n \to \infty} \sum_{i = 0}^{n-1} f(x_i^*) \Delta x\) converges are called Riemann integrable.
Note, it need not be the case that the rectangles have equal width, which is an assumption made here to simplify the presentation.
Subsection 6.2 The trapezoid rule
An immediate observation of a Riemann sum approximation for a definite integral might lead you to conclude that other shapes might provide more accurate approximations than rectangles. An easy shape to work with in this context is the trapezoid - it has a simple formula for area and allows us to avoid having to choose random points inside the subintervals. Compare the following pictures.
While the example might seem to be artificially chosen to make the trapezoids significantly more accurate than the rectangles, in fact, the vast majority of graphs of interest will look like the pictures above for small enough subintervals. This motivates the development of the trapezoid rule for approximating a definite integral.
Recall that the area of a trapezoid with height \(h\) and base widths \(b_1, b_2\) is given by the formula
Suppose that \(f\) is a function defined on the interval \(I = [a,b]\) and let \(x_0, \ldots, x_n\) be a uniform partition of \(I\) with subinterval width \(\Delta x = \frac{b -a}{n}\text{.}\) Consider the subinterval \([x_i, x_{i+1}]\text{.}\) Then
Thus,
and we define the \(n\) segement trapezoid approximation to the area under \(f\) by
Let's use the trapezoid rule to approximate the integral indicated above - \(\int_0^6 x^2 \, dx\text{.}\) We'll use three trapezoids as in the example picture.
We have included the true result, since we can use the fundamental theorem in this case. The result of the approximation with just 3 trapezoids is a relative error of just 5 percent.Subsection 6.3 A special case of Richardson's extrapolation (optional)
This section will have a different flavor than most of the rest of the notes. Here, we'll see the “analysis” part of numerical analysis - that is, we're going to use theoretical ideas and estimates to improve the approximation given in the trapezoid formula. The idea is that the application of mathematical reasoning can lead to significant improvements in our naive formulations (a theme common in approximation theory).
We'll first recall the triangle inequality, which says that \(\abs{a + b} \leq \abs{a} + \abs{b}\text{.}\) In fact, we can apply this to a sum of any finite length, by induction:
Now, Riemann sums are finite sums, and so the triangle inequality applies.
and when the limit of the Riemann sum exists as the number of rectangles tends to infinity (that is, whenever \(f\) is Riemann integrable), we get the integral version of the triangle inequality:
This is a theorem in real analysis and will be used here without a formal proof beyond the sketch above.
Let \(I = \int_a^b f(x) \, dx\text{.}\) Let \(T_n\) represent the \(n\) segment trapezoid approximation of \(I\text{.}\) Let \(E_T\) be the error in the approximation - that is
Our first goal is to measure how large the error \(E_T\) is expected to be in terms of the number of trapezoids \(n\text{.}\)
Theorem 6.2.
Let \(f\) be Riemann integrable on \([a,b]\text{.}\) Then
Proof.
Let \(\Delta x = x_{i+1} - x_i = \frac{b -a}{n}\text{.}\) We first analyze the error in the trapezoid approximation on a single interval. A \(u\)-substitution gives
Using integration by parts twice, we get
\(u\)
\(v\)
\(f(t + x_i)\)
\(1\)
\(f'(t + x_i)\)
\(t + A\)
\(f''(t + x_i)\)
\(\frac{(t + A)^2}{2} + B\)
From this point, the idea is to choose values of \(A, B\) that force each term to play a certain role, with the goal of concentrating the error of the approximation in the integral term. First, we choose \(A\) so that the first term above is equal to the trapezoid area - that is, we want
Algebra shows that \(A = \frac{-\Delta x}{2}\) solves the equation.
Now, we want to choose \(B\) so that the second term is zero. That is, we want
This obviously holds when \(B = \frac{-(\Delta x)^2}{8}\text{.}\)
We conclude that the error on the \(i\)th segment, denoted \(E_T(i)\) is given by
Now, we can get the total error in the trapezoid approximation by adding each of the individual errors.
For a well-behaved function \(f\) (the precise assumption is that \(f\) is \(C^2\) on \([a,b]\)),the second derivative is bounded on \([a,b]\) - that is, we assume that there exists a constant \(K\) so that \(\abs{f''(x)} \leq K\) for all \(x \in [a,b]\text{.}\) Then, using the triangle inequality, we derive the approximation
The function \(g(t) =\left(\frac{(t - \frac{\Delta x}{2})^2}{2} - \frac{(\Delta x)^2}{8}\right)\) is a parabola that opens upwards with zeros at \(t = 0\) and \(t = h\text{,}\) and so
Putting this together with the previous computation, we get
where \(K\) was an absolute bound for \(f''\) on \([a,b]\text{,}\) and in the worst case, we get \(\abs{E_T} \leq \frac{K(b-a)^3}{12} \frac{1}{n^2} = \frac{C}{n^2}\) - that is, the error is proportional to \(\frac{1}{n^2}\text{,}\) which establishes the claim.
Under the assumption of worst case error and a reasonable function \(f\text{,}\) we conclude that the total trapezoidal error \(E_T\) is proportional to \(\frac{1}{n^2}\text{,}\) or in other words that
So how can we use this to build a better process? Note that for \(n\) segments, we can write
and likewise for \(2n\) segments, we have
which is a system of simultaneous equations. We'll prepare to eliminate \(C\text{.}\)
Thus, we have what is known as a first order Richardson's extrapolation -
Let's see how it performs with our existing example.
Subsection 6.4 Simpson's 1/3 rule
An alternative to using trapezoids is to use polynomials to interpolate sample points. It turns out that using quadratic polynomials on equally spaced interpolation points gives a very nice formula. We'll begin with a single segment and approximate \(\int_a^b f(x)\, dx\text{.}\)
Recall that there is a unique parabola through any three points - we'll use the points \((a, f(a)), ((a+b)/2, f((a+b)/2), (b, f(b))\text{.}\) We have several techniques available for finding such an interpolation - we'll derive ours using Newton polynomials. The Newton polynomial through our points is
where
So in effect, we're saying that
where we have shoved integration and substitution under the rug (or into the ellipsis, as it were).
Now suppose that \([a,b]\) is partitioned into \(n\) segments of equal length \(\Delta x = \frac{b - a}{n}\text{,}\) where \(n\) is even (which will allow overlapping sequences of three points). Then for three sequential points \(x_i, x_{i+1}, x_{i+2}\text{,}\) the previous computation gives
Since we chose \(n\) to be even, we get