Numerical Integration Techniques
Numerical Integration Techniques
1 Introduction
In this note, we will discuss numerical methods for the approximation of (finite) integrals of the
form Z b
I [ f ]( a, b) = f ( x )dx.
a
A numerical quadrature or a quadrature rule is a formula for approximating such integrals. Quadra-
tures are usually of the form
n
Q[ f ]( a, b) = ∑ wi f ( x i ),
i =0
where xi , wi for i = 0, 1, . . . , n are respectively the nodes and the weights of the quadrature rule.
If the function f is given from the context, we will for simplicity denote the integral and the
quadrature simply as I ( a, b) and Q( a, b). Examples of numerical quadratures known from previ-
ous courses are the trapezoidal rule, the midpoint rule and Simpson’s rule.
In practice, we will not use a single (or simple) quadrature for the whole interval [ a, b], but rather
choose a partitioning
a = X0 < X1 · · · < X m = b
into m sub-intervals, apply a quadrature on each of the subintervals [ X j , X j+1 ], and then add the
results together. This leads to composite quadratures yielding the approximation
m −1 m −1
I [ f ]( a, b) = ∑ I [ f ]( X j , X j+1 ) ≈ ∑ Q[ f ]( X j , X j+1 ).
j =0 j =0
In this note we will see how quadrature rules can be constructed from integration of interpolation
polynomials. We will demonstrate how to do error analysis and how to find error estimates both
for simple and composite quadratures. Moreover, we will demonstrate how the partitioning of
the integration interval can be chosen automatically based on these error estimates; this idea is
called adaptive integration.
In the sequel, we will use material from Preliminaries, Sections 3.2, 4 and 5.
1
2 Quadrature based on polynomial interpolation.
This section relies on the content of the note on polynomial interpolation, in particular the section
on Lagrange polynomials.
Choose n + 1 distinct nodes xi , i = 0, . . . , n in the interval [ a, b], and let pn ( x ) be the interpolation
polynomial satisfying the interpolation condition
p n ( x i ) = f ( x i ),
i = 0, 1, . . . .
Rb Rb
We will then use a pn ( x )dx as an approximation to a f ( x )dx. By using the Lagrange form of the
polynomial
n
pn ( x ) = ∑ f (xi )`i (x)
i =0
with the cardinal functions `i ( x ) given by
n x − xj
`i ( x ) = ∏ xi − x j
,
j=0,j6=i
The weights in the quadrature are simply the integrals of the cardinal functions over the interval:
Z b
wi = `i ( x ) dx.
a
Let us derive two schemes for integration over the interval [0, 1], and apply them to the integral
Z 1 π 2
I (0, 1) = cos x = = 0.636619 . . . .
0 2 π
Example 1 (trapezoidal rule): Let x0 = 0 and x1 = 1. The cardinal functions and thus the
weights are given by
Z 1
`0 ( x ) = 1 − x, w0 = (1 − x )dx = 1/2,
0
Z 1
`1 ( x ) = x, w1 = xdx = 1/2.
0
The corresponding quadrature rule, better known as the trapezoidal rule and usually denoted by
T, is given by
1
T (0, 1) = [ f (0) + f (1)] .
2
This formula applied to the function f ( x ) = cos(πx/2) gives
1h π i 1
T (0, 1) = cos(0) + cos = ,
2 2 2
and the error is
2 1
I (0, 1) − T (0, 1) = − = 0.138 . . .
π 2
2
√
Example
√ 2 (Gauß-Legendre quadrature with two nodes): Let x0 = 1/2 − 3/6 and x1 = 1/2 +
3/6. Then √
√ 1+ 3
Z 1
`0 ( x ) = − 3x + , w0 = `0 ( x )dx = 1/2,
2 0
√
√ 1− 3
Z 1
`1 ( x ) = 3x + , w1 = `1 ( x )dx = 1/2.
2 0
The quadrature rule is
" √ ! √ !#
1 1 3 1 3
Q(0, 1) = f − +f + .
2 2 6 2 6
1h π π i
Q(0, 1) = cos x0 + cos x1 = 0.635647 . . .
2 2 2
with an error
I (0, 1) − Q(0, 1) = 9.72 . . . · 10−4 .
So the choice of nodes clearly matters!
Before concluding this section, let us present simple indication on the quality of a method:
Q[ x j ]( a, b) = I [ x j ]( a, b), j = 0, 1, . . . , d,
Q[ x d+1 ]( a, b) 6= I [ x d+1 ]( a, b).
All quadratures constructed from Lagrange interpolation polynomials in n + 1 distinct nodes will
automatically be of precision at least n. This follows immediately from the way these quadratures
are constructed: Indeed, if p ∈ Pn is a polynomial of degree at most n, then the interpolation
polynomial will simply be equal to p itself, and thus the integration is performed exactly.
Note, however, that the degree of precision can actually be larger than n. It is left to the reader
to show that the trapezoidal rule from Example 1 has degree of precision 1, whereas the formula
from Example 2 has degree of precision 3.
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:
3
Construction.
1. Choose n + 1 distinct nodes on a standard interval [−1, 1].
2. Let pn ( x ) be the polynomial interpolating some general function f in the nodes, and define
the quadrature on [−1, 1] to be Q[ f ](−1, 1) := I [ pn ](−1, 1).
3. Transfer the formula Q from [−1, 1] to any arbitrary interval [ a, b].
4. Find the composite formula by dividing the interval [ a, b] into subintervals and applying the
quadrature formula on each subinterval.
5. Find an expression for the error E[ f ]( a, b) = I [ f ]( a, b) − Q[ f ]( a, b).
6. Find an expression for an estimate of the error, and use this to create an adaptive algorithm.
We will go through the steps above for one method, Simpson’s formula. The strategy is quite
generic, so it is more important to understand and remember how results are derived, not exactly
what they are. The different algorithms will be implemented and tested, and theoretical results
will be verified by numerical experiments.
We will adopt the standard notation and denote this particular quadrature by S[ f ]( a, b).
The quadrature rule is defined by the choice of nodes on a standard interval [−1, 1]. For Simpson’s
rule, choose the nodes t0 = −1, t1 = 0 and t2 = 1. The corresponding cardinal functions are
1 1
`0 = ( t2 − t ), `1 ( t ) = 1 − t2 , `2 ( t ) = ( t2 + t ).
2 2
which gives the weights
Z 1 Z 1 Z 1
1 4 1
w0 = `0 (t)dt = , w1 = `1 (t)dt = , w2 = `2 (t)dt =
−1 3 −1 3 −1 3
such that
Z 1 Z 1 2
1
−1
f (t)dt ≈
−1
p2 (t)dt = ∑ wi f ( t i ) = 3 [ f (−1) + 4 f (0) + f (1) ] .
i =0
4
3.1.2 Transfer the integral and the quadrature to the interval [ a, b]
The integral and the quadrature are transferred to some arbitrary interval [ a, b] by the transforma-
tion
b−a b+a b−a
x= t+ , so dx = dt.
2 2 2
By this transformation, the nodes t0 = −1, t1 = 0, and t2 = 1 in the interval [−1, 1] are mapped to
a+b
x0 = a, x1 = , x2 = b.
2
Thus we obtain the quadrature
Z b Z 1
b−a b−a b−a
b+a b+a
f ( x )dx = f t+ dt ≈ f ( a) + 4 f + f (b) .
a 2 −1 2 2 6 2
b−a b+a
S( a, b) = [ f ( a) + 4 f (c) + f (b) ] with c = .
6 2
Next we will have to discuss the corresponding composite rule. Here we have to choose a parti-
tion of the interval [ a, b] into sub-intervals, evaluate the quadrature on each of the sub-intervals,
and finally add all results together. The final result will heavily rely on the choice of the sub-
intervals, and we will discuss later an automated strategy for their construction. For now, we
content ourselves with the simplest construction, where we take sub-intervals of equal lengths.
Divide [ a, b] into 2m equal intervals of length h = (b − a)/(2m). Let x j = a + jh, i = 0, · · · , 2m,
and apply Simpson’s rule on each subinterval [ x2j , x2j+2 ]. The result is:
Z b m−1 Z x2j+2 m −1
a
f ( x )dx = ∑ x2j
f ( x )dx ≈ ∑ S( x2j , x2j+2 )
j =0 j =0
m −1
h
∑
= f ( x2j ) + 4 f ( x2j+1 ) + f ( x2j+2 )
j =0
3
" #
m −1 m −1
h
= f ( x0 ) + 4 ∑ f ( x2j+1 ) + 2 ∑ f ( x2j ) + f ( x2m ) =: Sm ( a, b).
3 j =0 j =1
We will use the the notation Sm ( a, b) for the composite Simpson’s rule on m subintervals.
It is now time to implement the composite Simpson’s method, and see how well it works. Start by
calling the necessary modules etc:
5
[1]: %matplotlib inline
Test if the code is correct. We know that Simpson’s rule has precision 3, thus all third degree
polynomials can be integrated exactly. Choose one such polynomial, find the exact integral, and
compare.
Numerical experiment 1: Apply the code on the integral, and compare with the exact result:
Z 2
(4x3 + x2 + 2x − 1)dx = 18.
−1
6
Numerical experiment 2: We will assume that the error decreases when the number of subinter-
vals m increases. But how much?
Apply the composite method on the integral (again with a known solution):
Z 1 πx 2
cos dx = .
0 2 π
Use the function simpson with m = 1, 2, 4, 8, 16 and see how the error changes with m. Comment
on the result.
[4]: # Numerical experiment 2
def f(x):
return cos(0.5*pi*x)
a, b = 0, 1
I_exact = 2/pi
for m in [1,2,4,8,16]:
S = simpson(f, a, b, m=m) # Numerical solution, using m subintervals
err = I_exact-S # Error
if m == 1:
print('m = {:3d}, error = {:.3e}'.format(m, err))
else:
print('m = {:3d}, error = {:.3e}, reduction factor = {:.3e}'.format(m,␣
,→err, err/err_prev))
err_prev=err
m = 1, error = -1.451e-03
m = 2, error = -8.568e-05, reduction factor = 5.903e-02
m = 4, error = -5.281e-06, reduction factor = 6.164e-02
m = 8, error = -3.289e-07, reduction factor = 6.228e-02
m = 16, error = -2.054e-08, reduction factor = 6.245e-02
From the experiment we observe that the error is reduced by a factor approximately 0.0625 = 1/16
whenever the number of subintervals increases with a factor 2. In the following, we will prove
that this is in fact what can be expected.
First we will find an expression for the error E( a, b) = I ( a, b) − S( a, b) over one interval ( a, b).
This will then be used to find an expression for the composite formula.
Let c = ( a + b)/2 be the midpoint of the interval, and h = (b − a)/2 be the distance between c
and the endpoints a and b. Do a Taylor series expansion of the integrand f around the midpoint,
7
and integrate each term in the series:
Z b Z h
f ( x )dx = f (c + s)ds
a −h
Z h
0 1 2 00 1 3 000 1 4 (4)
= f (c) + s f (c) + s f (c) + s f (c) + s f (c) + · · · ds
−h 2 6 24
h3 h 5 (4)
= 2h f (c) + f 00 (c) + f (c) + · · · .
3 60
Similarly, we compute a Taylor series expansion of the quadrature S( a, b) around c:
h
S( a, b) = ( f (c − h) + 4 f (c) + f (c + h))
3
h 1 1 1
= f (c) − h f 0 (c) + h2 f 00 (c) − h3 f 000 (c) + h4 f (4) (c) + · · ·
3 2 6 24
+ 4 f (c)
1 1 1
+ f (c) + h f 0 (c) + h2 f 00 (c) + h3 f 000 (c) + h5 f (4) (c) + · · ·
2 6 24
h3 h 5 (4)
= 2h f (c) + f 00 (c) + f (c) + · · ·
3 36
The series expansion of the error becomes therefore:
h 5 (4) ( b − a ) 5 (4)
Z b
E( a, b) = f ( x )dx − S( a, b) = − f (c) + · · · = − 5 f (c) + · · · ,
a 90 2 · 90
using h = (b − a)/2.
NB! By choosing to do the Taylor-expansions around the midpoint, every second term disappear
thanks to symmetry. Choosing another point ĉ in the interval will give the same dominant error
term (with c replaced by ĉ), but the calculations will be much more cumbersome.
Usually, we will assume h to be small, such that the first nonzero term in the series dominates the
error, and the rest of the series can be ignored. It is however possible, but not trivial, to prove the
following result:
Theorem: Error in Simpson’s method. Let f ( x ) ∈ C4 [ a, b]. There exist a ξ ∈ ( a, b) such that
( b − a ) 5 (4)
Z b
b−a
b+a
E( a, b) = f ( x )dx − f ( a) + 4 f + f (b) =− f ( ξ ).
a 6 2 2880
NB! : Since p(4) ( x ) = 0 for all p ∈ P3 , this is in agreement with the observation that degree of
precision of Simpson’s rule is equal to 3.
8
We now can use this theorem to find an expression for the error in the composite Simpson’s for-
mula Sm ( a, b):
Z b m−1 Z x2j+2
h
∑
f ( x )dx − Sm ( a, b) = f ( x )dx − f ( x2j ) + 4 f ( x2j+1 ) + f ( x2j+2 )
a j =0 x2j 3
m −1
(2h)5 (4)
= ∑ −
2880
f (ξ j )
j =0
where ξ j ∈ ( x2j , x2j+2 ). We can then use the generalized mean value theorem, see Preliminaries,
section 5, result 2. According to this, there is some ξ ∈ ( a, b) such that
m −1
∑ f (4) ( ξ j ) = m f (4) ( ξ ) .
j =0
By inserting the equality 2mh = b − a, we see that the following theorem has been proved:
Example 4: Find the upper bound for the error when the composite Simpson’s rule is applied to
R1
the integral 0 cos(πx/2)dx.
In this case f (4) ( x ) = (π 4 /16) cos(πx/2), so that | f 4) ( x )| ≤ (π/2)4 . The error bound becomes
4
π4 1
1 1 π 4
| I ( a, b) − Sm ( a, b)| ≤ = .
180 2m 2 46080 m4
If m is increased by a factor 2, the error will be reduced by a factor of 1/16, as indicated by Nu-
merical experiment 2.
Numerical exercise: Include the error bound in the output of Numerical experiment 2, and con-
firm that it really holds.
Remark: The result above shows that the composite Simpson rule with equidistant nodes con-
verges with convergence order 4 (in terms of the node distance h) to the actual integral. That
is, the convergence order is equal to the degree of precision +1. This relation between degree
of precision and convergence order can be shown to hold in general: If a composite quadrature
rule with equidistant nodes is based on a simple quadrature rule, as constructed above, with de-
gree of precision p, then the composite rule will have convergence order p + 1 for all functions
f ∈ C p+1 [ a, b].
9
3.3 Error estimate
From a practical point of view, the error expression derived above has some limitations, the main
difficulty being that it depends on the unknown value f (4) (ξ ). In practice, we can at best use an
error estimate of the form
( b − a ) h 4 (4)
| I ( a, b) − Sm ( a, b)| ≤ k f k∞ .
180
This bound, however, often vastly overestimates the actual error. In addition, we do not always
know (or want to find) k f (4) k∞ . So the question arises: How can we find an estimate of the error,
without any extra analytical calculations?
This is the idea: Let the interval ( a, b) chosen small, such that f (4) ( x ) can be assumed to be almost
constant over the interval. Let H = b − a be the length of the interval. Let S1 ( a, b) and S2 ( a, b)
be the results from Simpson’s formula over one and two subintervals respectively. Further, let
C = − f (4) ( x )/2880 for some x ∈ [ a, b] — which x does not matter, as f (4) is assumed almost
constant anyway. The errors of the two approximations are then given by
I ( a, b) − S1 ( a, b) ≈ CH 5 ,
5
H
I ( a, b) − S2 ( a, b) ≈ 2C .
2
This gives us a computable estimate for the error, both in S1 and S2 . As the error in S2 ( a, b) is
about 1/16 of the error in S1 ( a, b), and we anyway need to compute both, we will use S2 ( a, b) as
our approximation. An even better approximation to the integral is given for free by just adding
the error estimate:
16 1
I ( a, b) ≈ S2 ( a, b) + E2 ( a, b) = S2 ( a, b) − S1 ( a, b).
15 15
R1
Example 5: Find an approximation to the integral 0 cos( x )dx = sin(1) by Simpson’s rule over
one and two subintervals. Find the error estimates Em , m = 1, 2 and compare with the exact error.
Solution:
1
S1 (0, 1) = cos(0.0) + 4 cos(0.5) + cos(1.0) = 0.8417720923,
6
1
S2 (0, 1) = cos(0.0) + 4 cos(0.25) + 2 cos(0.5) + 4 cos(0.75) + cos(1.0) = 0.8414893826.
12
10
The exact error and the error estimate become:
16
E1 (0, 1) = sin(1) − S1 (0, 1) = −3.011 · 10−4 , E1 (0, 1) = (S2 − S1 ) = −3.016 · 10−4 ,
15
1
E2 (0, 1) = sin(1) − S2 (0, 1) = −1.840 · 10−5 , E2 (0, 1) = (S2 − S1 ) = −1.885 · 10−5 .
16
In this case, ther is a very good correspondence between the error estimate and the exact error. An
even better approximation is obtained by adding the error estimate to S2 :
with an error sin(1) − Q = 4.4945 · 10−7 . This gives a lot of additional accuracy without any extra
work.
# The nodes
c = 0.5*(a+b)
d = 0.5*(a+c)
e = 0.5*(c+b)
Test: As a first check of the implementation, use the example above, and make sure that the
results are the same:
11
[6]: # Test of simpson_basic
a, b = 0, 1 # Integration interval
1. Use simpson_basic to find an approximation to the integral over the interval [0, 8]. Print
out S2 (0, 8), the error estimate E2 (0, 8) and the real error E2 (0, 8). How reliable are the error
estimates?
2. Repeat the experiment over the intervals [0, 1] and [4, 8]. Notice the difference between exact
error of the two intervals.
3. Repeat the experiment over the interval [0, 0.1].
This is what you should observe from the experiment:
1. Interval [0, 8]: The error is large, and the error estimate is significantly smaller than the real
error (the error is under-estimated).
2. Interval [0, 1]: As for the interval [0, 8].
3. Interval [4, 8]: Small error, and a reasonable error estimate.
4. Interval [0, 0.1]: Small error, reasonable error estimate.
Why is it so, and how can we deal with it? Obviously, we need small subintervals near x = 0,
while large subintervals are acceptable in the last half of the interval.
12
Explanation: The error in Simpson’s method is given by
( b − a ) 5 (4)
E( a, b) = − f ( ξ ).
2880
1 1280x4 − 160x2 + 1
f (x) = ⇒ f (4) ( x ) = 6144
1 + 16x2 (1 − 16x2 )5
It is no surprise that the error is large and the error estimates fail (we have assumed f (4) almost
constant for the estimates) over the interval [0, 1]. The part of the interval where f (4) ( x ) is large
has to be partitioned in significantly smaller subintervals to get an acceptable result. But how, as
f (4) is in general not known? This is the topic of the next section.
13
3.4 Adaptive integration
Algorithm: Adaptive quadrature. Given f , a, b and a user defined tolerance Tol. * Calculate
Q( a, b) and E ( a, b).
• if |E ( a, b)| ≤ Tol:
– Accept the result, return Q( a, b) + E ( a, b) as an approximation to I ( a, b).
• else:
– Let c = ( a + b)/2, and repeat the process on each of the subintervals [ a, c] and [c, b],
with tolerance Tol/2.
• Sum up the accepted results from each subinterval.
3.4.1 Implementation
The adaptive algorithm is implemented below with simpson_basic as the basic quadrature rou-
tine. The function simpson_adaptive is a recursive function, that is a function that calls itself. To
avoid it to do so infinitely many times, an extra variable level is introduced, this will increase
by one for each time the function calls itself. If level is over some maximum value, the result is
returned, and a warning printed.
# The nodes
c = 0.5*(a+b)
d = 0.5*(a+c)
14
e = 0.5*(c+b)
# -------------------------------------------------
# Write the output, and plot the nodes.
# This part is only for illustration.
if level == 0:
print(' l a b feil_est tol')
print('==============================================')
print('{:2d} {:.6f} {:.6f} {:.2e} {:.2e}'.format(
level, a, b, abs(error_estimate), tol))
x = linspace(a, b, 101)
plot(x, f(x), [a, b], [f(a), f(b)], '.r')
title('The integrand and the subintervals')
# -------------------------------------------------
15
result_left = simpson_adaptive(f, a, c, tol = 0.5*tol, level = level+1)
result_right = simpson_adaptive(f, c, b, tol = 0.5*tol, level = level+1)
result = result_right + result_left
return result
l a b feil_est tol
==============================================
0 0.000000 8.000000 4.25e-02 1.00e-03
1 0.000000 4.000000 1.85e-02 5.00e-04
2 0.000000 2.000000 5.11e-03 2.50e-04
3 0.000000 1.000000 7.84e-04 1.25e-04
4 0.000000 0.500000 6.41e-04 6.25e-05
5 0.000000 0.250000 3.43e-05 3.13e-05
6 0.000000 0.125000 1.21e-06 1.56e-05
6 0.125000 0.250000 1.31e-06 1.56e-05
5 0.250000 0.500000 7.82e-07 3.13e-05
4 0.500000 1.000000 1.45e-05 6.25e-05
3 1.000000 2.000000 1.40e-05 1.25e-04
2 2.000000 4.000000 8.29e-06 2.50e-04
1 4.000000 8.000000 4.33e-06 5.00e-04
16
3.5 Other quadrature formulas
Simpson’s rule is only one example of quadrature rules derived from polynomial interpolations.
There are many others, and the whole process of deriving the methods, do error analysis, develop
error estimates and adaptive algorithms can be repeated.
Let us just conclude with a few other popular classes of methods:
These are based on equidistributed nodes. The simplest choices here — the closed Newton-Cotes
methods — use the nodes xi = a + ih with h = (b − a)/n. Examples of these are the Trapezoidal
rule and Simpson’s rule. The main appeal of these rules is the simple definition of the nodes.
If n is odd, the Newton-Cotes method with n + 1 nodes has degree of precision n; if n is even, it has
degree of precision n + 1. The corresponding convergence order for the composite rule is, as for
all such rules, one larger than the degree of precision, provided that the function f is sufficiently
smooth.
However, for n ≥ 8 negative weights begin to appear in the definitions. This has the undesired
effect that the numerical integral of a positive function can be negative. In addition, this can lead
to cancellation errors in the numerical evaluation, which may result in a lower practical accuracy.
Since the rules with n = 6 and n = 7 yield the same convergence order, this mean that it is mostly
the rules with n ≤ 6 that are used in practice.
The open Newton-Cotes methods, in contrast, use the nodes xi = a + (i + 1/2)h with h = (b −
a)/(n + 1). The simplest example here is the midpoint rule. Here negative weights appear already
17
for n ≥ 2. Thus the midpoint rule is the only such rule that is commonly used in applications.
For the standard interval [−1, 1] choose the nodes as the zeros of the Legendre polynomial Ln of
degree n:
dn
L n ( t ) = n ( t2 − 1) n .
dt
The resulting quadrature rules have a degree of precision d = 2n − 1, and the corresponding
composite rules have a convergence order of 2n. It is possible to show that this is the highest
achievable degree of precision with n nodes.
For n = 1, one obtains the midpoint rule. For n = 2 one obtains the method√discussed near
the beginning
√ of these notes in example 2, which has the nodes x0 = 1/2 + 3/6 and x1 =
1/2 − 3/6 and the corresponding weights w0 = w1 = 1/2.
18