57. Numerical integration: Part II#
Anne Kværnø, André Massing
Date: Jan 28, 2021
import matplotlib.pyplot as plt
import numpy as np
from IPython.core.display import HTML
from numpy import pi
def css_styling():
try:
with open("tma4125.css") as f:
styles = f.read()
return HTML(styles)
except FileNotFoundError:
pass # Do nothing
# Comment out next line and execute this cell to restore the default notebook style
css_styling()
As usual, we import the necessary modules before we get started.
%matplotlib inline
newparams = {
"figure.figsize": (8.0, 4.0),
"axes.grid": True,
"lines.markersize": 8,
"lines.linewidth": 2,
"font.size": 14,
}
plt.rcParams.update(newparams)
58. General construction of quadrature rules#
In the following, you will learn the steps on how to construct realistic algorithms for numerical integration, similar to those used in software like Matlab of SciPy. The steps are:
Construction.
Choose
distinct nodes on a standard interval , often chosen to be .Let
be the polynomial interpolating some general function in the nodes, and let the .Transfer the formula
from to some interval .Design a composite formula, by dividing the interval
into subintervals and applying the quadrature formula on each subinterval.Find an expression for the error
.Find an expression for an estimate of the error, and use this to create an adaptive algorithm.
In this course, we will not have the time to cover the last two steps.
59. Constructing quadrature rules on a single interval#
We have already seen in the previous Lecture how quadrature rules
on a given interval
For
where
But how to you proceed if you know want to
compute an integral on a different interval, say
The answer is NO! One can easily transfer quadrature points and weights
from one interval to another.
One typically choose the simple reference interval
The quadrature points can then be transferred to an arbitrary interval
and thus we define
59.1. Example 1: Simpson’s rule#
Choose standard interval
. For Simpson’s rule, choose the nodes , and . The corresponding cardinal functions are .
which gives the weights
such that
.
After transferring the nodes and weights, Simpson’s rule over the interval
becomes .
60. Composite quadrature rules#
To generate more accurate quadrature rule
Increase the order of the interpolation polynomial used to construct the quadrature rule.
Subdivide the interval
into smaller subintervals and apply a quadrature rule on each of the subintervals, leading to Composite Quadrature Rules which we will consider next.
Select
60.1. Composite trapezoidal rule#
Using the trapezoidal rule
60.2. Exercise 1: Testing the accuracy of the composite trapezoidal rule#
Have a look at the CT
function which implements the composite trapezoidal rule:
def CT(f, a, b, m):
"""Computes an approximation of the integral f
using the composite trapezoidal rule.
Input:
f: integrand
a: left interval endpoint
b: right interval endpoint
m: number of subintervals
"""
x = np.linspace(a, b, m + 1)
h = float(b - a) / m
fx = f(x[1:-1])
ct = h * (np.sum(fx) + 0.5 * (f(x[0]) + f(x[-1])))
return ct
Use this function to compute an approximate value of integral
for
# Insert your code here.
Solution.
# Define function
def f(x):
return np.cos(pi * x / 2)
# Exact integral
int_f = 2 / pi
# Interval
a, b = 0, 1
# Compute integral numerically
for m in [4, 8, 16, 32, 64]:
cqr_f = CT(f, a, b, m)
print(f"Number of subintervals m = {m}")
print(f"Q[f] = {cqr_f}")
print(f"I[f] - Q[f] = {int_f - cqr_f:.10e}")
Number of subintervals m = 4
Q[f] = 0.6284174365157311
I[f] - Q[f] = 8.2023358519e-03
Number of subintervals m = 8
Q[f] = 0.6345731492255537
I[f] - Q[f] = 2.0466231420e-03
Number of subintervals m = 16
Q[f] = 0.6361083632808496
I[f] - Q[f] = 5.1140908673e-04
Number of subintervals m = 32
Q[f] = 0.6364919355013015
I[f] - Q[f] = 1.2783686628e-04
Number of subintervals m = 64
Q[f] = 0.636587814113642
I[f] - Q[f] = 3.1958253939e-05
60.2.1. Remarks#
We observe that for each doubling of the number of subintervals
we decrease the error by a fourth.
That means that if we look at
the quadrature error
60.3. Error estimate for the composite trapezoidal rule#
We will now theoretically explain the experimentally observed convergence rate in the previous Exercise 1: Testing the accuracy of the composite trapezoidal rule.
First we have to recall the error estimate for
for the trapezoidal rule on a single interval
60.4. Theorem 1: Quadrature error estimate for composite trapezoidal rule#
Let
where
Proof.
60.5. Interlude: Convergence of -dependent approximations#
Let
The Big
Again, we see that a higher approximation order
60.5.1. Numerical verification#
The following is based on the assumption that
Choose a test problem for which the exact solution is known and compute the
error for a decreasing sequence of
For one refinement step where one passes from
Since
$
60.6. Exercise 1 (extension): Examine the convergence order of composite trapezoidal rule#
# Insert your code here.
Solution
# Define function
def f(x):
return np.cos(pi * x / 2)
# Exact integral
int_f = 2 / pi
# Interval
a, b = 0, 1
errs = []
hs = []
# Compute integral numerically
for m in [4, 8, 16, 32, 64]:
cqr_f = CT(f, a, b, m)
print(f"Number of subintervals m = {m}")
print(f"Q[f] = {cqr_f}")
err = int_f - cqr_f
errs.append(err)
hs.append((b - a) / m)
print(f"I[f] - Q[f] = {err:.10e}")
hs = np.array(hs)
errs = np.array(errs)
eoc = np.log(errs[1:] / errs[:-1]) / np.log(hs[1:] / hs[:-1])
print(eoc)
plt.loglog(hs, errs, "bo-")
Number of subintervals m = 4
Q[f] = 0.6284174365157311
I[f] - Q[f] = 8.2023358519e-03
Number of subintervals m = 8
Q[f] = 0.6345731492255537
I[f] - Q[f] = 2.0466231420e-03
Number of subintervals m = 16
Q[f] = 0.6361083632808496
I[f] - Q[f] = 5.1140908673e-04
Number of subintervals m = 32
Q[f] = 0.6364919355013015
I[f] - Q[f] = 1.2783686628e-04
Number of subintervals m = 64
Q[f] = 0.636587814113642
I[f] - Q[f] = 3.1958253939e-05
[2.00278934 2.00069577 2.00017385 2.00004346]
[<matplotlib.lines.Line2D at 0x7fee1892c680>]

60.7. Exercise 2: Composite Simpson’s rule#
We can now play the exact same game to construct and analyze a Composite Simpson’s rule. In this set of exercises you are ask to develop the code and theory for the composite Simpson’s rule. This will be part of the next homework assignment.
a) Start from Simpson’s rule
where
Show that the resulting composite Simpson’s rule is given by
b) Implement the composite Simpson’s rule. Use this function to compute an approximate value of integral
for
# Insert your code here.
c)
Recall the error estimate for Simpson’s rule on a single
interval, stating that
there is a
whenever
60.8. Theorem 2: Quadrature error estimate for composite Simpon’s rule#
Let
where
Proof.
Insert your proof here.