Numerical Differentiation and Integration
Numerical Differentiation and Integration
1
1 Numerical Differentiation
Recall that the derivative of the function f at x0 is
f (x0 + h) − f (x0 )
f 0 (x0 ) = lim .
h→0 h
This formula gives a way of approximating the derivative of the function f (x) at
x = x0 . We just compute
f (x0 + h) − f (x0 )
h
for a small value of h. However, we do not have any information about the quality of
the approximation. In other words, we want to compute the truncation error. To this
end, let f ∈ C2 [a, b], x0 , x1 ∈ [a, b], and h = x1 − x0 . To get an idea of the truncation
error in approximating f 0 (x0 ) by
f (x0 + h) − f (x0 )
,
h
we consider a Lagrange polynomial of degree 1 for the function f determined by two
points x0 and x1 with its error term
f (x0 + h) − f (x0 ) h 00
f 0 (x0 ) = − f (ξ(x0 )).
h 2
Hence
0
f (x0 ) − f (x0 + h) − f (x0 ) ≤ M h,
h 2
where M is a bound on | f 00 (x)| for x ∈ [a, b]. The formula
f (x0 + h) − f (x0 )
f 0 (x0 ) ≈
h
2
is known as the forward difference formula if h > 0 and backward difference formula
if h < 0.
Let F( f , h)(x) be a finite difference approximation of f (p) (x) using step-size h
at x, where f (p) (x) is the pth derivative of a real-valued function f . We assume that
f is sufficiently smooth function. We call the approximation F( f , h) to be of oder n
accurate if
|F( f , h)(x) − f (p) (x)| ≤ O(hn ).
1.1 Central Difference Formula for the First and Second Deriva-
tives
The central difference formula to approximate f 0 (x0 ) can also be derived as follows.
Let us expand f in terms of a second order Taylor polynomial and evaluate at x0 + h
and x0 − h:
h2 00 h3
f (x0 + h) = f (x0 ) + h f 0 (x0 ) + f (x0 ) + f (3) (ξ),
2! 6
and
h2 00 h3
f (x0 − h) = f (x0 ) − h f 0 (x0 ) +
f (x0 ) − f (3) (η),
2! 6
where x0 − h < η < x0 < ξ < x0 + h. Subtracting the second equation from the first
one, we obtain
h3 (3) h3
f (x0 + h) − f (x0 − h) = 2h f 0 (x0 ) +f (ξ) + f (3) (η).
6 6
h i
Assume that f (3) is continuous on [x0 − h, x0 + h]. Since 12 f (3) (ξ) + f (3) (η) is
between f (3) (ξ) and f (3) (η), the Intermediate Value Theorem implies that a number
κ exists between η and ξ and hence in (x0 − h, x0 + h) with
1 h (3) i
f (3) (κ) = f (ξ) + f (3) (η) .
2
Hence we have the central difference formula:
1 h2
f 0 (x0 ) = [ f (x0 + h) − f (x0 − h)] − f (3) (κ),
2h 6
where κ lies between x0 − h and x0 + h.
We present a method to approximate the second derivative of a function by using
its tabulated values. For this purpose, we expand the function in a third order Taylor
polynomial about a point x0 and evaluate at x0 + h and x0 − h. Then
h2 00 h3 h4
f (x0 + h) = f (x0 ) + h f 0 (x0 ) + f (x0 ) + f 000 (x0 ) + f (4) (ξ),
2! 3! 24
3
and
h2 00 h3 h4
f (x0 ) − f 000 (x0 ) + f (4) (η),
f (x0 − h) = f (x0 ) − h f 0 (x0 ) +
2! 3! 24
where x0 − h < η < x0 < ξ < x0 + h. We now cancel terms involving f 0 (x0 ) and
f 000 (x0 ) by adding these equations:
h4 h (4) i
f (x0 + h) + f (x0 − h) = 2 f (x0 ) + f 00 (x0 )h2 + f (ξ) + f (4) (η) .
24
Solving this equation for f 00 (x0 ) yields
00 1 h2 h (4) (4)
i
f (x0 ) = 2 [ f (x0 + h) − 2 f (x0 ) + f (x0 − h)] − f (ξ) + f (η) .
h 24
h i
Assume that f (4) is continuous on [x0 − h, x0 + h]. Since 12 f (4) (ξ) + f (4) (η) is
between f (4) (ξ) and f (4) (η), the Intermediate Value Theorem implies that a number
κ exists between η and ξ and hence in (x0 − h, x0 + h) with
1 h (4) i
f (4) (κ) = f (ξ) + f (4) (η) .
2
Hence the formula for f 00 (x0 ) is
1 h2 (4)
f 00 (x0 ) = [ f (x0 + h) − 2 f (x0 ) + f (x0 − h)] − f (κ)
h2 12
for some κ ∈ (x0 − h, x0 + h). We have to assume the continuity of f (4) on (x0 −
h, x0 + h) to get its boundedness there.
4
2 Numerical Integration
Sometimes we have to compute the definite integral of a function which does not have
an explicit anti-derivative or whose anti-derivative is not
Rb
easy to obtain. The method
of approximating the definite integral of a function a f (x) dx is called numerical
quadrature or simply quadrature. A numerical quadrature uses a sum ∑ni=0 wi f (xi )
to approximate the definite integral ab f (x).
R
Thus Z b n Z b
n
f (x) dx ≈ ∑ li (x) dx f (xi ) = ∑ wi f (xi ),
a i=0 a i=0
where Z b
wi = li (x) dx, i = 0, · · · , n.
a
f (n+1) (ξ(x))
f (x) = pn (x) + πn+1 (x),
(n + 1)!
5
The error term En ( f ) in the quadrature formula
Z b n
f (x) dx ≈ ∑ wi f (xi )
a i=0
is given by
Z b
1
En ( f ) = πn+1 (x) f (n+1) (ξ(x)) dx.
(n + 1)! a
There are two types of Newton-Cotes formulas. The closed ones and the open
ones. The closed formulas use the function value at all points, and the open formulas
do not use the function values at the endpoints.
The wi ’s are called weights or coefficients and are derived from Lagrange basis func-
tions. The weights wi ’s are then given by
Z b
wi = li (x) dx, i = 0, 1, 2, · · · , n,
a
The interpolating polynomial pn (x) for the set of points ∆ = {x0 , · · · , xn } can be
written as
n
pn (x) = ∑ li (x) f (xi ).
i=0
The integral is thus approximated by integrating pn (x) instead of the function f .
We now consider two examples.
6
Trapezoidal Rule
Z b
(b − a)
f (x) dx ≈ ( f (a) + f (b)).
a 2
Simpson’s Rule
Let c = (a + b)/2.
Z b
(b − a)
f (x) dx ≈ ( f (a) + 4 f (c) + f (b)).
a 6
Thus Z 1
2
δi j .
Pi (x)Pj (x) dx =
−1 2i + 1
These polynomials can be computed by using the Gram-Schmidt orthognalisation
process. For example, we start from P0 (x) = 1, and let P1 (x) = x + a. Then a is
determined by the equation
Z 1 Z 1
P0 (x)P1 (x) dx = 0, or (x + a) dx = 0, which gives a = 0.
−1 −1
7
which give
1
c = 0, d=− .
and
3
We can prceed to compute P3 (x), P4 (x), · · · , in a similar way. The first few monic
Legendre polynomials are
1 3 6 3
P0 (x) = 1, P1 (x) = x, P2 (x) = x2 − , P3 (x) = x3 − x, P4 (x) = x4 − x2 + .
3 5 7 35
Theorem 1. Suppose that {x1 , · · · , xn } are the roots of the nth Legendre polynomial
Pn (x) and that for each i = 1, 2, · · · , n, the numbers ci are defined by
Z 1 n
x−xj
ci = ∏ dx.
−1 j=1 xi − x j
j6=i
Proof. We first consider a situation for a polynomial P(x) of degree less than n. We
rewrite P(x) as the Lagrange interpolating polynomial of degree n − 1 with nodes at
the roots of the nth Legendre polynomial Pn (x). Thus
n nx−xj
P(x) = ∑ ∏ P(xi ),
i=1 j=1 xi − x j
j6=i
and thus Z 1 n Z 1 n
x−xj
P(x) dx = ∑ ci P(xi ) with ci = ∏ dx.
−1 i=1 −1 j=1 xi − x j
j6=i
If the polynomial P(x) is of degree at least n but less than 2n, we can divide this by
the nth Legendre polynomial Pn (x) to obtain
where R(x) is the remainder polynomial and is of degree less than n, and Q(x) is the
quotient polynomial and is also of degree less than n. Since xi is a root of Pn (x) for
each i = 1, 2, · · · , n, we have
8
Using the property of nth Legendre polynomial Pn (x), we have
Z 1 Z 1 Z 1 Z 1
P(x) dx = Q(x)Pn (x) dx + R(x) dx = R(x) dx.
−1 −1 −1 −1
Thus Z 1 n
P(x) dx = ∑ ci P(xi ).
−1 i=1
Using the linear mapping y = 12 (b − a)x + 12 (a + b) from [−1, 1] to [a, b], we have
y = a when x = −1 and y = b when x = 1, and dy = 12 (b − a) dx. This transforms the
numerical integration over the interval [−1, 1] to [a, b]. Thus the Gaussian quadrature
with two points over [a, b] becomes
Z b
b−a
f (x) dx ≈ [ f (x1 ) + f (x2 )] ,
a 2
where
1 1 1 1 1 1
x1 = − (b − a) √ + (a + b) and x2 = (b − a) √ + (a + b).
2 3 2 2 3 2
9
2.4 Two-Dimensional Integration
The idea of two-dimensional integration is the same as one-dimensional case. We
approximate the integral as
Z n
f (x, y) dxdy ≈ ∑ wi f (xi , yi ).
Ω i=1
We can apply the idea of iterated integral to compute the approximation. Thus
Z bZ b Z b n n n n n
g(x, y) dx dy ≈ ∑ wig(xi, y) dy ≈ ∑ w j ∑ wig(xi, y j ) = ∑ ∑ w j wig(xi, y j ).
a a a i=1 j=1 i=1 j=1 i=1
This is the tensor product of the above quadrature. The two-dimensional integra-
tion on a square is obtained by using the tensor product structure from the one-
dimensional case. For example, the Trapezoidal rule over the rectangle [a, b] × [c, d]
in the two-dimensional case becomes
Z dZ b
(b − a)(c − d)
f (x, y) dxdy ≈ [ f (a, c) + f (a, d) + f (b, c) + f (b, d)] .
c a 4
The Simpson’s rule over the rectangle [a, b] × [c, d] in the two-dimensional case be-
comes
Z dZ b
(b − a)(c − d)
f (x, y) dxdy ≈ [ f (a, c) + f (a, d) + f (b, c) + f (b, d) +
c a 36
4[ f (x1 , c) + f (x1 , d) + f (a, y1 ) + f (b, y1 )] + 16 f (x1 , y1 )].
10
Remark 1. The tensor product integration formulas are not optimal in the sense of
using a fewer number quadrature points. For example, a quintic polynomial has 21
monomials in two dimensions and using a non-tensor product formula of the form
Z 1Z 1 n
f (x, y)dx dy ≈ ∑ wi f (xi , yi )
−1 −1 i=1
will be exact using only n = 7 points. We need to use at least 9 points in tensor prod-
uct Gaussian quadrature rule. Non-tensor-product formulas are not easy to derive.
Quadrature rules are generally obtained by the method of undetermined coefficients
as illustrated below for triangles.
11
which can integrate a quadratic function exactly.
The two important rules for numerical integration over a triangle K are
1. The three point rule with vertices
|K|
Z
φ dx ≈ (φ(p1 ) + φ(p2 ) + φ(p3 )) .
K 3
This rule gives the exact result for a polynomial of degree one.
Here n is the number of points, p is the degree of accuracy, (xi , yi ) the nodes and wi
the weights of the quadrature rule
12