Today I feel bored, so let’s go over some mathematics. We’ll review weeks 4−6 even though it’s way past week 4 because matrices are really tedious to typeset in LATEX so I don’t know if I even want to write about them yet to be honest.
Approximating definite integrals
There’s probably (is) an infinite number of ways to approximate definite integrals. We’ll go over a few of them.
Gaussian quadrature
First, we want to find some way to approximate
∫abf(x)dx.
We can do this by considering the integration rule
where x0,x1,…,xn are the nodes in the interval [a,b] and A0,A1,…,An are the coefficients.
Since there are 2n+2 unknowns, we can make eqref(2) exact for polynomials of degree at most 2n+1 i.e., f(x)=xk for k=0,1,…,2n+1.
This is known as Gaussian quadrature. Now let us look at a few different Gaussian quadrature rules.
Deriving the two-point Gaussian rule
We’ll consider the interval [−d,d] and let
∫−ddf(x)dx≈A0f(x0)+A1f(x1).
eqref(3) gives us the exact result whenever f(x) is a polynomial of degree at most 3. That is, for f(x)=a0+a1x+a2x2+a3x3, for some constants a0,a1,a2,a3.
Why you ask? Integrating said constants will tell us.
After solving this system of nonlinear equations, notice that A0=A1=d and the nodes are symmetric about 0 with magnitude 3d.
Without loss of generality, we can take x0=−3d and x1=3d and the formula becomes
∫−ddf(x)dx≈d[f(−3d)+f(3d)].
Two-point Gaussian rule on [−1,1]
Generally speaking, we use the interval [−1,1] instead of [−d,d], so eqref(9) becomes
∫−11f(x)dx≈f(−31)+f(31).
Mapping the rule to an arbitrary interval [a,b]
If you decide to use an arbitrary interval [a,b], we can do this fairly easily by mapping x∈[−1,1] to t∈[a,b] from the transformation function
t=21[(b−a)x+(a+b)].
eqref(11) forces t=a for x=−1 and t=b for x=1, and dxdt=21(b−a).
Thus, we have
For the error formula below, assume f has a continuous (2n+2)th derivative on [a,b]. Then the error from approximating ∫abf(x)dx with the (n+1)-point Gaussian rule is
Unlike Gaussian quadrature, Newton-Cotes formulas use equally spaced nodes (this will be important later). For a closed Newton-Cotes rule, the endpoints are included, while as you’d expect, an open Newton-Cotes rule excludes the endpoints.
xi=a+ih,h=nb−a,i=0,1,…,n,n≥1.
The general form is still
∫abf(x)dx≈i=0∑nAif(xi),
We’ll be looking at the Trapezoidal rule and Simpson’s rule which comes from the closed Newton-Cotes formula with n=1 and n=2 respectively. But first, let’s get the general error terms for closed Newton-Cotes formulas out of the way.
Proposition (Closed Newton-Cotes error term)
Suppose
∫abf(x)dx≈i=0∑nAif(xi)
is the (n+1)-point closed Newton-Cotes formula with x0=a, xn=b, h=nb−a, and n≥1. Define the error by
En=∫abf(x)dx−i=0∑nAif(xi).
Ck([a,b]) denotes the set of functions with k continuous derivatives on [a,b].
If n is even and f∈Cn+2([a,b]), then there exists ξ∈(a,b) such that
I split the cases to make the distinction clear. Specifically the parity of n. The Trapezoidal rule has n=1, so it uses the odd case in eqref(20). Simpson’s rule has n=2, so it uses the even case in eqref(19).
Following the degree we can see that the Trapezoidal rule approximates a curve with a straight line, while Simpson’s rule approximates the curve with a parabola. Both Trapezoidal and Simpson’s rule are from the closed Newton-Cotes family.
Now we know that any Newton-Cotes formula uses equally spaced nodes, which includes the Trapezoidal and Simpson’s rules. It’s very rigid in that sense, the data we use to approximate the integral must be equally spaced. For data on a uniform grid it’s nice but we aren’t free to choose better points.
Gaussian quadrature instead treats the locations of these sample points as part of the problem. Instead of choosing equally spaced nodes to simplify the problem (since they only have to deal with the weights), the goal is to find the “best” nodes and weights. So the points aren’t arbitrary, but placed in locations that make the formula exact for the largest possible range of polynomial behaviour. To avoid too much ambiguity, we don’t use evenly spaced points/nodes or “more” points to get a better approximation, rather “better” positioned points;
and these “better” points are placed where they give the most information about the integral. That is, the roots of the polynomials. Now let’s actually look at the approximation methods.
Trapezoidal rule
Proposition (Trapezoidal rule)
The Trapezoidal rule is the closed Newton-Cotes rule with n=1. Therefore,
x0=a,x1=b,h=b−a.
Approximating f by (a,f(a)) and (b,f(b)) gives
∫abf(x)dx≈2b−a[f(a)+f(b)].
The error term is
ET=−12(b−a)3f′′(ξ)
for some ξ∈(a,b). Thus,
∣ET∣≤12(b−a)3x∈[a,b]max∣f′′(x)∣.
Example (Trapezoidal approximation)
Approximate
∫01exdx
using the trapezoidal rule, and find an upper bound for the error.
Solution
Here a=0 and b=1. Using eqref(22),
T=21[f(0)+f(1)]=21(1+e)≈1.859140914.
For f(x)=ex, we have f′′(x)=ex. On [0,1],
x∈[0,1]max∣f′′(x)∣=e.
Using eqref(24),
∣ET∣≤12(1−0)3e=12e≈2.26523×10−1.
Since the exact value is e−1, the actual absolute error is
∣(e−1)−T∣≈1.40859×10−1.
Simpson’s rule
Proposition (Simpson's rule)
Simpson’s rule is the closed Newton-Cotes rule with n=2. The three equally spaced nodes are
x0=a,x1=2a+b,x2=b.
Approximating f gives
∫abf(x)dx≈6b−a[f(a)+4f(2a+b)+f(b)].
The error term is
ES=−2880(b−a)5f(4)(ξ)
for some ξ∈(a,b). Thus,
∣ES∣≤2880(b−a)5x∈[a,b]maxf(4)(x).
Example (Simpson approximation)
Approximate
∫01exdx
using Simpson’s rule, and find an upper bound for the error.
As you can see from Proposition 1.5, it’s pretty much exactly the same as the closed case, except for the being open part (and some shifted indexing, spacing, etc.).
Composite formulas
Instead of applying one rule across the whole interval, composite formulas split [a,b] into smaller subintervals and apply the same rule repeatedly.
Composite Trapezoidal rule
Proposition (Composite trapezoidal rule)
Let [a,b] be divided into n equal subintervals, where
h=nb−a,xi=a+ih,i=0,1,…,n.
Then
∫abf(x)dx≈2h[f(x0)+2i=1∑n−1f(xi)+f(xn)].
If M=maxx∈[a,b]∣f′′(x)∣, then the error satisfies
∣ET∣≤12Mnh3.
Example (Composite trapezoidal approximation)
Approximate
∫01exdx
using the composite trapezoidal rule with 6 equal subintervals. Then determine an error bound and compare it with the true error.
Splitting [a,b] into smaller subintervals (making h smaller) reduces the error for composite rules. For the composite trapezoidal rule,
∣ET∣≤12M(b−a)h2,ET=O(h2)
Halving h reduces the error by about a factor of 4. For the composite Simpson’s rule,
∣ES∣≤180M(b−a)h4,ES=O(h4)
Halving h reduces the error by about a factor of 16. Simpson’s rule converges much faster than the trapezoidal rule for smooth functions.
The exponent of h in the error term is what matters here. Higher order requires (often, but not necessarily) fewer points for the same accuracy (i.e., the function has to be nice relative to the approximation method being used). That’s the whole point, not just getting the answer but getting the right answer while using as little resources as possible (computationally speaking).
In practice, we don’t really care (much) about the error term, rather, we care about the order of convergence. The two are pretty much the same thing in a sense (not really, but also, not really still), but the idea is what matters.
In practice, there is a lot of nuance and cases which you should check and consider to get a feel for how these work among different functions, and I won’t be going over those cause you already have all the information you need to test it out yourself.
But you should have a look and see how these approximation methods are used and in which contexts they are used as well as why some methods aren’t used anymore (generally speaking).
Anyway that’s it for now, there are a lot of details that I’ve left out, but that’s what books are for.