Numerical Integration
Numerical Integration
Session 3
Numerical integration
Summary
Here we discuss some methods that can be used in order to obtain numerical ap-
proximations of integrals that we are not able to compute analytically. The methods
that are discussed here are all related to the familiar idea that integrals correspond
to the area under the curve representative of the function to integrate. We discuss
in particular the rectangle rule (closely related to the so-called Riemann sum and
Riemann integral) and the trapezoidal rule.
i
Contents
ii
Chapter 1
• rectangles, which leads to the so-called rectangle rule and the closely related
Riemann integral, which hence approximates an integral by a sum of areas of
rectangles (such a sum being called a Riemann sum);
• trapezoids, which leads to the so-called trapezoidal rule, which hence approxi-
mates an integral by a sum of areas of trapezoids.
We could of course also think about other, and potentially more precise, geo-
metrical figures, but here we’ll restrict to rectangles and trapezoids, just to see the
1
CHAPTER 1. APPROXIMATION OF INTEGRALS AND. . . 2
general idea.
A common feature of the various methods that can be used to numerically com-
pute an integral is that any such method is an approximation of the actual value of
the integral: this hence means that to any such method is mandatorily associated
a certain error. In view of obtaining a quantitative estimate of such an error, a
particular notation will prove to be particularly useful: the so-called big O notation
O, which we discuss now.
if there exist two constants δ, K > 0 such that, for any x such that 0 < |x − x0 | < δ,
we have
g1 (x)
lim ⩽K, (1.4)
x→x0 g2 (x)
assuming of course that the ratio g1 /g2 is well defined in the neighborhood of x0 .
cases x0 = 0 or x0 = ∞.
A trivial example of functions g1 and g2 that satisfy the above is of course when
they are merely proportional, i.e. g1 (x) = αg2 (x) for any x, with some constant α.
Then (1.3) is trivially satisfied for K = α and for any x.
The big O notation is in particular very useful whenever we deal with approxi-
mations of functions around certain values: the error made by making the approxi-
mation can typically be written as a big O term, as we now discuss on the particular
example of the Taylor series.
where f (n) (x0 ) denotes the n-th derivative of f evaluated at the point x = x0 , i.e.
dn
f (n) (x0 ) ≡ f (x) . (1.6)
dxn x=x0
Now, let’s truncate this series at, say, the first order: by doing this we get an
approximate value of f (x), that is we write
While the full series (1.5) yields an exact value for f (x), the truncated series (1.7)
only gives an approximate value of f (x): by truncating the original series (1.5), we
make a certain error when we write (1.7). The question at this point is thus: what
is the error that we made in this case? Well, the error is here readily obtained, and
it is merely given by all the terms (x − x0 )n for n ⩾ 2 in (1.5). Therefore, if we call
g1 (x) the error that we make when we write (1.7), we have
∞
X f (n) (x0 )
g1 (x) = (x − x0 )n , (1.8)
n=2
n!
CHAPTER 1. APPROXIMATION OF INTEGRALS AND. . . 4
Therefore, let’s now ask the question: can we write the error g1 as a big O of
some other function g2 (x)? The answer is actually yes: setting the function g2 to be
g2 (x) = (x − x0 )2 , (1.10)
Note that we assume that it must be implicitly understood that (1.11) is valid as
x → x0 , so that we didn’t explicitly specify this.
Proof of (1.11): To show that we indeed have (1.11), we must check that the
two functions g1 (x) and g2 (x) defined by (1.8) and (1.10), respectively, satisfy the
definition (1.4) of the big O notation.
Therefore, let’s first compute the ratio g1 /g2 here, we get in view of (1.8)
and (1.10)
∞ ∞
g1 (x) 1 X f (n) (x0 ) n
X f (n) (x0 )
= 2
(x − x 0 ) = (x − x0 )n−2 ,
g2 (x) (x − x0 ) n=2 n! n=2
n!
that is, with a change of index n → n′ = n − 2 in the series in the right-hand side,
∞ ′
g1 (x) X f (n +2) (x0 ) ′
= ′
(x − x0 )n ,
g2 (x) n′ =0 (n + 2)!
and thus, relabeling the dummy index n′ merely as n and isolating the first term of
the series,
∞
g1 (x) f (2) (x0 ) X f (n+2) (x0 )
= + (x − x0 )n . (1.12)
g2 (x) 2 n=1
(n + 2)!
Using now the triangle inequality |α + β| ⩽ |α| + |β|, we get upon taking the
absolute value of (1.12)
∞
g1 (x) f (2) (x0 ) X f (n+2) (x0 )
⩽ + (x − x0 )n . (1.13)
g2 (x) 2 n=1
(n + 2)!
CHAPTER 1. APPROXIMATION OF INTEGRALS AND. . . 5
∞
g1 (x) f (2) (x0 ) X f (n+2) (x0 )
lim ⩽ lim + lim (x − x0 )n . (1.14)
x→x0 g2 (x) x→x0 2 x→x0 (n + 2)!
n=1
Let’s separately compute the two limits in the right-hand side of (1.14): since
f (2) (x0 ) is by construction a constant, we first have
and then we have for the second limit in the right-hand side of (1.14)
∞
X f (n+2) (x0 )
lim (x − x0 )n = 0 ,
x→x0
n=1
(n + 2)!
since each term in the series vanishes for x = x0 . We hence get for (1.14)
which hence readily shows that (1.4) is indeed satisfied if we merely take the positive
constant K to be
f (2) (x0 )
K= . (1.16)
2
Therefore, by definition of the big O notation, the result (1.15) indeed ensures that
we can write (1.11). ■
Therefore, (1.11) shows that the general Taylor series (1.9) can be written as
Actually, more generally, it is easy to show along the same lines as above that the
Taylor series of f can be written as
N
X f (n) (x0 )
(x − x0 )n + O (x − x0 )N +1 , (1.18)
f (x) =
n=0
n!
or else “goes in (x − x0 )2 ”.
In regards with Taylor series, let’s also state here some useful properties of the
big O notation O. We can for instance write
n, n′ ∈ Z .
h ′
i h ′
i
(x − x0 )n O (x − x0 )n = O (x − x0 )n+n , (1.19)
and
for a constant α.
From the above example of the Taylor series of f , and how the error term can
be expressed as a big O of something, it should be clear that the argument of our
notation O when we write
1
Which is also typically what makes mathematicians sometimes/often angry about our way of
doing maths!
Chapter 2
Furthermore, we assume for simplicity that the partition P is regular, which merely
means that the points xj are regularly spaced: that is, we have
xj+1 = xj + ∆x , ∀j , (2.3)
x1 = x0 + ∆x ,
7
CHAPTER 2. GENERAL PROBLEM FOR NUMERICAL. . . 8
xj = x0 + j∆x , ∀j . (2.4)
xN = x0 + N ∆x ,
b = a + N ∆x ,
that is
N ∆x = b − a ,
and thus
b−a
∆x = . (2.5)
N
b
The idea is now to approximate the integral a
dxf (x) by a discrete sum of the
form
b N
X −1
dx f (x) ≈ fj ∆x , (2.6)
a j=0
or, if we also explicitly write the error E that we make when we do such an approx-
imation,
b N
X −1
dx f (x) = fj ∆x + E , (2.7)
a j=0
where the coefficients fj in both (2.6) and (2.7) must be determined. The precise
expression of these coefficients fj , as well as the expression of the error E, will depend
on the particular method that we use in order to write the approximation (2.6). In
the next chapters we discuss two different possible methods.
Chapter 3
• f (xj ), i.e. the value of f at the left point of the subinterval [xj , xj+1 ];
• f (xj+1 ), i.e. the value of f at the right point of the subinterval [xj , xj+1 ];
For concreteness, here we stick to one definite choice, and we take the height of our
rectangle to be the value f (xj ) of f on the left of the interval. We do this for each
subinterval [xj , xj+1 ].
Therefore, let’s now write down the area Aj of our rectangle on the subinterval
[xj , xj+1 ]: we have of course
9
CHAPTER 3. THE RECTANGLE RULE: RIEMANN. . . 10
We would get similar approximations if we choose the other possible choices for the
height of the rectangles that we consider.
The discrete sum that approximates the integral in (3.2) is called a Riemann
sum. A sum of this type (i.e. a sum of areas of rectangles) was indeed used by
Bernhard Riemann in order to build the first rigorous definition of the integral of a
function on an interval [a, b]: this approach to integration hence defines the so-called
Riemann integral.
Since (3.2) is an approximation, we hence now ask the question: what is the
error that we make when we write (3.2)?
that is
b N
X −1 xj+1
dx f (x) = dx f (x) , (3.3)
a j=0 xj
which is exact so far. Now, since we expect the approximation (3.2) to be better the
smaller is ∆x = xj+1 − xj , this suggests to write down a Taylor series of f around
xj for each integral in the right-hand side of (3.3). We have
∞
X f (n) (xj )
f (x) = f (xj ) + (x − xj )n ,
n=1
n!
CHAPTER 3. THE RECTANGLE RULE: RIEMANN. . . 11
xj+1
" ∞
#
X f (n) (xj )
= dx f (xj ) + (x − xj )n
xj n=1
n!
xj+1 ∞ xj+1
X f (n) (xj )
= dx f (xj ) + dx (x − xj )n
xj n=1
n! xj
∞ xj+1
f (n) (xj )
X 1
= f (xj ) [x]xxj+1 + (x − xj ) n+1
j
n=1
n! n + 1 xj
∞
X f (n) (xj ) 1
(xj+1 − xj )n+1 − (xj − xj )n+1 ,
= f (xj )(xj+1 − xj ) +
n=1
n! n+1
b N −1
" ∞
#
X X f (n) (xj )
dx f (x) = f (xj )∆x + ∆xn+1 ,
a j=0 n=1
(n + 1)!
that is
b N
X −1
dx f (x) = f (xj )∆x + E , (3.5)
a j=0
where E is defined by
N −1 X
∞
X f (n) (xj ) n+1
E≡ ∆x . (3.6)
j=0 n=1
(n + 1)!
b
Comparing the approximation (3.2) of a dxf (x) with its exact expression (3.5)
hence readily shows that the quantity E defined by (3.6) is nothing but the exact
expression of the error that we make when we write the approximation (3.2).
While (3.6) gives the exact expression of the error, let’s now see if we can get a
rough idea of what it looks like, namely how it scales with ∆x, that is in other words
CHAPTER 3. THE RECTANGLE RULE: RIEMANN. . . 12
what is the leading order in ∆x of E. To do this, let’s use the big O notation O
that we introduced in chapter 1 above. First, in view of what we did in section 1.3
regarding how we can write the remaining of a Taylor series as a O of the leading
order term [see in particular (1.8) and (1.11)], here we can readily write something
similar for the series (with respect to the index n) in the right-hand side of (3.6).
Indeed, since this series has ∆x2 as its leading order, we can write
∞
X f (n) (xj )
∆xn+1 = O ∆x2 . (3.7)
n=1
(n + 1)!
Therefore, the leading order of each individual term in the sum over j in (3.6) is
something proportional to ∆x2 : combining (3.6) with (3.7) hence allows us to write
[by treating O(∆x2 ) as something independent of the summation index j]
N −1 X
∞ N −1 −1
X f (n) (xj ) n+1 X 2 2
N
X
1 = N O ∆x2 ,
E= ∆x = O ∆x = O ∆x
j=0 n=1
(n + 1)! j=0 j=0
and here we must be careful to not naively apply the rule (1.21) because here the
factor N actually depends on ∆x in view of (2.5), namely N = (b − a)/∆x, so that
we get
b−a
O ∆x2 ,
E=
∆x
where we can now apply the rules (1.19) and (1.21) to get
b−a
O ∆x2 = (b − a) (∆x)−1 O ∆x2 = (b − a)O (∆x) = O (∆x) ,
E=
∆x
and thus
E = O (∆x) . (3.8)
Therefore, the error E that we make when we make the approximation (3.2) scales
in ∆x: this means that the smaller we choose our ∆x, the better becomes our
approximation (3.2).
and division: so-called round-off errors can quickly build up and propagate.
1
= f (xj )∆x + [f (xj+1 ) − f (xj )] ∆x
2
1 1
= f (xj ) + f (xj+1 ) − f (xj ) ∆x ,
2 2
that is
1
Aj = [f (xj ) + f (xj+1 )] ∆x , ∀j . (4.1)
2
b
Therefore, the integral a dxf (x) is here approximated by the sum of the areas Aj
of all these trapezoids, and we have
b N −1 N −1
X X f (xj ) + f (xj+1 )
dx f (x) ≈ Aj = ∆x . (4.2)
a j=0 j=0
2
b
This approximation (4.2) of the integral a dxf (x) is called the trapezoidal rule.
We can show that the error E that we make when we make the approxima-
14
CHAPTER 4. THE TRAPEZOIDAL RULE 15
E = O(∆x2 ) . (4.3)
Comparing (4.3) with (3.8) hence readily shows that the error in the case of the
trapezoidal rule is now one order of magnitude smaller as compared to the error
made when using the rectangle rule: namely, the error is of order ∆x2 for the
trapezoidal rule rather than ∆x for the rectangle rule.