Applied Numerical Analysis (AE2220-I) : R. Klees and R.P. Dwight
Applied Numerical Analysis (AE2220-I) : R. Klees and R.P. Dwight
February 2018
2
Contents
3 Polynomial Interpolation in 1d 21
3.1 The Monomial Basis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
3.2 Why interpolation with polynomials? . . . . . . . . . . . . . . . . . . . . . . . 26
3.3 Newton polynomial basis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
3.4 Lagrange polynomial basis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
3.5 Interpolation Error . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
3.5.1 Chebychev polynomials . . . . . . . . . . . . . . . . . . . . . . . . . . 34
3
4 CONTENTS
5 Least-squares Regression 63
5.1 Least-squares basis functions . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
5.2 Least-squares approximation - Example . . . . . . . . . . . . . . . . . . . . . 65
5.3 Least-squares approximation - The general case . . . . . . . . . . . . . . . . . 67
5.4 Weighted least-squares (?? not examined) . . . . . . . . . . . . . . . . . . . . 71
6 Numerical Differentiation 73
6.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
6.2 Numerical differentiation using Taylor series . . . . . . . . . . . . . . . . . . . 73
6.2.1 Approximation of derivatives of 2nd degree . . . . . . . . . . . . . . . 79
6.2.2 Balancing truncation error and rounding error . . . . . . . . . . . . . 81
6.3 Richardson extrapolation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
6.4 Difference formulae from interpolating polynomials (?? - not examined) . . . 83
7 Numerical Integration 89
7.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
7.2 Solving for quadrature weights . . . . . . . . . . . . . . . . . . . . . . . . . . 91
7.3 Numerical integration error – Main results . . . . . . . . . . . . . . . . . . . . 93
7.4 Newton-Cotes formulas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
7.4.1 Closed Newton-Cotes (s=2) – Trapezoidal rule . . . . . . . . . . . . . 95
7.4.2 Closed Newton-Cotes (s=3) – Simpson’s rule . . . . . . . . . . . . . . 96
7.4.3 Closed Newton-Cotes (s=4) – Simpson’s 3/8-rule . . . . . . . . . . . . 96
7.4.4 Closed Newton-Cotes (s=5) – Boules’s rule . . . . . . . . . . . . . . . 97
7.4.5 Open Newton-Cotes Rules . . . . . . . . . . . . . . . . . . . . . . . . . 97
7.5 Composite Newton-Cotes formulas . . . . . . . . . . . . . . . . . . . . . . . . 98
7.5.1 Composite mid-point rule . . . . . . . . . . . . . . . . . . . . . . . . . 99
7.5.2 Composite trapezoidal rule . . . . . . . . . . . . . . . . . . . . . . . . 99
7.6 Interval transformation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
7.7 Gauss quadrature (?? – not examined) . . . . . . . . . . . . . . . . . . . . . . 101
7.8 Numerical integration error – Details (?? – not examined) . . . . . . . . . . . 105
7.9 Two-dimensional integration . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
7.9.1 Cartesian products and product rules . . . . . . . . . . . . . . . . . . 113
7.9.2 Some remarks on 2D-interpolatory formulas . . . . . . . . . . . . . . . 116
Bibliography 161
6 CONTENTS
Chapter 1
Preliminaries: Motivation,
Computer arithmetic, Taylor series
n−1
X f (xi + h) + f (xi )
In = h· ,
2
i=0
iπ
xi = ,
n
π
h=
n
where f (x) = x sin x, the integrand. The xi define the edges of the subintervals, and h the
width of each subinterval. The accuracy of the approximation In ≈ I˜ = 3.14159265359 · · ·
depends on n:
1
2 Chapter 1. Preliminaries: Motivation, Computer arithmetic, Taylor series
n In ˜
= |In − I|
10 3.11 2.6 × 10−2
100 3.1413 2.6 × 10−4
1000 3.141590 2.6 × 10−6
10000 3.14159262 2.6 × 10−8
We want efficient methods, where the error → 0 rapidly as n → ∞. It is often the case
that evaluation of f (x) is expensive, and then using n = 10000 might not be practical. In
the above as n is increased by a factor of 10, h is reduced by a factor of 10, but the error
is reduced by 102 = 100. Because of the exponent 2 the method is said to be 2nd-order
accurate.
In the above integral, an analytic solution was possible. Now what about:
Z πp
˜
I= 1 + cos2 x dx?
0
With conventional analysis there exists no closed-form solution. With numerical analysis the
procedure is exactly the same as before! In engineering practice integrands are substantially
more complicated than this, and may have no closed-form expression themselves.
the computer will have consequences for the accuracy of the numerical methods we develop
in this course.
1.2.1 Integers
To represent integers we use a binary number system. Assume we have N bits
b = (b0 , b1 , · · · , bN −1 )
• Overflow - trying to represent a number larger than zmax . E.g. typically for unsigned
32-bit integers (232 − 1) + 1 → 0, for signed 32-bit integers (231 − 1) + 1 → −231 ,
depending on the exact system.
E.g. with 32-bits, 1 bit to represent the sign, and a interval h of 1 × 10−4 , we can repre-
sent numbers between ±231 · 1 × 10−4 ≈ ±200000 with a resolution of 0.0001. This range
and accuracy is obviously very limited. It is used primarily on embedded systems, for e.g.
video/audio decoding where accuracy is not critical. Possible errors:
P10
• Accumulation of rounding error - e.g. in the above system i=1 0.00011 gives
0.0010, rather than the exact 0.0011.
A real-life example of the last error is failures in the Patriot missile system. A 24-bit
1
fixed-point number contained the current time in seconds, which was incremented every 10 th
1
of a second. Key point - 10 = 0.0001100110011... has an non-terminating expansion in binary,
which was truncated after the 24th bit. So each increment we make an error of 9.5 × 10−8 s.
After 100 hours cumulative error is 100 × 60 × 60 × 10 × 9.5 × 10−8 s = 0.34 s - in which time
a target missile travels ≈ 0.5km. Quick fix: reboot every few days.
x = s × be .
In particular:
For example, 5-digit mantissa, b = 10, −8 ≤ e ≤ 8. Then 10 · π = 3.1416 × 101 . The system
as described does not contain zero, this is added explicitly. Negative numbers are defined
using a sign bit, as for integers. The resulting sampling of the real-number line is shown in
Figure 1.2.
Possible errors:
• Overflow - trying to represent a number larger/smaller than ±smax × bemax . For ex-
ample 9.9999 × 108 + 0.0001 = inf. Special value inf.
• Underflow - trying to represent a number closer to zero than 1 × bemin . For example
2
1. × 10−8 = 0.
Floating-point concept
Decimal system with 5-digit manitssa (sign = +1)
0.01 0.1 1 10
1.0001
0.099999 0.99999 9.9999 99.999
x
Machine ε
0
e=-2 e=-1 e=0 e=1
0.01 0.1 1 10
1.0001
0.099999 0.99999 9.9999 99.999
log x
IEEE754 is a technical standard for floating-point arithmetic, defining not only the rep-
resentation (see Figure 1.3), but also rounding behaviour under arithmetic operations.
• 64-bits, b = 2, 11-bit exponent, giving emax = 1024, emin = −1023, 53-bit mantissa
(including 1 sign bit). This corresponds approximately to a decimal exponent between
53
− and 308 (since 10308 ≈ 21024 ), and about 16 decimal-digits (since 21 ≈ 1.1×10−16 .
0 0 1 1 1 1 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 = 0.15625
we first approximate f (x) by a polynomial p(x), and then posit that the integral of p(x) will
be an approximation of the integral of f (x):
Z 1 Z 1
f (x) ≈ p(x), ∀x ∈ [0, 1] ⇒ f (x) dx ≈ p(x) dx.
0 0
One simple polynomial approximation that comes from basic calculus is Taylor’s theorem:
Theorem 1.1 (Taylor’s theorem with Lagrange remainder)
Let f (x) be a f : R → R which is N + 1-times continuously differentiable on the interval
[x0 , x] (the first N + 1 derivatives exist and are continuous). Then the Taylor expansion of
f (x) about x0 is
N
X f (n) (x0 )
f (x) = (x − x0 )n + O(x − x0 )N +1 .
n!
n=0
For small (x − x0 ) we expect the last term, the truncation error to be small, and therefore
the sum to be a good approximation to f (x). Note that the sum contains only powers of x -
it is therefore a polynomial in x.
Furthermore we can write the truncation error in a specific form: there exists a ξ ∈ [x0 , x]
such that
N
X f (n) (x0 ) f (N +1) (ξ)
f (x) = (x − x0 )n + (x − x0 )N +1 . (1.2)
n! (N + 1)!
n=0
This is the Lagrange form of the remainder, and a generalization of the Mean-Value Theorem.
It is important as it gives us an estimate for the error in the expansion, though in practice
we never know ξ.
1.3. Taylor Series Review 7
h = x − x0 ,
given which
N
X f (n) (x0 ) f (N +1) (ξ) N +1
f (x0 + h) = hn + h . (1.3)
n! (N + 1)!
n=0
and the series is a good approximation for small h. The Taylor series including terms up to
and including hN is called an N th-order Taylor expansion, or alternatively an (N + 1)th-term
Taylor expansion.
Example 1.2
Expand f (x) = cos(x) about x0 = 0 in an 4th-order Taylor expansion, plus remainder.
cos(0) = 1
cos0 (0) = − sin(0) = 0
cos00 (0) = − cos(0) = −1
cos000 (0) = sin(0) = 0
cos(4) (0) = cos(0) = 1
cos(5) (ξ) = − sin(ξ)
x2 x4 x5
cos(x) = 1 − + − sin(ξ)
2! 4! 5!
Example 1.3
Consider the case of expanding a polynomial f (x) = ax4 + bx3 + cx2 + dx + e as a Taylor
series about 0:
1.5
Taylor 1-term: p(x) =1 1.5
Taylor 2-term: p(x) =1−x2!2 1.5
Taylor 3-term: p(x) =1−x2!2 + x4!4
f(x)
p(x)
1.0 1.0 1.0
then
f (0) = 0!e
f 0 (0) = 1!d
f 00 (0) = 2!c
f 000 (0) = 3!b
f 0000 (0) = 4!a
0000
and rearranging each term shows a = f 4!(0) etc. So an N -term Taylor series reproduces
polynomials exactly, given sufficient terms. If you forget Taylor’s expansion, you can derive
it like this. Also for a general function f (x), the Taylor series selects the polynomial with
the same derivatives as f (x) at x0 .
We never know ξ. The important consequence of this remainder is that the rate at which
the error goes to zero as h → 0 is known: ∝ hN +1 . Using big-O notation = O(hN +1 ).
We do not know the exact error (if we did we would know the exact value of the function at
x), but we know how quickly it gets smaller, and that we can make it as small as we like by
reducing h.
Exercise 1.4
Is the condition on differentiability of f (x) important? Is the theorem still true if f (x) is not
continuously differentiable? Consider the case f (x) = |x| expanded about x = 1.
1.3. Taylor Series Review 9
1.0000000099999999 − 1.
f 0 (0) ≈ = 0.99999999392252903.
1 × 10−8
10 Chapter 1. Preliminaries: Motivation, Computer arithmetic, Taylor series
Chapter 2
then we can write down the solution of f (x) = 0 if the polynomial is of degree one or two.
It is also possible to obtain the zeros of a cubic polynomial but the formula is already very
complicated and hardly used in practice. For degree 5 and higher it can be shown that no
such formula exists. Then, we have to resort to numerical approximation techniques. For
functions f (x) which are not polynomial in nature, numerical techniques are virtually the
only approach to find the solution of the equation f (x) = 0. For instance, some elementary
mathematical functions such as the square root and the reciprocal are evaluated on any
√
computer or calculator by an equation-solving approach: a is computed by solving the
2 1 1
equation x − a = 0 and a is computed by solving x − a = 0.
The algorithms we are going to study are all iterative in nature. That means we start our
solution process with a guess at the solution being sought and then refine this guess following
specific rules. In that way, a sequence of estimates of the solution is generated x0 , x1 , · · · xN
which should converge to the true solution x̃, meaning that
lim xN = x̃,
N →∞
where f (x̃) = 0. However this is guaranteed only under certain conditions, depending on the
algorithm.
11
12 Chapter 2. Iterative Solution of Non-linear Equations
Therefore simply describing the algorithm, and thereby the sequence, is not sufficient.
We wish to know in advance:
3. How rapidly the algorithm converges (the rate at which the error in xN decreases).
f (x) := x3 − x − 1 = 0, (2.1)
starting from the interval [1, 2]. We first note that f (1) = −1 and f (2) = 5, and since f (x) is
continuous on [1, 2], it must vanish somewhere in the interval [1, 2] by the intermediate value
theorem for continuous functions. Now, we take the midpoint of the interval [1, 2] as the
initial guess of the zero, i.e. x0 = 1.5. The error in that guess 0 := |x0 − x̃| is at most half of
the length of the interval, i.e. ≤ 0.5. Since f (1.5) = 0.875 > 0 the zero must lye in the smaller
interval [1, 1.5]. Again, we take the midpoint of that interval as the next guess of the solution,
i.e. x1 = 1.25, and the error of that guess is ≤ 0.25. We obtain f (1.25) = −0.296 < 0, thus
a yet smaller interval where the solution must lie is [1.25, 1.5], and the next guess of the
solution is x2 = 1.375; it has an error ≤ 0.125. See Figure 2.1.
2.1. Recursive Bisection 13
It is easy to prove that for any continuous function f (·) and initial end points satisfying
f (xleft ) · f (xright ) < 0 the sequence of midpoints generated by the bisection method converges
to the solution of f (x) = 0. Obviously, the midpoint of the corresponding interval differs
from the solution by at most half the length of the interval. This gives a simple expression
for the upper-bound on the error.
Let the error in the root after the ith iteration be
i := |xi − x̃|.
On the first iteration we know that 0 ≤ (b − a)/2, and for every subsequent iteration the
interval size halves, so
b−a
N ≤ EN = N +1 ,
2
where EN is the upper-bound on the error in xN . Note that
1
EN +1 = EN ,
2
so the error at each iteration is reduced by a constant factor of 0.5. This is an example of a
linear rate of convergence. The name “linear” originates from the convergence curve when
plotted on an iteration-log error graph, see Figure 2.2.
We are interested in the rate of convergence because of the common case where f (x) is
extremely expensive to evaluate. In N iterations of recursive bisection f (·) must be evaluated
N + 2 times.
14 Chapter 2. Iterative Solution of Non-linear Equations
Figure 2.2: Convergence of recursive bisection (linear – left) and Newton (quadratic –
right).
f : Rn → Rn .
The fixed-point iteration and Newton methods in the following can be generalized.
Exercise 2.3
Consider applying the recursive bisection method to a continuous function f (x) with multiple
roots in the initial interval.
1. What restrictions exist on the number of roots in [a, b] given that f is continuous and
f (a) < 0, f (b) > 0?
2. If a continuous curve has 3 roots in the interval [a, b], can recursive bisection converge
to the middle root?
Now consider the case that the function is not continuous - consider for example f (x) = 1/x
on the initial interval [−1, 2]. Does recursive bisection converge? Does it converge to a root?
f (x) = 0 (2.2)
Provided that the two equations (2.2), (2.3) are equivalent, i.e. x = ϕ(x) ⇔ f (x) = 0, it
follows that any solution of the original equation (2.2) is a solution of the second equation.
Then, an initial guess x0 generates a sequence of estimates of the fixed point by setting
xn = ϕ(xn−1 ), n = 1, 2, . . . . (2.4)
Clearly if the exact solution x̃ is achieved on iteration n, xn = x̃, then xn+1 = x̃. The exact
solution is preserved, it is a fixed point of the iteration. See Figure 2.3. The algorithm can
be summarized as follows:
Algorithm 2.4 (Fixed-Point Iteration)
Let the initial guess be x0 . Then the following performs a fixed-point iteration:
for i = [0 : Nmax ] do
xi+1 ← ϕ(xi )
end for
return xi+1
Note that the choice of ϕ(·) is not unique. As before, the questions we have to answer are
(a) whether the iteration scheme converges, and if so (b) how the error behaves. Both these
depend on the choice of ϕ(·).
• x = x3 − 1 =: ϕ(x)
• x = (x + 1)1/3 =: ϕ(x)
16 Chapter 2. Iterative Solution of Non-linear Equations
1 1
• x= x + x2
=: ϕ(x), x 6= 0
1
• x= x2 −1
=: ϕ(x), x 6= {1, −1}
Let us take ϕ(x) = (x + 1)1/3 . As starting value we use x0 = 1.5. Then, following 2.4 we
obtain the scheme
xn = (xn−1 + 1)1/3 , n = 1, 2, . . . , (2.5)
which generates the following values for x1 , x2 , x3 , and x4 :
xn = x3n−1 − 1, n = 1, 2, . . . , (2.6)
Obviously, the latter sequence does not converge, since the solution of the given equation is
1.3247179 . . . . However, the first iteration scheme seems to converge, because x4 = 1.3249 is
a much better approximation than the starting value x0 = 1.5.
The example shows that it is necessary to investigate under what conditions the iteration
scheme will converge and how fast the convergence will be. First we need some calculus:
f (b) − f (a)
f 0 (ξ) = .
b−a
Note that this is a special case of Taylors theorem with Lagrange remainder. Visually the
mean-value theorem is easy to verify, see Figure 2.4.
We now investigate the convergence of a fixed-point iteration. Let x̃ be the exact root of
f (x), and xi the current approximation. Now we can derive a relationship between the error
in xi and xi+1 as follows:
xi+1 = ϕ(xi )
xi+1 − x̃ = ϕ(xi ) − x̃
xi+1 − x̃ = ϕ(xi ) − ϕ(x̃)
ϕ(xi ) − ϕ(x̃)
xi+1 − x̃ = (xi − x̃)
xi − x̃
xi+1 − x̃ = ϕ0 (ξi )(xi − x̃),
2.2. Fixed-point iteration 17
where the mean-value theorem has been applied at the last stage. Note that the ξ in the
MVT will be different at each iteration, hence ξi . By defining the error i := |xi − x̃|, we have
If we want the error to strictly drop at iteration i, then we require |ϕ0 (ξi )| < 1. Otherwise
error grows (divergence). If −1 < ϕ0 (ξ) < 0 the error oscillates around the root, as we
observed in Figure 2.3.
Assume that |ϕ0 (ξi )| < K < 1, ∀i. Then we have an error bound after n iterations:
Again we have linear convergence - error is reduced by a constant factor at each iteration.
Assume that f is two times continuously differentiable in [a, b] and that it has a simple
zero x̃ ∈ [a, b], i.e. f (x̃) = 0 and f 0 (x̃) 6= 0. The Taylor series expansion of f at x0 is
(x̃ − x0 )2
f (x̃) = f (x0 ) + f 0 (x0 )(x̃ − x0 ) + f 00 (ξ) = 0, ξ ∈ [x0 , x̃].
2
If we are close to the solution, then |x̃ − x0 | is small, and we can neglect the remainder term.
By neglecting the remainder we are invoking the linear approximation. We obtain
Stability and Convergence Again we ask the questions: (a) does the iteration converge?,
and (b) how rapidly? To answer (a) we observe that
f (x)f 00 (x)
ϕ0 (x) = .
(f 0 (x))2
So, ϕ0 (x̃) = 0. Therefore, it follows that (provided ϕ0 is continuous, which it is if f is twice
continuously differentiable) |ϕ0 (x)| < 1 for all x in some neighbourhood of the solution x̃.
Therefore, Newton’s method will converge provided that the initial guess x0 is “close enough”
to x̃. The convergence result demands a starting point x0 which may need to be very close
to the solution we look for. This is called local convergence. Such a starting point can often
be found by first using several iterations of a FPI in the hope that a suitably small interval
is obtained.
Something special happens in the convergence rate of Newton – which makes it unique.
As before, let x̃ be a root of f (x), and xi the current approximation. By defining the current
error:
ei := xi − x̃
which we expect to be small (at least close to the root), the iteration can be approximated
with Taylor series about the exact solution:
n xn
0 1.5
1 1.348
2 1.3252
3 1.3247182
4 1.324717957
Exercise 2.9
√
Use Newton’s method to compute 2. Use the function f (x) = x2 − 2 and the initial guess
x0 = 0.2. How many iterations are needed to get six decimal places? Perform the iterations.
Chapter 3
Polynomial Interpolation in 1d
The process of constructing a smooth function which passes exactly though specified data
points is called interpolation. Introducing some notation, the data points we denote
(xi , fi ), for i = 0, 1, . . . , n,
which we imagine to come from some exact function f (x) which is unknown, and which we
wish to reconstruct on the interval [x0 , xn ]. We usually expect that x0 < x1 < · · · < xn , in
particular that no two xi s are equal.
An interpolating function is called an interpolant and is a linear combination of prescribed
basis functions. If the basis functions are:
{ϕi (x) : i = 0, . . . , n}
where ai are the interpolation coefficients, and are constant (not a function of x), and must
be choosen to force φ(x) to match the data (xi , fi ).
φ(xi ) = fi , for i = 0, . . . , n.
21
22 Chapter 3. Polynomial Interpolation in 1d
In the above definition note that we have n + 1 interpolation conditions to satisfy, and
also n + 1 degrees of freedom (DoFs) the ai that we can change to satsify the conditions. This
suggests that the problem can be solved.
The functions ϕi (x) might be polynomials (leading to polynomial interpolation), trigono-
metric functions (Fourier interpolation), rational functions (rational interpolation), or any-
thing else. Then, the problem of linear interpolation can be formulated as follows:
Pn
If the element φ = i=0 ai ϕi is to satisfy the interpolation conditions φ(xi ) = fi for
i = 0, . . . , n, then the coefficients must satisfy the linear system of equations:
a0 ϕ0 (x0 ) + · · · + an ϕn (x0 ) = f0 ,
a0 ϕ0 (x1 ) + · · · + an ϕn (x1 ) = f1 ,
.. .. ..
. . .
a0 ϕ0 (xn ) + · · · + an ϕn (xn ) = fn ,
or more concisely:
n
X
ai ϕi (xj ) = fj , for j = 0, . . . , n. (3.1)
i=0
This is a matrix equation. If A is the matrix with elements aij = φj (xi ), a = (a0 , . . . , an ),
f = (f0 , . . . , fn ), we write (3.1) as
Aa = f . (3.2)
Equation (3.2) is a linear system of dimension (n+1)×(n+1). There exists a unique solution
a = A−1 f ,
if
det A 6= 0.
The value of det A depends on the chosen basis functions {φj } and on the data locations
{xi }, but not on the data values {fi }. If det A 6= 0 for every selection of n + 1 distinct data
points, then the system of basis functions {φj (x)} is called unisolvent - a highly desirable
property. Note that if xi = xj for any i 6= j then two rows of A will be identical and therefore
det A = 0.
Initially we concentrate on the one-dimensional case (that is with only one independent
variable x). This is called univariate interpolation.
3.1. The Monomial Basis 23
ϕ0 (x) = 1
ϕ1 (x) = x
ϕ2 (x) = x2
.. ..
. .
ϕn (x) = xn ,
It is clear that all polynomials of degree ≤ n can be written in this form. This is not the only
possible basis, the Lagrange basis and the Newton basis will be discussed later.
As for all bases the coefficients {ai } are uniquely determined by the interpolation condi-
tions
pn (xi ) = fi , i = 0, . . . , n,
these are n + 1 conditions for n + 1 unknowns. We write the conditions a linear system
V a = f.
The particular form of the matrix that results with a monomial basis has a special name: the
Vandermonde matrix
1 x0 x20 . . . xn−1 xn0
0
V = ... ... .. .. .. . (3.3)
. . .
1 xn x2n . . . xn−1
n xnn
The right-hand side is simply
T
f = f0 f1 f2 . . . fn−1 fn .
We therefore solve
1 0 0 a0 0
2
1 π/2 π /4 a1 = 1
1 π π2 a2 0
for a, giving
a0 = 0 a1 = 4/π a2 = −4/π 2 .
The approximating function is therefore
a0 = f (x0 )
a1 = f 0 (x0 )
1
a2 = f 00 (x0 )
2!
.. ..
. .
1 (n)
an = f (x0 ).
n!
Thus
1 00 1
φ(x) = f (x0 ) + f 0 (x0 )x +
f (x0 )x2 + · · · + f (n) (x0 )xn ,
2! n!
is the polynomial reconstruction — and we already know an expression for the error (the
Lagrange remainder). Note that it is not an interpolation, as it does not in general pass
3.1. The Monomial Basis 25
2.0
sin(x)
Monomial 2th order
1.5 Taylor 5th order
1.0
0.5
0.0
0.5
1.0
1.5 1 0 1 2 3 4
x
Figure 3.1: Approximating sin(x) with monomial interpolation and Taylor expansion.
26 Chapter 3. Polynomial Interpolation in 1d
through a prescribed set of points {(xi , fi )}. Also the approximation is typically very good
close to x0 , and deteriorates rapidly with distance. In interpolation we would like the error to
be small on an entire interval. Finally in practice we often have a set of data points, but we
very rarely have higher derivatives f (n) (x0 ) of the function of interest, so we can not compute
ai .
The Taylor expansion of sin(x) about x0 = 0 is plotted in Figure 3.1. The expansion is
better than 2nd-order monomial interpolation close to 0, but the latter much better over the
whole interval with a polynomial of much lower order.
The theorem tells us that any continuous function may be approximated as closely as we
wish by a polynomial of suffciently high degree. The theorem does not tell us how to find
that polynomial, and the remainder of this chapter is dedicated to that task.
Since a polynomial of degree n has n + 1 coefficients, there is a unique polynomial of
degree ≤ n which agrees with a given function at n + 1 data points.
There are other reasons for using polynomials as interpolant:
• polynomials can be evaluated using the arithmetic operations +,−,× only (i.e. easy for
a computer);
• derivatives and indefinite integrals of polynomials are easy to compute and are polyno-
mials themselves;
1
The ∞-norm is defined by
kf k∞ := max |f (x)|,
x∈[a,b]
and is one measure of the “size” of a function or distance between two functions. Another common measure
is the L2 -norm (pronounced “L-two”),
s
Z b
kf k2 := [f (x)]2 dx,
a
p
which is comparable to the Euclidian norm for vectors |x|2 = x2 + y 2 + z 2 .
3.3. Newton polynomial basis 27
This last is very important: it means there is exactly one polynomial of degree ≤ n that
passes through n + 1 points:
pn (xi ) = fi , i = 0, 1, . . . , n. (3.5)
Note carefully that the polynomial pn has degree n or less. Thus, if the n + 1 data points lie
on a straight line, the polynomial πn (x) will look like
a0 + a1 x + 0x2 + . . . + 0xn .
time we add a new node to the data-set, we have to re-evaluate the values of all coeffients,
which is inefficient. This can be avoided when using the Newton basis:
k−1
Y
π0 (x) = 1, πk (x) = (x − xj ), k = 1, 2, . . . , n. (3.6)
j=0
Then as usual the interpolation polynomial can be written as a sum of basis functions:
Why is this a good choice of basis? Consider the interpolation conditions (3.1) for the
Newton basis, and the linear system Aa = f resulting from these. In general the matrix A
has entries aij = φj (xi ), in the case of the Newton basis we have the matrix
π0 (x0 ) π1 (x0 ) . . . πn (x0 )
U = ... .. .. . (3.8)
. .
π0 (xn ) π1 (xn ) . . . πn (xn )
π0 (x) = 1
π1 (x) = (x − x0 ) = x
π2 (x) = (x − x0 )(x − x1 ) = x(x − π/2)
3.4. Lagrange polynomial basis 29
Clearly from the first row d0 = 0. Substituting into the 2nd row gives directly d1 = 2/π, and
then the 3rd row becomes
2 π2
π + d2 = 0
π 2
so d2 = −4/π 2 and the interpolating polynomial is
2 4 π 4 4
p(x) = x − 2 x(x − ) = x − 2 x2 ,
π π 2 π π
i.e. exactly the same polynomial obtained in Example 3.2.
If you ever have to do polynomial interpolation on paper (e.g. in an exam) — this is usually
the easiest way to do it
Then, every element li (x) of l is a polynomial in x of degree n, and the expression for the
interpolating polynomial (3.12) becomes simply:
n
X
pn (x) = l(x)T f = fi li (x).
i=0
I.e. the interpolation matrix A has become the identity, and the interpolation coefficients a
are just the function values f .
From the interpolation condition pn (xi ) = fi , i = 0, . . . , n, it follows that
li (xj ) = δij ,
30 Chapter 3. Polynomial Interpolation in 1d
where
(
1 if i = j
δij := .
0 otherwise
Hence, the nodes {xj : i 6= j} are the zeros of the polynomial li (x). In the next step we
therefore construct polynomials which take the value 0 at all nodes except xi , and take the
value 1 at xi :2
n
Y x − xj
li (x) = , i = 0, . . . , n.
j=0
x i − xj
j6=i
These polynomials of degree n, li (x), are called Lagrange polynomials and are often denoted
li (x). This notation will be used in these notes:
n
Y x − xj
li (x) = , i = 0, . . . , n.
j=0
xi − xj
j6=i
which is the Lagrange representation of the interpolation polynomial. The advantage of the
Lagrange representation is that the Vandermonde matrix (3.3) may become ill-conditioned if
n is large and/or the distance between two interpolation nodes xi and xj is small (and two
rows of V become similar, so det V → 0. When using the Lagrange representation (3.14) it is
possible to write down the interpolating polynomial pn (x) without solving a linear system to
compute the coefficients; the coefficient of the Lagrange basis functions li (x) is the function
value fi (this is useful on quizzes).
Example 3.7
Find the Lagrange interpolation polynomial which agrees with the following data. Use it to
estimate the value of f (2.5).
i 0 1 2 3
xi 0 1 3 4
f (xi ) 3 2 1 0
2
Alternatively we could invert V in (3.13) which gives the same answer, but in a less clean form.
3.5. Interpolation Error 31
n = 11 and n = 21 equally spaced data points. The results are shown in figure 3.3. It can be
shown that pn (x) 6→ f (x) as n → ∞ for any |x| > 3.63.
Hence, the interpolation error kf − pn k∞ grows without bound as n → ∞. Another
example is f (x) = |x| on [−1, 1], for which the interpolation polynomials do not even converge
pointwise except at the 3 points −1, 0, 1. So, the answer to the question, whether there
exists a single grid X for which the sequence of interpolation polynomials converge to any
32 Chapter 3. Polynomial Interpolation in 1d
2 20
n1 n2
1.5
0
1
0.5 −20
0
−40
−0.5
x− x+ x− x+
−1 −60
−5 0 5 −5 0 5
x x
0.4 0.4
n1 n2
0.3 0.3
0.2 0.2
0.1 0.1
0 0
−0.1 −0.1
x− x+ x− x+
−0.2 −0.2
−5 0 5 −5 0 5
x x
Figure 3.3: The top row shows f (x) (dashed line) and the interpolant (solid line) using
n = 11 and n = 21 equally spaced data points (stars) respectively. We also
indicated the lines x = ±3.5. At the bottom row the difference between f and
its interpolant are shown for x ∈ [−3.5, 3.5].
3.5. Interpolation Error 33
continuous function f is ’no’. However, Weierstrass’ theorem tells us that such a grid exists
for every continuous function! Hence, the statement made before implies that we would need
to determine this grid for each individual continuous function newly.
The nodal polynomial is unique, a polynomial of degree n + 1, and has leading coefficient 1.
The latter means that the coefficient of the highest power of x, xn+1 , is equal to 1. Moreover,
all nodes xi are the zeros of the nodal polynomial.
In most instances we don’t know ξ. Then, we may use the following estimate:
|ωn+1 (x)|
|Rn (f ; x)| ≤ max |f n+1 (x)| . (3.16)
x∈[a,b] (n + 1)!
In (3.16), we have no control over maxx∈[a,b] |f n+1 (x)|, which depends on the function, and
which can be very large. Take for instance
1
f (x) = ⇒ kf n+1 k∞ = (n + 1)! αn+1 .
1 + α 2 x2
However, we may have control on the grid X. Hence, we may reduce the interpolation
error if we choose the grid so that kωn+1 k∞ is small. The question is therefore: what is the
grid for which this is minimized? The answer is given by the following theorem:
Tn+1 (x)
ωn+1 (x) = ,
2n
and
1
kωn+1 k∞ =
2n
and this is the smallest possible value! The grid X such that the xi ’s are the n+1 zeros of the
Chebychev polynomial of degree n + 1, Tn+1 (x), is called the Chebychev-Gauss grid. Then,
Eq. (3.16) tells us that if f n+1 < C, for some constant C the convergence of the interpolation
polynomial towards f for n → ∞ is extremely fast. Hence, the Chebyshev-Gauss grid has
much better interpolation properties than any other grid, in particular the uniform one.
What is left is to define briefly the Chebyshev polynomials. The Chebyshev polynomial
of degree n for x ∈ [−1, 1] is defined by
or equivalently
Tn (cos(x)) = cos(nx).
Note that the Chebyshev polynomials are only defined on the interval [−1, 1].
Chebyshev polynomials can also be defined recursively. The first two are
T0 (x) = 1, T1 (x) = x,
For instance, T2 (x) = 2x2 − 1, T3 (x) = 4x3 − 3x etc. Note that Tn (x) is an polynomial in x
of degree n. It has n distinct zeros, which are all located inside the interval [−1, 1]. These
zeros are given by
2i − 1
ξi = cos π , i = 1, . . . , n. (3.18)
2n
The first few Chebyshev polynomials are plotted in Figure 3.4 - note their roots, and
take maximum and minimum values of ±1. The coeffcient of xn in Tn is 2n−1 , which can be
deduced from examining the recurrence relation (3.17). Therefore the maximum value of the
monic polynomial
Tn (x)
T̃n (x) := n+1 ,
2
3.5. Interpolation Error 35
T0 (x)
T1 (x)
1.0
T2 (x)
T3 (x)
T4 (x)
T5 (x)
0.5
0.0
0.5
1.0
x
1.0 0.5 0.0 0.5 1.0
is 1/2n+1 .
When we want to interpolate a function f (x) on the interval [a, b] using a polynomial of
degree n on a Chebyshev-Gauss grid, the grid points a < x0 < x1 < . . . < xn < b can be
computed by a simple linear transformation, which maps [−1, 1] on [a, b]. Taking also into
account that the first node of the grid, x0 , has index 0 and the zero closest to −1 of the
Chebyshev polynomial Tn+1 (x) is ξn+1 , the nodes on [a, b] can be computed by
b+a b−a
xn−i = + ξi , i = 1, . . . , n. (3.19)
2 2
The {xi } are the nodes of the grid X we would choose for interplation on the interval [a, b].
These zeros are transformed onto [6, 10] using the transformation
x5−i = 8 + 2 ξi , i = 1, . . . , 5. (3.21)
The results are shown in table 3.1 Hence, the interpolation nodes are x0 = 6.098, x1 = 6.824,
i ξi x5−i
1 0.951 9.902
2 0.588 9.176
3 0.000 8.000
4 -0.588 6.824
5 -0.951 6.098
Though the choice of the Chebyshev-Gauss grid for interpolation has advantages over a
uniform grid, there are also some penalties.
2. In practice it may be difficult to obtain the data fi measured at the Chebyshev points.
Therefore, if for some reason it is not possible to choose the Chebyshev-Gauss grid,
choose the grid so that there are more nodes towards the endpoints of the interval
[a, b].
1 1
n1 n2
0.8
0.5
0.6
0.4
0
0.2
−0.5 0
−5 0 5 −5 0 5
x x
0.6 0.6
n1 n2
0.4 0.4
0.2 0.2
0 0
−0.2 −0.2
−5 0 5 −5 0 5
x x
Figure 3.5: Here we show f (dashed line) and its interpolant (solid line) using unequally
spaced data points (stars) distributed over [−5, 5] according to (3.19). The
bottom row shows again the difference between f and its interpolant.
38 Chapter 3. Polynomial Interpolation in 1d
Chapter 4
39
40 Chapter 4. Advanced Interpolation: Splines, Multi-dimensions and Radial Bases
This large error bound suggests that we can make the interpolation error as small as we wish
by freezing the value of n and then reducing the size of b − a. We still need an approximation
over the original interval [a, b], so we use a piecewise polynomial approximation: the original
interval is divided into non-overlapping subintervals and a different polynomial fit of the data
is used on each subinterval.
A simple piecewise polynomial fit is obtained in the following way: for data {(xi , fi ) :
i = 0, . . . , n}, where a ≤ x0 < x1 < · · · < xn ≤ b, we take the straight line connection of
two neighbouring data points xi and xi+1 as the interpolant on the interval [xi , xi+1 ]. Hence,
globally the interpolant is the unique polygon obtained by joining the data points together.
Instead of using linear polynomials to interpolate between two neighboring data points we
may use quadratic polynomials to interpolate between three neighboring data points, cubic
polynomials to interpolate between four neighboring data points etc. In this way we may
improve the performance but at the expense of smoothness in the approximating function.
Globally the interpolant will be at best continuous, but it will not be differentiable at the data
points. For many applications a higher degree of smoothness at the data points is required.
This additional smoothness can be achieved by using low-degree polynomials on each interval
[xi , xi+1 ] while imposing some smoothness conditions at the data points to ensure that the
overall interpolating function has globally as high a degree of continuity as possible. The
corresponding functions are called splines.
where
fi+1 − fi
si (x) = fi + (x − xi ), i = 0, 1, . . . , n − 1. (4.6)
xi+1 − xi
4.1. Spline interpolation 41
Hence, if we are given n+1 data points, the linear spline will consists of n degree-1 polynomials
each of which holds between a pair of consecutive data points. With h := maxi |xi+1 − xi |,
we obtain an upper bound for the interpolation error for x ∈ [xi , xi+1 ] from (4.1) with n = 1
as follows: define
xi + xi+1
x̂ := ,
2
the interval midpoint. Then for the nodal polynomial in this case:
xi+1 − xi xi+1 − xi
|ω2 (x)| = |(x − xi )(x − xi+1 )| ≤ |(x̂ − xi )(x̂ − xi+1 )| = ,
2 2
where the 1st inequality follows from the fact that the maximum or minimum of a parabola
is located at the midpoint of the two roots. Therefore by (4.1), an upper bound on the
interpolation error on the interval is:
The conditions which s must satisfy are that s interpolates f at the data points x0 , . . . , xn and
that s0 and s00 must be continuous at the interior data points x1 , . . . , xn−1 . Let us determine
the spline interpolant s from these conditions.
42 Chapter 4. Advanced Interpolation: Splines, Multi-dimensions and Radial Bases
Since si is a cubic polynomial, s00 is linear. Let us denote the yet unknown values of s00 at
the data points xi and xi+1 by Mi and Mi+1 , respectively, i.e. s00 (xi ) = Mi and s00 (xi+1 ) =
Mi+1 .
si (x) is a cubic polynomial, hence it can be written as
si (x) = ai (x − xi )3 + bi (x − xi )2 + ci (x − xi ) + d1 , i = 0, . . . , n − 1. (4.8)
Then,
hence,
Mi
s00i (xi ) = Mi = 2bi ⇒ bi =
2
Mi+1 − Mi
s00i (xi+1 ) = 6ai hi + 2bi ⇒ ai = ,
6hi
where we have defined hi := xi+1 − xi . We insert the results for ai and bi into the equation
of the spline si (x) and find
Mi+1 − Mi Mi
si (x) = (x − xi )3 + (x − xi )2 + ci (x − xi ) + di . (4.9)
6hi 2
si (xi ) = fi = di ⇒ di = fi
Mi+1 − Mi 3 Mi 2
si (xi+1 ) = fi+1 = hi + h + chi + fi
6hi 2 i
fi+1 − fi hi hi
⇒ ci = − Mi − Mi+1 .
hi 3 6
We insert the results for di and ci into the equation for si (x) and find
Mi+1 − Mi Mi f
i+1 − fi hi hi
si (x) = (x−xi )3 + (x−xi )2 + − Mi − Mi+1 (x−xi )+fi . (4.10)
6hi 2 hi 3 6
So far, we have not used the conditions
Mi+1 − Mi f
i+1 − fi hi hi
s0i (x) = (x − xi )2 + Mi (x − xi ) + − Mi − Mi+1 , (4.12)
2hi hi 3 6
4.1. Spline interpolation 43
hence,
f
i+1 − fi hi hi
s0i (xi ) = − Mi − Mi+1 . (4.13)
hi 3 6
In the same way, we find
Mi − Mi−1 f − f
i i−1 hi−1 hi−1
s0i−1 (x) = (x − xi−1 )2 + Mi−1 (x − xi−1 ) + − Mi−1 − Mi ,
2hi−1 hi−1 3 6
(4.14)
hence,
hi−1 hi−1 fi − fi−1
s0i−1 (xi ) = Mi + Mi−1 + . (4.15)
3 6 hi−1
Therefore, the conditions s0i (xi ) = s0i−1 (xi ), i = 1, . . . , n − 1 yield the following system of n − 1
equations for the n + 1 unknowns M0 , M1 , . . . , Mn−1 , Mn :
hi−1 (hi−1 + hi ) hi fi+1 − fi fi − fi−1
Mi−1 + Mi + Mi+1 = − , i = 1, . . . , n − 1. (4.16)
6 3 6 hi hi−1
This system has infinitely many solutions. A unique solution can only be obtained if addi-
tional constraints are imposed. There are many constraints we could choose. The simplest
constraints are
M0 = Mn = 0. (4.17)
When making this choice, the cubic spline is called natural cubic spline. The natural cubic
spline may deliver no accurate approximation of the underlying function at the ends of the
interval [x0 , xn ]. This may be anticipated from the fact that we are forcing a zero value on
the second derivative when this is not necessarily the value of the second derivative of the
function which the data measures. For instance, a natural cubic spline is built up from cubic
polynomials, so it is reasonable to expect that if the data is measured from a cubic polynomial
then the natural cubic spline will reproduce the cubic polynomial. However, if the data are
measured from, e.g., the function f (x) = x2 , then the natural cubic spline s(x) 6= f (x). The
function f (x) = x2 has nonzero second derivatives at the nodes x0 and xn where the value
of the second derivative of the natural cubic spline is zero by definition.
To clear up the inaccuracy problem associated with the natural spline conditions, we
could replace them with the correct second derivatives values
These second derivatives of the data are not usually available, but they can be replaced by
reasonable approximations. Anyway, if the exact values or sufficiently accurate approxima-
tions are used then the resulting spline will be as accurate as possible for a cubic spline. Such
approximations may be obtained by using polynomial interpolation to sufficient data values
separately near each end of the interval [x0 , xn ]. Then, the two interpolating polynomials are
each twice differentiated and the resulting twice differentiated polynomials are evaluated at
the corresponding end points to approximate the f 00 there.
44 Chapter 4. Advanced Interpolation: Splines, Multi-dimensions and Radial Bases
A simpler, and usually sufficiently accurate spline may be determined as follows: on the
first two and the last two intervals we define each a cubic polynomial, hence x1 and xn−1
are in fact no nodes. This fixes in fact the two unknown second derivatives M1 and Mn−1
uniquely and we are left with the solution of a linear system of dimension n − 1 for the n − 1
unknowns M0 , M2 , M3 , . . . , Mn−2 , Mn . The corresponding cubic spline is sometimes called a
not-a-knot spline (’knot’ is an alternative notation for ’node’).
For each way of supplying the additional constraints that is discussed before, the cubic
spline is unique. From the error bound for polynomial interpolation, for a cubic polynomial
interpolating at data points in the interval [a, b], we have
where C is a constant and h = maxi (xi+1 − xi ). Therefore, we might anticipate that the error
associated with a cubic spline interpolant behaves like h4 for h small. However, the maximum
absolute error associated with a natural cubic spline behaves like h2 as h → 0. In contrast,
the maximum absolute error for a cubic spline based on correct endpoint second derivatives
or on the not-a-knot conditions behaves like h4 . Unlike the natural cubic spline, the correct
second derivative value and not-a-knot cubic splines reproduce cubic polynomials.
Example 4.2
Find the natural cubic spline which interpolates the data
With h0 = 0.1, h1 = 0.2, and h2 = 0.3 we obtain the following linear system of equations for
the unknowns M1 and M2 :
0.3 0.2
M1 + M2 = 1.8975 − 2.6240
3 6
0.2 0.5
M1 + M2 = 1.2923 − 1.8975
6 3
It has the solution M1 = −6.4871, M2 = −2.3336. Then, the natural cubic spline is
3
−11.0812 x + 2.7321 x
x ∈ [0, 0.1]
s(x) = 3.4613(x − 0.1)3 − 3.2436(x − 0.1)2 + 2.4078(x − 0.1) + 0.2624 x ∈ [0.1, 0.3]
1.2964(x − 0.3)3 − 1.1668(x − 0.3)2 + 1.5257(x − 0.3) + 0.6419 x ∈ [0.3, 0.6]
We can simplify substantially the rather time-consuming computations if the data points are
equally spaced so that xi = x0 + ih, i = 1, . . . , n. Then we obtain the following linear system
4.2. Bivariate interpolation 45
Example 4.3
1
Find the natural cubic spline interpolant to the function f (x) = 1+x2
from the following
table:
xi −1 −0.5 0 0.5 1
fi 0.5 0.8 1 0.8 0.5
It is s(0.8) = s3 (0.8) = 0.8 − 0.6 · 0.3 = 0.62 and the absolute error is |f (0.8) − s(0.8)| =
|0.61 − 0.62| = 0.01.
Exercise 4.4
Find the natural cubic spline which interpolates the values ln(1+2x) at x = 0, 0.1, 0.2, 0.3, 0.4,
and 0.5. Use this spline to estimate the values of ln(1.1), ln(1.3), ln(1.5), ln(1.7), and ln(1.9).
Compare the results with those using the linear spline.
p(xi ) = fi , i = 1, . . . , n. (4.21)
Solving the interpolation problem under this assumption leads to a system of linear equations
of the form
A c = f, (4.23)
where the entries of the matrix A are
A unique solution of the interpolation problem will exist if and only if det(A) 6= 0. In one-
dimensional interpolation, it is well-known that one can interpolate to arbitrary data at n
distinct points using a polynomial of degree n − 1. However, it can be shown that det(A) 6= 0
does not hold for arbitrary distributions of distinct data points in two (or more) dimensions.
Hence, it is not possible to perform a unique interpolation with bivariate polynomials of a
certain degree for data given at arbitrary locations in R2 . If we want to have a well-posed
(i.e. uniquely solvable) bivariate interpolation problem for scattered data, then the basis
needs to depend on the data (what this means will be explained later). There are a very few
exceptions of this rule, for specific data distributions, choice of basis functions, and choice of
the orientation of the Cartesian coordinate system.
In the following we will discuss bivariate interpolation. We will start with polynomial
interpolation, which is suitable for gridded data. For scattered data, basis functions will be
used that depend on the data, the so-called radial basis functions. Points in R2 are described
using Cartesian coordinates, i.e., x = (x, y)T .
4.2. Bivariate interpolation 47
For ϕi and ψj we may use any of the representations mentioned before. In particular, if we
choose the Lagrange representation, it is
(X) (Y )
bij (x, y) = li (x) · lj (y), (4.26)
where
n n
(X)
Y x − xj (Y )
Y y − yk
li (x) = , lj (y) = , (4.27)
xi − xj yj − yk
j=0,j6=i k=0,k6=j
and (
(X) (Y ) 1 if k = i and l = j
bij (xk , yl ) = li (xk ) · lj (yl ) = . (4.28)
0 otherwise
Hence, the functions bij (x, y) behave like the univariate Lagrange basis functions, but are
based now on the rectangular lattice. Then, the bivariate interpolation polynomial is given
48 Chapter 4. Advanced Interpolation: Splines, Multi-dimensions and Radial Bases
by
n X
X m
p(x, y) = fij bij (x, y). (4.29)
i=0 j=0
Defining a data matrix F with entries Fij = fij and vectors of basis functions lX (x) =
T T
(X) (X) (Y ) (Y )
l0 (x) . . . ln (x) and lY (y) = l0 (y) . . . lm (y) , we can write the bivariate interpo-
lation polynomial also as
T
p(x, y) = lTX (x) F lY (y). (4.30)
(X) (Y )
Note that in general lk (x) 6= lk (y) unless X = Y .
Instead of using the Lagrange representation of the univariate polynomials in x and y,
we could also use the Newton representation or the monomial representation. However, as
already mentioned in the section on univariate interpolation, the Newton representation has to
be preferred, because it is less computationally intensive than the other two representations.
While in this manner quite acceptable interpolation surfaces result for small values of n
and m, for larger values of n and m and/or certain configurations of data (i.e., equidistant
data), the resulting surfaces have a very wavy appearance, due to strong oscillations along
the boundary of the lattice. This phenomenon has already been discussed in the section on
univariate interpolation, and all statements also apply to bivariate polynomial interpolation.
Therefore, for larger values of n and m it is better to use piecewise polynomials, e.g.,
splines. Howeover, the approach described above can only be used for Overhauser splines in
x and y, but not for instance for the cubic spline. Theoretically, it would also be possible to
use different basis functions for x and for y, though this must be justified by some a-priori
information about the behavior of the function f in x and y.
The approach outlined before is called tensor-product interpolation. Existence and unique-
ness of the tensor product interpolation is stated in the following theorem:
Theorem 4.6
Let ϕ0 , . . . , ϕn be a set of functions and x0 < x1 < . . . < xn be a set of points with the property
that, for any f0 , f1 , . . . , fn ,there exist unique numbers α0 , . . . , αn such that ni=0 αi ϕi (xk ) =
P
Then, given any set of numbers fij there exists a unique corresponding set of numbers aij
such that the function
Xn X m
p(x, y) = aij bij (x, y) (4.32)
i=0 j=0
Example 4.7
Find the bilinear interpolation polynomial from the data f (0, 0) = 1, f (1, 0) = f (0, 1) =
f (1, 1) = 0.
The bilinear interpolation polynomial p(x, y) is given by:
1 X
X 1
p(x, y) = fij li (x)lj (y)
i=0 j=0
Therefore, we obtain
Exercise 4.8
Interpolate f (x, y) = sin(πx) sin(πy) on (x, y) ∈ [0, 1] × [0, 1] on a rectangular grid with the
step size hx = hy = 21 using a polynomial which is quadratic in x and y.
p(x, y) = a0 + a1 x + a2 y + a3 xy
Let us assume that the points P1 , P2 , P3 , and P4 have the local coordinates (−1, 1), (1, 1),
(−1, −1), and (1, −1), respectively. Then, the interpolation condition reads
p(xi , yi ) = fi , i = 1, . . . , 4
or
f1 1 −1 1 −1 a0
f2 1 1 1 1 a1
=
1 −1 −1
f3 1 a2
f4 1 1 −1 −1 a3
50 Chapter 4. Advanced Interpolation: Splines, Multi-dimensions and Radial Bases
P4 P3
P1 P2
f =A·a
Formally,
a = A−1 f ,
and we can write the interpolation polynomial on the rectangle as
The vector S(x, y) obtains the so-called “shape functions”. They can be calculated without
knowing the function values at the nodes. In our case:
and we obtain
1 − x + y − xy (1 + y)(1 − x)
1 + x + y + xy (1 + y)(1 + x)
S(x, y) = bT (x, y) A−1 = 1
= 1
(4.33)
4
1 − x − y + xy 4
(1 − y)(1 − x)
1 + x − y − xy (1 − y)(1 + x)
1 5 2
8 6
3 7 4
If the function values at the mid-side nodes (as figure 4.2 shows) are known as well, we obtain
with
bT (x, y) = (1, x, y, xy, x2 , y 2 , x2 y, xy 2 )
52 Chapter 4. Advanced Interpolation: Splines, Multi-dimensions and Radial Bases
−(y + 1)(1 − x)(1 + x − y)
−(y + 1)(1 + x)(1 − x − y)
−(1 − x)(1 − y)(1 + x + y)
1
−(1 + x)(1 − y)(1 − x + y)
S(x, y) = 4
2(1 − x)(1 + x)(1 + y)
2(1 + x)(1 + y)(1 − y)
2(1 + x)(1 − x)(1 − y)
2(1 + y)(1 − x)(1 − y)
1 2 3 4 5 6 7 8
ξi 1.0 2.0 1.0 2.0 1.5 2.0 1.5 1.0
ηi 0.5 0.5 0.2 0.2 0.5 0.35 0.2 0.35
fi 1.703 3.943 0.640 1.568 2.549 2.780 0.990 1.181
We may also consider triangular elements, e.g. the standard triangle (0, 0), (1, 0), (0, 1), as
shown in figure 4.3. If we know the function values at the vertices, we can uniquely determine
a linear interpolator:
p(x, y) = a0 + a1 x + a2 y
using the interpolation condition
p(xi , yi ) = fi i = 1, 2, 3.
p(x, y) = a0 + a1 x + a2 y + a3 x2 + a4 xy + a5 y 2
can be determined if in addition the function values at the three mid-sides are known, as is
shown in figure 4.3.
y y
P3 P3
P6 P5
x x
P1 P2 P1 P4 P2
Figure 4.3: Linear triangular element (left) and quadratic triangular element (right).
and finally
(1 − x − y)(1 − 2x − 2y)
x(2x − 1)
y(2y − 1)
S(x, y) = .
4x(1 − x − y)
4xy
4y(1 − x − y)
v y
P3
P3a
P1
P2
u x
P1a P2a
Thus,
S(x, y) = S (x(u, v), y(u, v)) = S∗ (u, v) (4.35)
Then X X
p∗ (u, v) = fi Si∗ (u, v) = p(x, y) = fi Si (x, y) (4.36)
i i
is the interpolant in (u, v)-coordinates.
In an analogous manner we can transform a general parallelogram onto the standard
rectangle [−1, 1] × [−1, 1], as figure 4.5 shows.
v y
P4 P3 Q4 Q3
P1 P2
u Q1 Q2
The interpolation problem using radial basis functions (RBF’s) can be formulated as
follows:
Definition 4.12 (Basic RBF method)
Given a set of n distinct data points {xi : i = 1, . . . , n} and data values {fi : i = 1, . . . , n},
the basic RBF interpolant is given by
n
X
s(x) = aj Φ(kx − xj k), (4.39)
j=1
where Φ(r), r ≥ 0 is some radial function. The coefficients aj are determined from the
interpolation conditions
s(xj ) = fj , j = 1, . . . , n, (4.40)
which leads to the following symmetric linear system:
A a = f. (4.41)
Some common examples of the Φ(r) that lead to a uniquely solvable method (i.e., to a
non-singular matrix A) are:
56 Chapter 4. Advanced Interpolation: Splines, Multi-dimensions and Radial Bases
1. Gaussian:
2
Φ(r) = e−(εr) . (4.43)
4. Multiquadric (MQ): p
Φ(r) = 1 + (εr)2 . (4.46)
5. Linear:
Φ(r) = r. (4.47)
In all cases, ε is a free parameter to be chosen appropriately. It controls the shape of the
functions: as ε → 0, radial functions become more flat. At this point, assume that it is
some fixed non-zero real value. The cubic RBF and the very popular thin-plate spline RBF,
defined by
6. Cubic RBF:
Φ(r) = r3 . (4.48)
7. Thin-plate spline:
Φ(r) = r2 log r. (4.49)
do not provide a regular matrix A unless some additional restrictions are met, which leads to
the so-called augmented RBF method, which is discussed next. Before we define this method
we need to make one more definition:
Definition 4.13
Let Πm (R2 ) be the space of all bivariate polynomials that have degree less than or equal to
m. Furthermore, let M denote the dimension of Πm (R2 ), then
1
M = (m + 1)(m + 2). (4.50)
2
For instance, a basis of Π1 comprises the polynomials 1, x, and y; this space has dimension
M = 3; a basis of Π2 comprises the polynomials 1, x, y, x2 , xy, and y 2 ; the dimension of the
space is M = 6. In general, any function f ∈ Πm can be written as
X
f (x, y) = aij xi y j , aij ∈ R. (4.51)
0≤i+j≤m
where {pk (x) : k = 1, . . . , M } is a basis of the space Πm (R2 ) and Φ(r), r ≥ 0 is some
radial function. To account for the additional polynomial terms, the following constraints
are imposed:
Xn
λj pk (xj ) = 0, k = 1, . . . , M. (4.53)
j=1
The expansion coefficients λj and γk are then determined from the interpolation conditions
and the constraints (4.53): ! ! !
A P λ f
· = . (4.54)
PT 0 γ 0
P is the n × M matrix with entries Pij = pj (xi ) for i = 1, . . . , n and j = 1, . . . , M .
It can be shown that the augmented RBF method is uniquely solvable for the cubic and
thin-plate spline RBFs when m = 1 and the data points are such that the matrix P has
rank(P) = M . This is equivalent to saying that for a given basis {pk (x) : k = 1, . . . , M } for
Πm (R2 ) the data points {xj : j = 1, . . . , n} must satisfy the condition
M
X
γk pk (xj ) = 0 ⇒ γ = 0, (4.55)
k=1
Hence, we see that the augmented RBF method is much less restrictive than the basic RBF
method on the functions Φ(r) that can be used; however, it is far more restrictive on the data
points {xj : j = 1, . . . , n} that can be used. Remember that the only restriction on the data
points for the basic method is that the points are distinct.
Let us make some remarks related to the effect of ε and n on the stability of the basic
and/or augmented RBF method. For the infinitely smooth Φ(r) (i.e., for the Gaussian, the
58 Chapter 4. Advanced Interpolation: Splines, Multi-dimensions and Radial Bases
inverse quadratic, the inverse multiquadric, and the multiquadric RBF), the accuracy and
the stability depend on the number of data points n and the value of the shape parameter ε.
For a fixed ε, as the number of data points increases, the RBF interpolant converges to the
underlying (sufficiently smooth) function being interpolated at a spectral rate, i.e., O(e−c/h ),
where c is a constant and h is a measure of the typical distance between data points. The
2
Gaussian RBF exhibits even ’super-spectral’ convergence, i.e., O(e−c/h ). In either case, the
value of c in the estimates is effected by the value of ε. For a fixed number of data points, the
accuracy of the RBF interpolant can often be significantly improved by decreasing the value
of ε. However, decreasing ε or increasing the number n of data points has a severe effect on
the stability of the linear system (4.41) and (4.54), respectively. For a fixed ε, the condition
number of the matrix in the linear systems grows exponentially as the number of data points
is increased. For a fixed number of data points, similar growth occurs as ε → 0.
A very important feature of the RBF method is that its complexity does not increase
as dimension of the interpolation increases. Their simple form makes implementing the
methods extremely easy compared to, for example, a bicubic spline method. However, main
computational challenges are that i) the matrix for determining the interpolation coefficients,
(4.41) and (4.54) is dense, which makes the computational cost of the methods high; ii) the
matrix is ill-conditioned when the number of data points is large; iii) for the infinitely smooth
RBFs and a fixed number of data points, the matrix is also ill-conditioned when ε is small.
There are techniques available, which address these problems. However, they are beyond the
scope of this course.
S(xi , yj ) = fij , i = 0, 1, . . . , n, j = 0, 1, . . . , m.
∂2S
(2) S ∈ C 1 (D), continuous on D.
∂x∂y
(3) S is a bicubic polynomial within each rectangle Dij :
(4) S fulfills certain conditions on the boundary of the domain D̄ which still have to be
defined.
4.2. Bivariate interpolation 59
Because of (3), the bicubic spline function S(x, y) has on (x, y) ∈ Dij the representation
3 X
X 3
S(x, y)|Dij =: Sij = aijkl (x − xi )k (y − yj )l ,
k=0 l=0
(x, y) ∈ Dij , i = 0, . . . , n − 1, j = 0, . . . , m − 1 (4.58)
The 16m · n coefficients aijkl have to be determined such that the conditions (1) and (2) are
fulfilled. To determine them uniquely we must formulate certain boundary conditions like in
the one-dimensional case. One possibility is to prescribe the following partial derivatives of
S:
∂S
(xi , yj ) =: pij = aij10 , i = 0, n, j = 0, 1, . . . , m
∂x
∂S
(xi , yj ) =: qij = aij01 , i = 0, 1, . . . , n, j = 0, m (4.59)
∂y
∂2S
(xi , yj ) =: rij = aij11 , i = 0, n, j = 0, m
∂x∂y
They can be calculated approximately using one-dimensional splines or other interpolation
methods. For example one-dimensional splines through three points each and their derivatives
can be used to approximate the boundary conditions. The following algorithm assumes that
the boundary values (4.59) are given. It determines all coefficients aijkl in 9 steps:
Step 1: Calculation of aij10 = pij for i = 1, . . . , n − 1 and j = 0, . . . , m:
1 1 1 1
ai−1,j10 + 2 + aij10 + ai+1,j10 =
hi−1 h i−1 hi hi
3 3
= 2 (aij00 − ai−1,j00 ) + 2 (ai+1,j00 − aij00 ),
hi−1 hi
i = 1, . . . , n − 1, j = 0, . . . , m (4.60)
−1
Step 6: Determination of the matrix G(yj )T . Because
1 0 0 0
0 1 0 0
G(yj ) =
1 hj hj 2 h3j
0 1 2hj 3h2j
4.2. Bivariate interpolation 61
−1
with det G(yj ) = h4j 6= 0, hj = xj+1 − xj , j = 0, . . . , m − 1, the inverse G(yj )T
exists:
0 − h32 2
1 h3j
j
1 − h2j 1
0
−1 h2j
G(yj )T =
3
− h23
0 0 h2j
j
1 1
0 0 −j h2j
Step 9: Formation of the bicubic spline function Sij (x, y) for all rectangles Dij with equa-
tion (4.58).
The boundary conditions can be determined for instance by fitting one-dimensional splines
through three points each: Through the points (xi , fij ), i = 0, 1, 2, n − 2, n − 1, n we fit
for j = 0, . . . , m one-dimensional natural cubic splines and calculate the derivatives; they
provide the pij at the boundary. Through the points (yj , fij ), j = 0, 1, 2, m − 2, m − 1, m
for i = 0, . . . , n we fit the same type of spline functions and calculate the derivatives; they
provide the qij at the boundary. To calculate the rij = aij11 for i = 0, n, j = 0, m we fit
one-dimensional natural splines through (xi , qij ) for i = 0, 1, 2, n − 2, n − 1, n and j = 0, m
and determine the derivatives.
62 Chapter 4. Advanced Interpolation: Splines, Multi-dimensions and Radial Bases
Chapter 5
Least-squares Regression
In regression, as for interpolation, we consider the case where a function has to be recov-
ered from partial information, e.g. when we only know (possibly noisy) values of the function
at a set of points.
63
64 Chapter 5. Least-squares Regression
with T
c := c1 c2 . . . cM , (5.2)
and T
ϕ(x) := ϕ1 (x) ϕ2 (x) . . . ϕM (x) ,
exactly the same as for interpolation. In the following we will restrict to this type of ap-
proximants. Moreover, we will assume that the function we want to approximate is at least
continuous.
Where least-squares differs from interpolation is that the number of basis functions M is
in general less than the number of data points N ,
M ≤ N,
which means we will have more constraints than free variables, and therefore we will not be
able to satisfy all constraints exactly.
Commonly used classes of (univariate) basis functions are:
• algebraic polynomials:
ϕ1 = 1, ϕ2 = x, ϕ3 = x2 , . . .
• trigonometric polynomials:
• exponential functions:
ϕ1 = 1, ϕ2 = eα1 x , ϕ3 = eα2 x , . . .
• rational functions:
1 1
ϕ1 = 1, ϕ2 = p
, ϕ3 = ,..., pi ∈ N
(x − α1 ) 1 (x − α2 )p2
.
For bivariate regression, radial functions are also popular; they have been introduced in the
previous chapter.
5.2. Least-squares approximation - Example 65
i 1 2 3
xi 0 1 2
fi 4.5 3.0 2.0
φ(x) = a0 + a1 x,
with so far unknown coefficients a0 and a1 . Intuitively, we would like the straight line to be
as close as possible to the function f (x) that generates the data. A measure of the ’closeness’
between φ(x) and f (x) could be based on the difference of the function values of f and φ at
the given data points, i.e., on the quantities
The residuals are shown graphically in Figure 5.1 as the vertical red bars capturing the
distance between samples of f at the nodes, and the approximation φ. The ri ’s are called the
residuals. The least-squares method finds among all possible coefficients a0 and a1 the pair
that minimizes the square sum of the residuals,
3
X
ri2 ,
i=1
P3
i.e., that makes i=1 ri2 as small as possible. This minimization principle is sometimes called
the (discrete) least-squares principle. Of course, other choices also exists: we could, e.g.,
minimize the absolute sum of the residuals,
N
X
|ri |,
i=1
max(|ri |).
i
66 Chapter 5. Least-squares Regression
Figure 5.1: Example of regression of samples of a function f (x) with the curve φ(x). Red
bars show the residuals, which are minimized in order to solve for φ.
The advantage of the least-squares principle is that it is the only one among the three princi-
ples, which yields a linear system of equations for the unknown coefficients a0 and a1 . That is
the main reason why this principle has become so popular. Let us determine the coefficients
a0 and a1 according to the least-squares principle. We define a function Φ, which is equal to
the square sum of the residuals:
3
X 3
X
Φ(a0 , a1 ) := ri2 = (fi − a0 − a1 xi )2 .
i=1 i=1
We have written Φ(a0 , a1 ) to emphasize that the square sum of the residuals is seen as a
function of the unknown coefficients a0 and a1 . Hence, minimizing the square sum of the
residuals means to look for the minimum of the function Φ(a0 , a1 ). A necessary condition
for Φ to attain a minimum is that the first derivatives with respect to a0 and a1 are equal to
zero:
3
∂Φ X
= −2 (fi − a0 − a1 xi ) = 0,
∂a0
i=1
3
∂Φ X
= −2 (fi − a0 − a1 xi )xi = 0.
∂a1
i=1
This is a system of 2 equations for the 2 unknowns a0 and a1 . It is called normal equations.
The solution of the normal equations is sometimes called the least-squares solution, denoted
â0 and â1 . This is mostly done to emphasize that other solutions are possible, as well, as
5.3. Least-squares approximation - The general case 67
outlined before. Adopting this notation for the least-squares solution, the normal equations
are written as
3
X 3
X
(â0 + â1 xi ) = fi
i=1 i=1
3
X 3
X
2
(â0 xi + â1 xi ) = fi xi ,
i=1 i=1
Numerically, we find ! ! !
3 3 â0 9.5
= .
3 5 â1 7
The solution is (to 4 decimal places) â0 = 4.4167 and â1 = −1.2500. Hence, the least-squares
approximation of the given data by a straight line is
The least-squares residuals are computed as r̂i = fi − φ̂i , which gives r̂1 = 0.0833, r̂2 =
−0.1667, and r̂3 = 0.0833. The square sum of the (least-squares) residuals is Φ̂ = 3i=1 r̂i2 =
P
0.0417. Notice that the numerical values of the coefficients found before is the choice which
yields the smallest possible square sum of the residuals. No other pair of coefficients a0 , a1
yields a smaller Φ (try it yourself!).
In order to give the normal equations more ’structure’, we can define the following scalar
product of two functions given on a set of N points xi :
N
X
hf, gi := f (xi )g(xi ).
i=1
Obviously, hf, gi = hg, f i, i.e., the scalar product is symmetric. Using this scalar product,
the normal equations (5.3) can be written as (try it yourself!):
! ! !
< 1, 1 > < 1, x > â0 < f, 1 >
= .
< 1, x > < x, x > â1 < f, x >
for given basis functions {ϕi (x)}. Note that we allow for both univariate and bivariate
data. In the univariate case, the location of a data point is uniquely described by 1 variable,
denoted e.g., x; in the bivariate case, we need 2 variables to uniquely describe the location
of a data point, e.g., the Cartesian coordinates (x, y). Please also notice that the number of
basis functions, M , must not exceed the number of data points, N . In least-squares, usually
M N.
Then, the residuals are (cf. the example above),
where the vector a is defined as a := (a1 , . . . , aM )T . The advantage of the use of a scalar
product becomes clear now, because the normal equations, which are the solution of the
minimization problem (5.4), are
hϕ1 , ϕ1 i . . . hϕ1 , ϕM i â1 hf, ϕ1 i
.. .. .. ..
. = , (5.5)
. . .
hϕM , ϕ1 i . . . hϕM , ϕM i âM hf, ϕM i
with
N
X
hϕj , ϕk i = ϕj (xi )ϕk (xi ). (5.6)
i=1
Note that the normal equations are symmetric, because
hϕk , ϕj i = hϕj , ϕk i.
The solution of the normal equations yields the least-squares estimate of the coefficients a,
denoted â, and the discrete least-squares approximation is the function
M
X
φ̂(x) = âi ϕi (x).
i=1
can be used as a criterion for the efficiency of the approximation. Alternatively, the so-called
root-mean-square (RMS) error in the approximation is also used as a measure of the fit of
the function φ̂(x) to the given data. It is defined by
r
hr̂, r̂i
σRM S := ,
N
5.3. Least-squares approximation - The general case 69
When univariate algebraic polynomials are used as basis functions it can be shown that
N ≥ M together with the linear independence of the basis functions guarantee the unique
solvability of the normal equations. For other types of univariate basis functions this is not
guaranteed. For N < M the uniqueness gets lost. For N = M we have interpolation and
φ̂(xi ) = f (xi ) for i = 1, . . . , N .
For the bivariate case using radial functions as basis functions we have the additional
problem that we have less basis functions (namely M ) than we have data sites (namely N ).
Hence, we cannot place below every data point a basis function. Therefore, we need to have
a strategy of where to locate the radial functions. This already indicates that the question
whether the normal equations can be solved in the bivariate case with radial functions is
non-trivial. In fact, we can only guarantee unique solvability of the normal equations for
certain radial functions (i.e., multiquadrics, Gaussian) and severe restrictions to the location
of the centres of the radial functions (they must be sufficiently well distributed over D in
some sense) and to the location of the data sites (they must be fairly evenly clustered about
the centres of the radial functions with the diameter of the clusters being relatively small
compared to the separation distance of the data sites). Least-squares approximation with
radial basis functions is not subject of this course.
The method of (discrete) least squares has been developed by Gauss in 1794 for smoothing
data in connection with geodetic and astronomical problems.
Example 5.1
Given 5 function values f (xi ), i = 1, . . . , 5, of the function f (x) = (1 + x2 )−1 (see the
table below). We look for the discrete least-squares approximation φ̂ among all quadratic
polynomials φ.
Step 1: the choice of the basis functions is prescribed by the task description:
X3
2
ϕ1 = 1, ϕ2 = x, ϕ3 = x ⇒ φ(x) = ci ϕi (x).
i=1
i 1 2 3 4 5
xi −1 − 21 0 1
2 1
f (xi ) 0.5 0.8 1 0.8 0.5
70 Chapter 5. Least-squares Regression
with the solution (to 5 decimal places) ĉ1 = 0.94857, ĉ2 = 0.00000, ĉ3 = −0.45714,
yielding φ̂(x) = 0.94857 − 0.45714 x2 , x ∈ [−1, 1]. Under all quadratic polynomials,
φ̂(x) is the best approximation of f in the discrete least squares sense. For x = 0.8, we
obtain for instance φ̂(0.8) = 0.65600. The absolute error at x = 0.8 is |f (0.8)− φ̂(0.8)| =
4.6 · 10−2 . The results are plotted in figure 5.2.
1.1
1 fx
0.9
0.8
0.7
0.6 px
0.5
0.4
Figure 5.2: The 5 data points given by f (x) = (1+x2 )−1 (dots) and the best approximation
φ (solid line) of all quadratic polynomials in the discrete least squares sense.
Exercise 5.2
Given points (xi , f (xi )), i = 1, . . . , 4.
i 1 2 3 4
xi 0.02 0.10 0.50 1.00
f (xi ) 50 10 1 0
We look for the best approximation φ̂ in the discrete least squares sense of all functions
P
φ(x) = ck ϕk (x) and the following basis functions:
5.4. Weighted least-squares (?? not examined) 71
1. ϕ1 = 1, ϕ2 = x
2. ϕ1 = 1, ϕ2 = x, ϕ3 = x2
3. ϕ1 = 1, ϕ2 = x, ϕ3 = x2 , ϕ4 = x3
4. ϕ1 = 1, ϕ2 = 1/x
Give a graphical representation of the four different φ̂. What choice of the basis functions
yields the “best” result?
and the weighted least-squares solution is given by Eq. (5.5) when using the definition of
the scalar product, Eq. (5.7). Note that according to Eq. (5.8), the weighted least-squares
method does not minimize the square sum of the residuals, but the weighted square sum of
the residuals. The RMS error in the weighted least-squares approximation is
v
N
r u
hr̂, r̂i u 1 X
σRM S = =t wi r̂i2 .
N N
i=1
72 Chapter 5. Least-squares Regression
Chapter 6
Numerical Differentiation
6.1 Introduction
Numerical differentiation is the computation of derivatives of a function using a computer.
The function may be given analytically or on a discrete set of points x0 , . . . , xn , the nodes,
similarly to interpolation. The derivatives may be needed at arbitrary points or at the nodes
x0 , . . . , x n .
The basic idea of numerical differentiation of a function f (x) is to first interpolate f (x)
at the n + 1 nodes x0 , . . . , xn and then to use the analytical derivatives of the interpolating
polynomial φ(x) as an approximation to the derivatives of the function f (x).
If the interpolation error E(f ) = f (x) − φ(x) is small it may be hoped that the result of
differentiation, i.e., φ(k) (x) will also satisfactorily approximate the corresponding derivative
f (k) (x). However, if we visualize an interpolating polynomial φ(x) we often observe that it
oscillates about the function f (x), i.e., we may anticipate the fact that even though the devi-
ation between the interpolating polynomial φ(x) and the function f (x) (i.e., the interpolation
error E(x)) be small throughout an interval, still the slopes of the two functions may differ
significantly. Furthermore, roundoff errors or noise in the given data of alternating sign in
consecutive nodes could effect the calculation of the derivative strongly if those nodes are
closely spaced.
There are different procedures for deriving numerical difference formulae. In this course,
we will discuss three of them: (i) using Taylor series, (ii) Richardson extrapolation, and (iii)
using interpolating polynomials.
73
74 Chapter 6. Numerical Differentiation
f (k) (ξ) k
Rk (ξ) = h , x < ξ < x + h. (6.2)
k!
We want to determine an approximation of the first derivative f 0 (x) for a fixed x ∈ (a, b). If
we choose k = 2 then
1
f (x + h) = f (x) + h f 0 (x) + h2 f 00 (ξ), x < ξ < x + h, (6.3)
2
which can be rearranged to give
f (x + h) − f (x) 1
f 0 (x) = − h f 00 (ξ), x < ξ < x + h. (6.4)
h 2
Suppose f (x) is given at a set of equidistant nodes x0 < x1 . . . < xi < . . . with distance h.
Using the equation just derived we find for f 0 (xi )
fi+1 − fi
f 0 (xi ) = + O(h). (6.5)
h
This formula is referred to as the forward difference formula 1 . It has simple geometrical
interpretation: we are trying to find the gradient of a curve by looking at the gradient of a
short chord to that curve (cf. Fig 6.1).
We observe that as h is taken smaller and smaller, the forward difference result appears
to converge to the correct value f 0 (1) = 1. We also observe that each time we divide h by
a factor of 10, then the error decreases (roughly) by a factor of 10. Thus we might say the
forward difference formula has error roughly proportional to h. This is exactly what the
term O(h) in Eq. (6.5) tells us; the error of the forward difference formula is on the order
of O(h), i.e., it scales proportional to h for sufficiently small h. Difference formulae, which
1
Remember from real analysis that the derivative is defined as the limit
f (x + h) − f (x)
f 0 (x) = lim .
h→0 h
Compare this with the forward difference formula.
6.2. Numerical differentiation using Taylor series 75
h (f (1 + h) − f (1))/h error
0.1 1.105342410 -0.105342410
0.01 1.010050300 -0.010050300
0.001 1.001001000 -0.001001000
0.0001 1.000100000 -0.000100000
have an error O(h) are called first order formulae. Therefore, the forward difference formula
is sometimes also called the first order forward difference formula. Note, however, that if h
is taken much smaller, then rounding error becomes an issue in these calculations, see below.
Similarly,
f 00 (η)
f (x − h) = f (x) + f 0 (x)(−h) + (−h)2 , x − h < η < x. (6.6)
2
fi − fi−1
f 0 (xi ) = + O(h), (6.7)
h
which is called the (first order) backward difference formula for the first derivative. A geo-
metrical interpretation of the backward difference formula is shown in Fig 6.2.
Higher order (i.e., more accurate) schemes can be derived by Taylor series of the function f
at different points about the point xi . For instance, assume that f ∈ C 3 ([a, b]). Taking the
6.2. Numerical differentiation using Taylor series 77
f (1+h)−f (1−h)
h 2h error
0.1 1.005008325 0.105342410
0.01 1.000050001 0.010050300
0.001 1.000000500 0.000000500
0.0001 1.000000005 0.000000005
Table 6.2: Example (second order) central difference formula for the first derivative.
.
6.2. Numerical differentiation using Taylor series 79
Example 6.3 (Comparison of various difference formulae for the first derivative)
Use the data given in table 6.3 to estimate the acceleration at t = 16 s using the forward,
backward, and central difference formula for various step sizes. The data have been generated
with the formula
h 1.4 · 105 i
v(t) = 2000 ln − 9.8 t, 0 ≤ t ≤ 30, (6.16)
1.4 · 105 − 2100 t
where v is given in [m/s] and t is given in [s]. The results are shown in table 6.3.
Table 6.3: Comparison of forward, backward, and central difference formula. Note that
the central difference formula outperforms the forward and backward difference
formulae.
Since f (4) is continuous, the intermediate value theorem implies that there must be some ξ
between ξ1 and ξ2 such that
f (4) (ξ) 4
f (x + h) − 2f (x) + f (x − h) = f 00 (x) h2 + h , x − h < ξ < x + h. (6.22)
12
Solving for f 00 (x) gives
Hence, if we take F (h) as approximation to f 0 (x), the error is O(hp1 ). If h goes to zero,
F (h) → f 0 (x) — i.e. the method is consistent. Assume that we know the power p1 , which we
can obtain by the analysis of the previous sections. If we compute F (h) for two step sizes, h
and h/2, we have
where li (x) are the Lagrange basis functions associated to the nodes x0 . . . xn . Note that
the first term on the right-hand side is the Lagrange representation of the interpolating
84 Chapter 6. Numerical Differentiation
Figure 6.4: Approximating derivatives of f (x) (black), by first approximating with a inter-
polating polynomial p(x) (red), and subsequently differentiating the polynomial
(blue).
6.4. Difference formulae from interpolating polynomials (?? - not examined) 85
polynomial. Correspondingly, the second term on the right-hand side is the interpolation
error. We differentiate this equation r times and obtain
n
X (r) 1 dr h i
f (r) (x) = li (x) fi + ω n+1 (x) f (n+1)
(ξ) , (6.45)
(n + 1)! dxr
i=0
where ξ = ξ(x), i.e., ξ is a function of x. Hence, we can compute an estimate of the r-th
derivative f (r) (x) by computing the r-th derivative of the interpolating polynomial.
while f 0 (x) = ex . Table 6.4 compares values of p0 (x) with f 0 (x) for various values of x (limited
to 4 decimal places):
For instance, from quadratic polynomial interpolation through the data points (xi−1 , fi−1 ),
(xi , fi ), and (xi+1 , fi+1 ), we find
(x − xi )(x − xi+1 )
φ(x) = fi−1
(xi−1 − xi )(xi−1 − xi+1 )
(x − xi−1 )(x − xi+1 )
+ fi (6.50)
(xi − xi−1 )(xi − xi+1 )
(x − xi−1 )(x − xi )
+ fi+1 ,
(xi+1 − xi−1 )(xi+1 − xi )
86 Chapter 6. Numerical Differentiation
for x ∈ [xi , xi+1 ]. We differentiate φ(x) analytically and can use the result to obtain an
approximation of f 0 (x) at any point x ∈ [xi , xi+1 ]. Note that you should not evaluate φ0 (x)
at a point x outside the interval [xi , xi+1 ]. If we want to know an approximation of f 0 (x) at
the node xi , we evaluate φ0 (x) at x = xi :
hi hi−1 − hi hi−1
φ0 (xi ) = − fi−1 − fi + fi+1 , (6.51)
hi−1 (hi−1 + hi ) hi−1 hi hi (hi−1 + hi )
where hk = xk+1 − xk . For equally spaced intervals it is hk = h = constant, and we find
fi+1 − fi−1
φ0 (xi ) = . (6.52)
2h
This is the central difference formula for the first derivative, which has already been derived
previously making use of Taylor series. If we differentiate φ(x), Eq. (6.50), twice we obtain a
constant:
2 2 2
φ00 (x) = fi−1 − fi + fi+1 , x ∈ [xi .xi+1 ]. (6.53)
hi−1 (hi−1 + hi ) hi−1 hi hi (hi−1 + hi )
This constant can be used as an approximation of f 00 (x) at any point in the interval [xi , xi+1 ].
For equidistant points, it simplifies to
fi−1 − 2fi + fi+1
φ00 (x) = . (6.54)
h2
When we use Eq. (6.54) as an approximation to f 00 (xi ), we obtain the well-known central
difference formula for the second derivative. Correspondingly, if we use Eq. (6.54) as an ap-
proximation to f 00 (xi−1 ) and f 00 (xi+1 ) it is called the forward formula for the second derivative
and backward formula for the second derivative, respectively. Note that the backward formula
for the second derivative is in fact based on an extrapolation.
Of course, additional differentiation formulae can be obtained if we fit an algebraic poly-
nomial through 4 or more points and computing the derivative(s) analytically. Note that
6.4. Difference formulae from interpolating polynomials (?? - not examined) 87
the data points do not need to be equidistant and that the use of interpolating polynomials
allows the computation of derivative(s) at any point inside the data interval. For instance,
for equidistant nodes through 5 points, we obtain the approximation
1 h4
f 0 (xi ) ≈ fi−2 − 8fi−1 + 8fi+1 − fi+2 + f (5) (ξ), x − 2h < ξ < x + 2h. (6.55)
12h 30
Example 6.8 (Differentiation using interpolating polynomials)
The upward velocity of a rocket is given as a function of time in table 6.5. Use a cubic
interpolating polynomial to obtain an approximation of the acceleration of the rocket at
t = 16 s. Since we want to find the velocity at t = 16 s, and we are using a cubic interpolating
Table 6.5: Example: approximate first derivatives from a cubic interpolating polynomial
polynomial, we need to choose the four points closest to t = 16 s and that also bracket t = 16
s to evaluate it. The four points are t0 = 10, t1 = 15, t2 = 20, and t3 = 22.5. We use the
monomial representation of the cubic interpolating polynomial,
φ(t) = a0 + a1 t + a2 t2 + a3 t3 , (6.56)
Numerical Integration
7.1 Introduction
The numerical computation of definite integrals is one of the oldest problems in mathematics.
In its earliest form the problem involved finding the area of a region bounded by curved lines,
a problem which has been recognised long before the concept of integrals was developed in
the 17th and 18th century.
Numerical integration is often referred to as numerical quadrature, a name which comes
from the problem of computing the area of a circle by finding a square with the same area.
The numerical computation of two- and higher-dimensional integrals is often referred to as
numerical cubature. We will treat both topics in this chapter.
There are mainly three situations where it is necessary to calculate approximations to
definite integrals. First of all it may be so that the antiderivative of the function to be
integrated cannot be expressed in terms of elementary functions such as algebraic polynomials,
2
logarithmic functions, or exponential functions. A typical example is e−x dx. Secondly, it
R
might be that the antiderivative function can be written down but is so complicated that its
function values are better computed using numerical integration formulas. For instance, the
number of computations that must be carried out to evaluate
x
√
x2 + 2x + 1
Z
dt 1 1 x x
= √ log √ + √ arctan √ + arctan √
0 1 + t4 4 2 x2 − 2x + 1 2 2 2−x 2+x
using the ‘exact’ formula is substantial. A final reason for developing numerical integration
formulas is that, in many instances, we are confronted with the problem of integrating ex-
perimental data, which also includes data generated by a computation. In that case the
integrand is only given at discrete points and theoretical devices may be wholly inapplica-
ble. This case also arises when quadrature methods are applied in the numerical treatment
of differential equations. Most methods for discretizing such equations rest on numerical
integration methods.
89
90 Chapter 7. Numerical Integration
Let us begin by introducing some notations which will be used in this chapter. The idea
Rb
of numerical integration of a definite integral a f (x)dx is to look for an approximation of
the form Z b Xn
f (x)dx ≈ wi f (xi ). (7.1)
a i=0
The coefficients wi are called the “weights”; they depend on the location of the points xi
and on the integration domain [a, b]. The points xi are called the “nodes” (or “knots”). The
definite sum is called a “quadrature formula”. All quadrate formulas look like (7.1); they
only differ in the choice of the nodes and the weights. We will often write
Z b
I(f ; a, b) := f (x)dx (7.2)
a
n
X
Q(f ; a, b) := wi f (xi ) (7.3)
i=0
The difference I(f ; a, b)−Q(f ; a, b) =: E(f ; a, b) is called the “error of numerical integration”,
or briefly “quadrature error ”. Thus,
Equivalent to this is the following formulation: the rule Q(f ; a, b) approximating the definite
integral I(f ; a, b) has DOP d if
Z b n
wi xqi
X
xq dx = (7.9)
a i=0
7.2. Solving for quadrature weights 91
Figure 7.1: Trapezoidal rule (Section 7.4.1) and Simpson’s rule (Section 7.4.2) for integral
approximation.
for q = 0, 1, . . . , d, but
Z b n
wi xqi
X
q
x dx 6= (7.10)
a i=0
of a given degree of precision at least n. Assume that the nodes x0 , . . . , xn are fixed and
distinct numbers lying between a and b and prescribed in advance. Then the only degrees of
freedom are the n + 1 weights wi which can be determined in two ways:
92 Chapter 7. Numerical Integration
(b) Select the weights w0 , . . . , wn so that the rule integrates the monomials xj exactly for
j = 0, . . . , n. I.e. we obtain the n + 1 integration conditions:
Z b n
wi xji , ∀j = 0, . . . , n,
X
xj dx =
a i=0
(compare these conditions with the interpolation conditions). Again the degree of
precision will be at least n by construction.
It can be shown that both ways lead to one and the same formula, which is called “an
interpolatory quadrature formula”. Following (b), the integration conditions yields a linear
system for the unknown weights wi :
Rb
1 1 ··· 1 w0 dx
R ba
x0 x1 · · · xn w1 a xdx
.
. .. .. .. =
.. (7.11)
. . . . .
Rb n
xn0 xn1 · · · xnn wn a x dx
The matrix is the familiar “Vandermonde matrix”. It is invertible if and only if the nodes
xi are distinct. Obviously, quadrature formulas of interpolatory type using n + 1 nodes (i.e.
n + 1 function evaluations) have exact degree of precision d = n, i.e. they integrate exactly
polynomials of degree at most n.
Exercise 7.2
Construct a rule of the form
Z 1
I(f ; −1, 1) = f (x)dx ≈ w0 f (−1/2) + w1 f (0) + w2 f (1/2)
−1
Rather than fixing xi and determining wi , one could prescribe the weights wi and deter-
mining the nodes xi such that the integration conditions are satisfied for all polynomials of
degree ≤ n. However this yields a non-linear system of algebraic equations for the nodes xi ,
which is much more difficult to solve then the linear system of equations for wi .
A more exciting possibility is to determine weights and nodes simultaneously such that
the degree of precision d is as high as possible. The number of degrees of freedom is 2(n + 1),
so we should be able to satisfy integration conditions of up to degree 2n + 1 – these are the
Gauss quadrature rules, and are the best possible integration rules (in the sense that they
have highest degree of precision).
7.3. Numerical integration error – Main results 93
where a ≤ x0 < x1 < . . . < xn ≤ b are the nodes and wi are the weights and E is the
integration error. By the definition of degree of precision, if the quadrature rule has DOP d
then:
E(xj ; a, b) = 0, for all j = 0, . . . , d, (7.13)
and
E(xd+1 ; a, b) 6= 0. (7.14)
A general expression for E will now be stated. First we must introduce some notation. The
truncated power function is defined as
(
(x − s)d when x ≥ s
(x − s)d+ := . (7.15)
0 when x < s
Then,
1 b (d+1)
Z
E(f ; a, b) = f (s) K(s) ds, (7.16)
d! a
where
K(s) = E((x − s)d+ ; a, b). (7.17)
This result may appear somewhat ponderous, but it captures a beautiful idea: the error in
the quadrate formula of f is related to the integral of a function K(s) (called the Peano
kernel or the influence function) that is independent of f .
Under typical circumstances K(s) does not change sign on [a, b]. In this circumstance
the second law of the mean can be applied to extract f from inside the integral in (7.16).
Therefore E(f ; a, b) can be described as
b
f (d+1) (ξ)
Z
E(f ; a, b) = K(s) ds a < ξ < b, (7.18)
d! a
= κ f (d+1) (ξ), a < ξ < b, (7.19)
94 Chapter 7. Numerical Integration
which fixes κ:
E(xd+1 ; a, b)
κ= .
(d + 1)!
Hence for any (d + 1)-times continuously differentiable function f ∈ C d+1 ([a, b]), we have
E(xd+1 ; a, b) (d+1)
E(f ; a, b) = f (ξ), a<ξ<b (7.21)
(d + 1)!
so we can test our quadrature rule against xd+1 , and if it does well, it will do well for all f (·)!
In the case of quadrature rules with equidistant nodes (closed Newton-Cotes rules), we
can be even more specific:
b−a
xi = a + ih, h=
n
and degree of precision n. If n is odd (and f ∈ C n+1 ([a, b])) then the integration error can
be written:
hn+2 f (n+1) (ξ) n
Z
En (f ; a, b) = s(s − 1) · · · (s − n) ds. (7.22)
(n + 1)! 0
These expressions are used in the following sections to derive error estimates for closed
Newton-Cotes rules of varying degree.
(a) the end-points of the integration domain are nodes, i.e. a = x0 , b = xn . Then, we talk
about closed Newton-Cotes formulas.
7.4. Newton-Cotes formulas 95
(b) the end-points of the integration domain are not nodes, i.e., a < x0 and xn < b. Then,
we talk about open Newton-Cotes formulas.
First let us consider closed-type formulas. Thus, the integration domain [a, b] is divided into
n subintervals of equal length h at the points
xi = a + ih, i = 0, . . . , n,
where h = b−a
n . The number of nodes is s = n + 1. For different s we obtain different closed
Newton-Cotes formulas:
With s = 2 we only have 2 points: x0 = a and x1 = b. The weights w0 and w1 are the
solution of the linear system of equations
! ! Rb !
1 1 w0 1dx = b − a
= R ab 1 2 2
(7.24)
a b w1 a xdx = 2 (b − a )
b−a
The solution is w0 = w1 = 2 . Thus, the quadrature formula is
b 1
b − ah
Z X i
f (x)dx ≈ wi f (xi ) = f (a) + f (b) (7.25)
a 2
i=0
This quadrature formula is called the trapezoidal rule. The same result is obtained if we use
the Lagrange representation of the interpolation polynomial through the data points:
b Z b
x − x1 x − x0
Z
h
p1 (x) dx = f0 + f1 dx = f0 + f1 )
a a x0 − x1 x1 − x0 2
h
= f (a) + f (b) , where h = b − a.
2
h3 f 00 (ξ) 1
Z
1 3 (2)
E(f ; a, b) = s(s − 1) ds = − h f (ξ), a < ξ < b. (7.26)
2 0 12
We say that E(f ; a, b) = O(h3 ), which means that E(f ; a, b) tends to zero as h3 , h → 0. Note
that if f is a linear function, f 00 = 0, hence E(f ; a, b) = 0. This was to be expected, because
the degree of precision of the trapezoidal rule is, by construction, equal to 1. Remember that
the error estimates requires f ∈ C 2 ([a, b]).
96 Chapter 7. Numerical Integration
b 2
b − ah
Z X a+b i
f (x)dx ≈ wi f (xi ) = f (a) + 4f + f (b) (7.28)
a 6 2
i=0
This quadrature formula is called “Simpson’s rule”. Using the Lagrange representation of
the degree-2 interpolation polynomial yields
b Z bh
(x − x1 )(x − x2 ) (x − x0 )(x − x2 )
Z
p2 (x) dx = f0 + f1
a a (x0 − x1 )(x0 − x2 ) (x1 − x0 )(x1 − x2 )
(x − x0 )(x − x1 ) i h b−a
+ f2 dx = f0 + 4f1 + f2 , where h = .
(x2 − x0 )(x2 − x0 ) 3 2
It is n = 2, and the error of Simpson’s rule is
h5 f (4) (ξ) 2 h5
Z
E(f ; a, b) = s(s − 1)2 (s − 2) ds = − f (4) (ξ), a < ξ < b. (7.29)
4! 0 90
Therefore, the error is of the order O(h5 ). This is remarkable, because the trapezoidal rule,
which uses only one node less, is of the order O(h3 ). That is the reason why Simpson’s rule
is so popular and often used.
This formula is called “Boules’s rule” or “4/90-rule”. The error is of the order O(h7 ):
b − a 4h
Q(f ; a, b) = 2f0 − f1 + 2f2 = 2f0 − f1 + 2f2 , (7.37)
3 3
98 Chapter 7. Numerical Integration
Example 7.5
1
Given the function f (x) = 1+x 2 for x ∈ [0, 1]. We look for an approximation to π/4 by
integrating f over the interval [0, 1] using (a) the trapezoidal rule, (b) Simpson’s rule, (c)
Simpson’s 3/8-rule, and (d) the 4/90-rule.
(b) Simpson’s rule: Q(f ; 0, 1) = 16 (f (0) + 4f (1/2) + f (1)) = 16 (1 + 3.2 + 0.5) = 0.78333.
1 1
(c) Simpson’s 3/8-rule: Q(f ; 0, 1) = 8 (f (0) + 3f (1/3) + 3f (2/3) + f (1)) = 8 (1 + 2.7 +
27/13 + 0.5) = 0.78462.
1 1
(d) 4/90-rule: Q(f ; 0, 1) = 90 (7f (0) + 32f (1/4) + 12f (1/2) + 32f (3/4) + 7f (1)) = 90 (7 +
512/17 + 48/5 + 512/25 + 3.5) = 0.78553.
The corresponding errors are for (a) 3.5 · 10−2 , (b) 2.1 · 10−3 , (c) 7.8 · 10−4 , and (d) 1.3 · 10−4 .
by one of the Newton-Cotes formulas just developed and add the results. Hence, if we
use a closed Newton-Cotes formula with s nodes, the composite Newton-Cotes formula has
n(s − 1) + 1 nodes. The corresponding rules are known as composite Newton-Cotes rules.
7.5. Composite Newton-Cotes formulas 99
b n−1 n−1
h3 X (2)
Z X
f (x) dx = h fi+1/2 + f (ξi )
a 24
i=0 i=0
n−1
X b − a 2 (2)
=h fi+1/2 + h f (ξ), a < ξ < b.
24
i=0
Note that if
|f (2) (x)| ≤ M for all x ∈ [a, b], (7.42)
then by choosing h sufficiently small, we can achieve any desired accuracy (neglecting roundoff
errors). The rule
n−1
X b−a
Q(f ; a, b) = h fi+1/2 , h = (7.43)
n
i=0
so that
Z b n−1 i h3 X s−2
h Xh
f (x) dx = fi + fi+1 − f (2) (ξi )
a 2 12
i=0 i=0
h1 1 i b − a 2 (2)
= h f0 + f1 + f2 + . . . + fn−1 + fn − h f (ξ), a < ξ < b.
2 2 12
b−a h3 (2)
Q(f ; a, b) = (f (a) + f (b)) ; E(f ; a, b) = − f (ξ), a < ξ < b.
2 12
100 Chapter 7. Numerical Integration
b−a
with ai = a + (i − 1)h, bi = a + ih, h = m . Since bi − ai = h, we obtain
m
X bi − ai
Qm (f ; a, b) = (f (ai ) + f (bi )) =
2
i=1
h
= f (a) + 2f (a + h) + · · · + 2f (a + (m − 1)h) + f (b) .
2
h3
f 00 (ξ1 ) + f 00 (ξ2 ) + · · · + f 00 (ξm )
Em (f ; a, b) = −
12
with ξi ∈ (ai , bi ), i = 1, . . . , m.
Following the mean value theorem that any continuous function g on [a, b] with c1 ≤
g(x) ≤ c2 for x ∈ [a, b] takes on every value between c1 and c2 at least once, we find at least
one ξ ∈ [a, b] such that
1
f 00 (ξ) = f 00 (ξ1 ) + f 00 (ξ2 ) + · · · + f 00 (ξm ) .
m
Therefore, we obtain
h2 b − a h2
Em (f ; a, b) = − m f 00 (ξ) = − (b − a) f 00 (ξ), a < ξ < b,
12 m 12
which means that the total error is of the order O(h2 ), one order less than that of the simple
trapezoidal rule, which has O(h3 ).
Suppose the interval to which the nodes and weights of Q refer is [−1, 1]. Let {ξi : i = 1, . . . , s}
be the nodes of Q(f ; −1, 1) and {wi : i = 1, . . . , s} be the weights. Then,
b 1 1
b−a b−a
Z Z Z
f (x) dx = f (x(ξ)) dξ = g(ξ) dξ = Q(g; −1, 1) + E(g; −1, 1), (7.46)
a 2 −1 2 −1
where
b−a
x= (ξ + 1) + a. (7.47)
2
Hence,
b−a
Q(f ; a, b) = Q(g; −1, 1), g(ξ) := f (x(ξ)). (7.48)
2
That is if Q(f ; −1, 1) has nodes ξ and weights wi , then Q(f ; a, b) has nodes xi and weights
b−a
2 wi , where
b−a
xi = (ξi + 1) + a. (7.49)
2
Hence,
Z b s
X b−a
f (x) dx ≈ wi fi , fi = f (xi ). (7.50)
a 2
i=1
That means if we want to construct a quadrature rule for an arbitrary integration interval
[a, b] from given nodes and weights referring to another interval, say, [−1, 1], we only need
to find the (linear) mapping that maps [−1, 1] onto [a, b]. This is exactly Eq. (7.47). The
transformation allows to compute the nodes referring to the interval [a, b] and the Jacobian
of the mapping multiplied by the weights referring to [−1, 1] gives the weights referring to
[a, b].
We shall call two rules by the same name, provided that the abscissas and weights are
related by a linear transformation as above. For instance, we shall speak of Simpson’s rule
over the interval [−1, 1] and over [a, b].
Then, the resulting quadrature formula has DOP s − 1, i.e., it is exact for all polynomials of
degree at most s − 1.
If we do not prescribe nodes and weights, the above system of equations contains 2s free
parameters, namely s nodes and s weights. The system is linear in the weights and non-
linear in the nodes. We may use the 2s degrees of freedom to demand that the integration
102 Chapter 7. Numerical Integration
formula is exact for polynomials of degree at most 2s − 1, which are uniquely determined by
2s coefficients. That means we require that
Z b Xs
p(x)dx = wi p(xi ), (7.52)
a i=1
for all p ∈ P2s−1 , i.e., for all polynomials of degree at most 2s − 1. This is the idea behind
the so-called Gauss quadrature formulas. Of course, the question is whether the system of
non-linear equations (7.52) is uniquely solvable.
Suppose the integration interval is [−1, 1] and suppose there exists nodes and weights so
that E(f ; −1, 1) = 0 for all f ∈ P2s−1 . Let q ∈ Ps−1 arbitrary and consider the polynomial
The polynomial
ωs (x) := (x − x0 )(x − x1 ) · · · (x − xs−1 ) (7.54)
is the nodal polynomial; it has degree s. Moreover, it holds obviously Q(q · ωs ; −1, 1) = 0.
Hence, it must hold Z 1
q(x) ωs (x) dx = 0, for all q ∈ Ps−1 . (7.55)
−1
We define in C([−1, 1]) the scalar product
Z 1
hf, gi := f (x)g(x) dx. (7.56)
−1
Then, condition (7.55) means that ωs (x) is orthogonal to all polynomials q ∈ Ps−1 with
respect to the scalar product (7.56). We construct a system of orthogonal polynomials Li ,
i.e., (
0 for i 6= j
hLi , Lj i = . (7.57)
6= 0 for i = j
This can be done from the monomial basis xk using the Gram-Schmidt orthogonalization
procedure. With L0 (x) = 1 and L1 (x) = x, the solution is
n−1
X hxn , Li i
Ln (x) = xn − Li (x). (7.58)
hLi , Li i
i=0
It is P0 (x) = 1, P1 (x) = x, P2 (x) = 21 (3x2 − 1), etc. They can be computed recursively:
2n + 1 n
Pn+1 (x) = x Pn (x) − Pn−1 (x), n = 1, 2, . . . . (7.61)
n+1 n+1
Hence, Ln is up to a scaling factor identical to Pn . The Legendre polynomial Pn (x) has n
distinct zeros in the interval (−1, 1) and so has Ln . For given number of nodes s, we choose
the zeros of Ps (x) as nodes of the integration formula. For arbitrary p ∈ P2s−1 , we have by
polynomial division
p(x) = q(x)Ls (x) + r(x), (7.62)
where q ∈ Ps−1 and a remainder r ∈ Ps−1 . Due to the orthogonality, we have
Z 1 Z 1 Z 1
p(x) dx = q(x)Ls (x) dx + r(x) dx. (7.63)
−1 −1 −1
The first integral on the right-hand site is zero due to the orthogonality. Hence,
Z 1 Z 1
p(x) dx = r(x) dx. (7.64)
−1 −1
if and only if Z 1
r(x) dx = Q(r; −1, 1), for all r ∈ Ps−1 . (7.67)
−1
That the latter holds can be achieved by a suitable choice of the weights according to the
principle of interpolatory quadrature. It is for r ∈ Ps−1 :
Z 1 Z s−1
1 X s−1 Z
X 1
r(x) dx = r(xi ) li (x) dx = li (x) dx r(xi ). (7.68)
−1 −1 i=0 i=0 −1
and can be computed from the nodes x0 , . . . xs−1 . The result is the following:
104 Chapter 7. Numerical Integration
where li (x) is the i-th Lagrange polynomial associated with the nodes {xi }. Then,
Z 1 s−1
X
p(x) dx = wi p(xi ) for all p ∈ P2s−1 , (7.71)
−1 i=0
The weights of all Gauss-Legendre quadrature rules are positive. The sum of the weights
is always equal to 2, the length of the interval [−1, 1]. A Gauss-Legendre rule with s nodes
has DOP 2s − 1. This is the maximum degree of precision a quadrate rule with s nodes
can have. All nodes are in (−1, 1). The nodes are placed symmetrically around the origin,
and the weights are correspondingly symmetric. For s even, the nodes satisfy x0 = −xs−1 ,
x1 = −xs−2 etc., and the weights satisfy w0 = ws−1 , w1 = ws−2 etc. For s odd, the nodes
and weights satisfy the same relation as for s even plus we have x s+1 = 0. Notice that we
2
have shown before that the Gauss-Legendre rules are interpolatory rules! If we need to apply
a Gauss-Legendre rule to approximate an integral over the interval [a, b] we transform the
nodes and weights from the interval [−1, 1] to the interval [a, b] as explained in section 7.6.
Example 7.8
When applying a Gauss-Legendre formula with s nodes to a function f on the interval [−1, 1],
we obtain for
h4 (4)
R1
s = 2: −1 f (x)dx ≈ f (− √13 ) + f ( √13 ), E(f ; −1, 1) = 135 f (ξ), −1 < ξ < 1.
R1 1
√ √ h6 (6) (ξ),
s = 3: −1 f (x)dx ≈ 9 5f (− 0.6) + 8f (0) + 5f ( 0.6) , E(f ; −1, 1) = 15750 f
−1 < ξ < 1.
Obviously, the error estimates are better than for the Newton-Cotes formulas with the
Ps
same number of nodes. Since no integration rule of type i=1 wi f (xi ) can integrate ex-
Qs 2
actly the function i=1 (x − xi ) ∈ P2s , we see that Gauss rules are best in the sense that
they integrate exactly polynomials of a degree as high as possible with a formula of type
Ps
i=1 wi f (xi ). Therefore, Gauss formulas possess maximum degree of precision. There is no
s-point quadrature formula with degree of precision 2s.
7.8. Numerical integration error – Details (?? – not examined) 105
s nodes xi , i = 1, . . . , 2s − 1 weights wi , i = 1, . . . , 2s − 1
1 x0 = 0 w0 = 2
2 x0,1 = ± √13 w0 = w1 = 1
√
3 x0,2 = ± 0.6 w0 = w2 = 59
x1 = 0 w1 = 98
4 x0,3 = ±0.86113631 w0 = w3 = 0.34785485
x1,2 = ±0.33998104 w1 = w2 = 0.65214515
The corresponding integration errors are 1.5 · 10−2 , 1.5 · 10−3 , 1.3 · 10−4 , and 4.8 · 10−6 ,
respectively.
Exercise 7.10
Find an approximation to
3
sin2 x
Z
I= dx
1 x
using Gaussian quadrature with s = 3 nodes. Evaluate the same integral but now using
composed Gaussian quadrature with m = 2 subintervals and s = 3 nodes each.
where a ≤ x0 < x1 < . . . < xn ≤ b are the nodes and {wi } are the weights of the integration
formula and E is the integration error. Hence,
Z b n
X
E(f ; a, b) = f (x) dx − wi f (xi ), (7.74)
a i=0
106 Chapter 7. Numerical Integration
and
E(xq ; a, b) = 0, for all q ≤ d, (7.75)
and
E(xq+1 ; a, b) 6= 0. (7.76)
Suppose f ∈ C d+1 ([a, b]). Then, for any value of x and x̄ ∈ [a, b], we can write
where pd (x) comprises the first d + 1 terms, i.e., is a polynomial of degree d. Hence,
Z b Z b n
X
E(f ; a, b) = pd (x) dx + ed (x) dx − wi pd (xi ) − ed (xi )
a a i=0
Z b n
X Z b n
X
= pd (x) dx − wi pd (xi ) + ed (x) dx − wi ed (xi )
a i=0 a i=0
Z b n
X
= ed (x) − wi ed (xi ),
a i=0
because any interpolatory rule integrates a constant exactly, i.e., the sum of all weights must
be equal to b − a. Hence, for integration rules with positive weights, it holds
M (b − a)d+2
|E(f ; a, b)| ≤ . (7.84)
2d (d + 1)!
The error bounds (7.82) and (7.84) are often too conservative. A more useful form is obtained
when we start with the interpolation error: let
n
X 1
f (x) = li (x) f (xi ) + ωn+1 (x)f (n+1) (ξ), (7.85)
(n + 1)!
i=0
Then,
Z b n Z b
X 1
f (x) dx = wi f (xi ) + ωn+1 (x) f (n+1) (ξ) dx, (7.87)
a (n + 1)! a
i=0
where Z b
wi = li (x) dx. (7.88)
a
Note that ξ = ξ(x), i.e., we can’t take the term f (n+1) (ξ) out of the integral. Hence, even
if f (n+1) (x) is known analytically, we can’t evaluate the second term on the right-hand side,
i.e., the integration error, analytically. However, with
If no one of the abscissas xi lies in (a, b), for instance if we want to compute the integral
Z xi+1
f (x) dx, (7.91)
xi
the nodal polynomial ωn+1 (x) does not change sign in (a, b) and the second law of the mean
may be invoked to show that
b
f (n+1) (η)
Z
E(f ; a, b) = ωn+1 (x) dx. (7.92)
(n + 1)! a
108 Chapter 7. Numerical Integration
If the nodes are distributed equidistantly over [a, b], we can obtain other error estimates. First
observe that if x = a + b−a
n s, n ∈ N+ , then
b n
b−a
Z Z
f (x) dx = F (s) ds, (7.94)
a n 0
where
b−a
F (s) = f a+ s . (7.95)
n
Suppose f (x) is interpolated by a polynomial of degree n, which agrees with f (x) at the n + 1
equally spaced nodes xi in [a, b]. Then,
Z b n
X
F (s) ds = wi F (i) + En (F ; 0, n), (7.96)
a i=0
where Z b Z n
wi = li (x) dx = Li (s) ds, (7.97)
a 0
where Y s−j
Li (s) = (7.98)
i−j
j=0,j6=i
and
Z n
1
En (F ; 0, n) = s(s − 1) · · · (s − n) F (n+1) (ξ1 ) ds, 0 < ξ1 < n. (7.99)
(n + 1)! 0
Since s(s − 1) · · · (s − n) is not of constant sign on [0, n], we can’t apply the second law of
the mean. However, it can be shown that when n is odd, the error can be expressed in the
form which would be obtained if the second law of the mean could be applied:
n
F (n+1) (ξ2 )
Z
En (F ; 0, n) = s(s − 1) · (s − n) ds, n odd. (7.100)
(n + 1)! 0
where in both cases 0 < ξ2 < n. Since we assumed equally spaced nodes, we may write if
b−a
h= , xi = a + ih, i = 0, . . . , n : (7.102)
n
n
hn+2 f (n+1) (ξ)
Z
En (f ; a, b) = s(s − 1) · · · (s − n) ds, n odd (7.103)
(n + 1)! 0
and
n
hn+3 f (n+2) (ξ)
Z
n
En (f ; a, b) = (s − ) s(s − 1) · · · (s − n) ds, n even, (7.104)
(n + 2)! 0 2
where a < ξ < b in each case.
We return to the integration error E(f ; a, b) for any interpolatory quadrature rule. Instead
of
f (d+1) (ξ)
ed (x) = (x − x̄)d+1 , (7.105)
(d + 1)!
we use the integral remainder term for the Taylor series:
1 x
Z
ed (x) = (x − s)d f (d+1) (s) ds. (7.106)
d! x̄
Let x̄ = a. Then,
Z bZ x n
X Z xi
d! E(f ; a, b) = (x − s)d f (d+1) (s) ds dx − wi (xi − s)d f (d+1) (s) ds. (7.107)
a a i=0 a
It will be convenient to remove the x from the upper limit of the interior integral above. To
this end we introduce the truncated power function
(
(x − s)d when x ≥ s
(x − s)d+ := . (7.108)
0 when x < s
We have just proved the Peano kernel theorem (in fact, the full theorem is a bit more general
than what we proved here, though our development is sufficient for analysis of Newton-Cotes
rules).
d. Then,
1 b (d+1)
Z
E(f ; a, b) = f (s) K(s) ds, (7.110)
d! a
where
K(s) = E((x − s)d+ ; a, b). (7.111)
This result may appear somewhat ponderous, but it captures a beautiful idea: the error in
the quadrate formula of f is related to the integral of a function K(s) (called the Peano
kernel or the influence function) that is independent of f .
In typical circumstances, K(s) does not change sign on [a, b], so the second law of the mean
can be applied to extract f from inside the integral. This allows E(f ; a, b) to be described as
where κ is independent of f (x). Hence, to calculate κ, we may make the special choice
f (x) = xd+1 . Then,
E(xd+1 ; a, b) = κ (d + 1)!, (7.113)
which fixes κ, hence,
E(xd+1 ; a, b) (d+1)
E(f ; a, b) = f (ξ), a<ξ<b (7.114)
(d + 1)!
if K does not change sign in [a, b]. Note that positive weights wi is not sufficient to guarantee
that K does not change sign in [a, b].
is a useful approximation to the integral on the left for a reasonable large class of functions
of two variables defined over B.
7.9. Two-dimensional integration 111
(1) In one dimension only three different types of integration domains are possible: the
finite interval (which has been discussed before), the single infinite interval, and the
double infinite interval. In two dimensions we have to deal with a variety of domains.
(2) The behaviour of functions of two variables can be considerably more complicated than
that of functions of one variable and the analysis of them is limited.
(3) The evaluation of functions of two variables takes much more time.
Only for some standard regions the theory is developed much further and the following brief
discussion will be restricted to the two most important standard regions, namely
They allow to transform two-dimensional integrals into two iterated integrals. Arbitrary
domains B can sometimes be transformed into a standard region D by affine or other trans-
formations. Then, given cubature formulas for D can be transformed as well providing a
cubature formula for B.
To explain this, let B be a region in the x, y-plane and D be our standard region in the
u, v-plane. Let the regions B and D be related to each other by means of the transformation
x = φ(u, v),
y = ψ(u, v).
∂φ ∂φ
!
∂u ∂v
J(u, v) = det ∂ψ ∂ψ
(7.116)
∂u ∂v
does not vanish in D. Suppose further that a cubature formula for the standard domain D
is available, i.e.
ZZ Xs
h(u, v)dudv ≈ wi h(ui , vi ), (ui , vi ) ∈ D, (7.117)
D i=1
112 Chapter 7. Numerical Integration
where
xi = φ(ui , vi ), yi = ψ(ui , vi ), Wi = wi |J(ui , vi )|.
Thus,
ZZ s
X
f (x, y)dxdy ≈ Wi f (xi , yi ). (7.121)
B i=1
An important special case occurs when B and D are related by a non-singular affine trans-
formation:
x = a0 + a1 u + a2 v,
y = b0 + b1 u + b2 v.
Examples are the parallelogram which is an affine transformation of the standard square U
and any triangle which is an affine transformation of the standard triangle T .
Example 7.13
Given the s nodes (ui , vi ) and weights wi of a cubature formula w.r.t. to the standard triangle
T = {(u, v) : 0 ≤ u ≤ 1, 0 ≤ v ≤ 1 − u}. Calculate the nodes (xi , yi ) and weights Wi of a
cubature formula
Xs Z
Q(f ; B) = Wi f (xi , yi ) ≈ f (x, y)dx dy,
i=1 B
x = a0 + a1 u + a2 v; y = b0 + b1 u + b2 v.
7.9. Two-dimensional integration 113
Suppose now that Q1 is an s1 -point rule of integration over I1 and Q2 is an s2 -point rule of
integration over I2 :
Xs1 Z
Q1 (h, I1 ) = wi h(xi ) ≈ h dx, (7.123)
i=1 I1
Xs2 Z
Q2 (g, I2 ) = vi g(yi ) ≈ g dy. (7.124)
i=1 I2
wi and vi denote the weights of the quadrature formulas Q1 and Q2 , respectively. Then, by
the product rule of Q1 and Q2 we mean the s1 · s2 -point rule applicable to I1 × I2 and defined
by
s1 X
X s2
Q1 × Q2 (f ; I1 × I2 ) = wi vj f (xi , yj ) (7.125)
i=1 j=1
ZZ
≈ f (x, y) dxdy. (7.126)
I1 ×I2
114 Chapter 7. Numerical Integration
We can show that if Q1 integrates h(x) exactly over I1 , if Q2 integrates g(y) exactly over I2 ,
and if f (x, y) = h(x)g(y), then Q1 × Q2 will integrate f (x, y) exactly over I1 × I2 .
Example 7.16
Let B be the rectangle a ≤ x ≤ b, c ≤ y ≤ d. The evaluation of
ZZ
f (x, y)dxdy
B
with dR = dr dα. The affine transformation r = 12 (u + 1), α = π(v + 1) transforms the unit
square U = [−1, 1] × [−1, 1] onto R. Its Jacobian is J2 (u, v) = π2 . Therefore, we obtain
Z Z
I= f |J1 |dR = f |J1 ||J2 |dU,
R U
R v=1
Let v=−1 g(u, v)dv =: h(u). Then, we may write
Z u=1 s
X
I= h(u)du ≈ wi h(ui ),
u=−1 i=1
where ui and wi denote the nodes and weights of the Gauss-Legendre quadrature w.r.t. the
interval [−1, 1]. Because of the definition of h(u) we obtain
Xs Xs Z v=1
I≈ wi h(ui ) = wi g(ui , v) dv.
i=1 i=1 v=−1
For fixed i, g(ui , v) is a function of v only, thus we can again apply the Gauss-Legendre
quadrature formula to compute the integral:
Z v=1 s
X
g(ui , v) dv ≈ wj g(ui , vj ).
v=−1 j=1
This yields
s X
X s
I≈ wi wj g(ui , vj ),
i=1 j=1
u+1 u+1 1
with g = f |J1 | |J2 |, x = 2 cos(π(v+1)), y = 2 sin(π(v+1)), r = 2(u+1) , and α = π(v+1).
We can generalize the product rule approach. If the region B is sufficiently simple, the
integral ZZ
I= f (x, y) dxdy
B
may be expressed as an iterated integral of the form
Z b Z u2 (x) Z b Z u2 (x)
I= f (x, y) dxdy =: g(x) dx, g(x) := f (x, y) dy (7.128)
a u1 (x) a u1 (x)
The double subscripts on the right reflect the fact that the abscissas and weights of Q2 must
be adjusted for each value of i to the interval u1 (xi ) ≤ y ≤ u2 (xi ). Thus
s1
X s2
X
I= wi vji f (xi , yji ) (7.131)
i=1 j=1
116 Chapter 7. Numerical Integration
is an s1 · s2 -point rule for I. Note that this is the same formula that results when applying the
product rule Q1 × Q2 to the transformed integral over the square [a, b] × [a, b] with Jacobian
u2 (x) − u1 (x)
J(x) = .
b−a
E(φi ; I) = 0, i = 1, . . . , M. (7.133)
Equations (7.133) are called moment equations. For a fixed choice of the basis {φ1 , . . . , φM }
and of the abscissas (x1 , y1 ), . . . , (xs , ys ) they define a system of linear equations
s
X
wi φj (xi , yi ) = Iφj , j = 1, . . . , M. (7.134)
i=1
Then, Q is called an interpolatory cubature formula, provided the system has a unique
solution.
In one dimension, Q is identical to the quadrature formula obtained by interpolation with
polynomials from Pd at the distinct nodes x1 . . . . , xs . This relationship does not generally
hold in two (or more) dimensions!
For arbitrary given distinct points (x1 , y1 ), . . . , (xs , ys ) the moment equations usually
have no unique solution. Thus, when trying to construct s-point interpolatory cubature
formulas, the linear system has to be solved not only for the weights w1 , . . . , ws , but also
7.9. Two-dimensional integration 117
for the nodes (x1 , y1 ), . . . , (xs , ys ). Then the system (7.134) is non-linear in the unknowns
(x1 , y1 ), . . . , (xs , ys ). Each node (xi , yi ) introduces three unknowns: the weight wi and the
two coordinates of each node (xi , yi ). Therefore, the cubature formula Q to be constructed
has to satisfy a system of d+2
d non-linear equations in 3s unknowns. For non-trivial values
of s, these non-linear equations are too complex to be solved directly.
However, Chakalov’s theorem guarantees at least that such a cubature formula exists:
Unfortunately, Chakalov’s theorem is not constructive, and therefore useless for the practical
construction of cubature formulas. Therefore, the construction of interpolatory cubature
formulas is an area of ongoing research. For most of the practical applications, however,
product rules as discussed in section 7.9.1 can be derived.
118 Chapter 7. Numerical Integration
Chapter 8
8.1 Introduction
Many problems in technical sciences result in the task to look for a differentiable function
y = y(x) of one real variable x, whose derivative y 0 (x) fulfils an equation of the form
Equation (8.1) is called ordinary differential equation. Since only the first derivative of y
occurs, the ordinary differential equations (8.1) is of first order. In general it has infinitely
many solutions y(x). Through additional conditions one can single out a specific one. Two
types of conditions are commonly used, the so-called initial condition
y(x0 ) = y0 , (8.2)
i.e. the value of y at the initial point x0 of I is given, and the so-called boundary condition
where g is a function of two variables. Equations (8.1),(8.2) define an initial value problem
and equations (8.1),(8.3) define a boundary value problem.
More generally, we can also consider systems of p ordinary differential equations of first
order
y10 (x) = f1 (x, y1 (x), y2 (x), . . . , yp (x))
y20 (x) = f2 (x, y1 (x), y2 (x), . . . , yp (x))
.. (8.4)
.
yp0 (x) = fp (x, y1 (x), y2 (x), . . . , yp (x))
119
120 Chapter 8. Numerical Methods for Solving Ordinary Differential Equations
for p unknown functions yi (x), i = 1, . . . , p of a real variable x. Such systems can be written
in the form (8.1) when y 0 and f are interpreted as vector of functions:
0
y1 (x) f1 (x, y1 (x), . . . , yp (x))
y 0 (x) := ... , f (x, y(x)) := ..
. (8.5)
.
yp0 (x) fp (x, y1 (x), . . . , yp (x))
In addition to ordinary differential equations of first order there are ordinary differential
equations of the mth order , which have the form
The corresponding inital value problem is to determine a function y(x) which is m-times
differentiable, satisfies (8.7), and fulfils the initial condition:
(i)
y (i) (x0 ) = y0 , i = 0, 1, . . . , m − 1. (8.8)
z1 (x) : = y(x)
z2 (x) : = y 0 (x),
.. (8.9)
.
zm (x) : = y (m−1) (x),
the ordinary differential equation of the mth order can always be transformed into an equiv-
alent system of m first order differential equations
z10 z2
. ..
..
= .
. (8.10)
0
zm−1 zm
zm0 f (x, z1 , z2 , . . . , zm )
In this chapter we will restrict to initial value problems for ordinary differential equations of
first order, i.e. to the case of only one ordinary differential equation of first order for only
one unknown function. However, all methods and results holds for systems of p ordinary
differential equations of first order, as well, provided quantities such as y and f (x, y) are
interpreted as vectors, and | · | as norm k · k. Moreover, we always assume that the initial
value problem is uniquely solvable. This is guaranteed by the following theorem:
8.1. Introduction 121
Figure 8.1: Graphical interpretation of Lipschitz continuity: finding a cone at each point
on the curve such that the function does not intersect the cone.
for all x ∈ [a, b] and all y1 , y2 ∈ R (“Lipschitz condition”). L is called the “Lipschitz constant”
of f . Then for every x0 ∈ [a, b] and every y0 ∈ R there exists exactly one solution of the
initial value problem (8.1),(8.2).
This theorem has a geometric interpretation illustrated in Figure 8.1: if at every point on
the curve we can draw a cone (in gray) with some (finite) slope L, such that the curve does
not intersect the cone, then the function is Lipschitz continuous. The function sketched in
the figure is clearly Lipschitz continuous.
Condition (8.11) is fulfilled if the partial derivative fy := ∂f
∂y exists on the strip G and is
continuous and bounded there. Then we can choose
L = max fy (x, y).
(x,y)∈G
xi are often equidistant, i.e. xi = x0 + i h, where h is the step size. We will discuss various
numerical methods and will examine whether and how fast η(x) converges to y(x) as h → 0.
We will intensively make use of the results of Chapter 1 and Chapter 4.
The numerical methods for solving the initial value problem (8.1), (8.2) differ in what quadra-
ture formula Q(f, y; xi , xi+1 ) is used to compute the integral on the right-hand side of equa-
tion (8.13):
The difference y(xi+1 ) − η̄(xi+1 ) is then equal to the quadrature error E(f, y; xi , xi+1 ). In
the context of ODE it is called local discretization error or local truncation error of step i + 1
and denoted by i+1 :
For applications it is important how fast the local discretization error decreases as h → 0.
This is expressed by the order of the local discretization error. It is the largest number p
such that
i+1
lim p < ∞. (8.17)
h→0 h
We simply write i+1 = O(hp ) which means nothing else but i+1 ≈ C hp with some C ∈ R.
Usually y(xi ) is not known, and (8.15) cannot be used directly. Therefore, an additional
approximation has to be introduced, for instance, by replacing the terms with y on the
right-hand side of (8.15) by the corresponding approximations η, which yields:
where y(x0 ) = η(x0 ) = y0 . To keep notation simple we will often write yj , ηj instead of
y(xj ), η(xj ).
Mostly we want to know how large the difference between yi+1 and its approximation
ηi+1 is. This question is not answered by the local discretization error because it only tells
us how good the numerical integration has been performed in the corresponding step of the
algorithm; the numerical integration errors of the previous steps and also the errors introduced
by replacing terms with y by the approximations η on the right-hand side of (8.15) are not
taken into account. The difference yi+1 −ηi+1 is called global discretization error or sometimes
global truncation error at xi+1 , denoted by ei+1 :
The order of the global discretization error is defined in the same way as the order of the
local discretization error. It can be shown that the global order is always one less than the
local order. It can be estimated using approximations with different step size: If ηhj (xi ) is
the approximation to y(xi ) obtained with a method of global order O(hpj ), and step size hj ,
j = 1, 2, we can proof that
This estimate of the global discretization error can be used to obtain an improved approxi-
mation η̄h1 (xi ):
p
h2
h1 ηh1 (xi ) − ηh2 (xi )
η̄h1 (xi ) = ηh1 (xi ) + ēi,h1 = p .
h2
h1 −1
and
2p ηh (xi ) − η2h (xi )
η̄h (xi ) = .
2p − 1
In addition to discretization errors, there are rounding errors which have to be taken into
account. Rounding errors are unavoidable since all numerical computations on a computer
are done in floating-point arithmetic with a limited number of decimal digits. They behave
like
ri = O(h−q ),
124 Chapter 8. Numerical Methods for Solving Ordinary Differential Equations
with some q > 0, where the error constant depends on the number of decimal digits in the
floating-point arithmetic. Note that the rounding error increases with decreasing step size h,
while the discretization error decreases with decreasing h. Therefore, the step size h has to be
chosen carefully. Investigations have shown that usually the choice hL = 0.05, . . . , 0.20 yields
good results, where L denotes the Lipschitz constant of f (cf. (8.11)). For more information
about rounding errors we refere to Stoer and Bulirsch (1993).
In this chapter we will focus on single-step methods and multistep methods. Single-step
methods are of the form
where the function Φ can be linear or non-linear in its arguments. If Φ does not depend on
ηi+1 , the method is called explicit, otherwise implicit. Characteristic for single-step methods
is that they use only one previously calculated approximation ηi to get an approximation
ηi+1 . Multistep methods (i.e., s-step methods with s > 1) make use of s + 1, s ≥ 1 previously
calculated approximations ηi−s , ηi−s+1 , . . . , ηi−1 , ηi to calculate ηi+1 . The linear multistep
methods have the form
s
X s
X
ηi+1 = as−k ηi+1−k + h bs−k fi+1−k , i = 0, 1, . . . , n − 1, (8.21)
k=1 k=0
with some real coefficients aj , bj . If bs = 0 they are called explicit, otherwise implicit.
A third group of numerical method are so-called extrapolation methods, which are similar
to Romberg integration methods; they are beyond the scope of this lecture. The reader is
referred to Stoer and Bulirsch (1993).
Among the single-step methods we will discuss the methods of Euler-Cauchy, the method
of Heun, and the classical Runge-Kutta method. Among the multistep methods we will dis-
cuss the method of Adams-Bashforth and the method of Adams-Moulton. In addition we
will discuss a subclass of single-step and multistep methods, the so-called predictor-corrector
(0)
methods. These are methods which first determine an approximation ηi+1 using a single-step
or a multistep method; the corresponding formula is called predictor. Then, the approxima-
(0)
tion ηi+1 is improved using another single-step or a multistep method (the so-called “correc-
(1) (2) 0 (k )
tor”), yielding approximations ηi+1 , ηi+1 , . . ., ηi+1 . For instance, the methods of Heun and
Adams-Moulton belong to the class of predictor-corrector methods.
What method is used to solve a given initial value problem depends on several factors,
among them are the required accuracy, the computer time and memory needed, the flexibility
w.r.t. the step size h, and the number of function evaluations. We will give some hints for
practical applications.
8.3. Single-step methods 125
y1 = y0 + h f (x0 , y0 ) + EC
1 = η1 + EC
1 ,
with
η1 := y0 + h f (x0 , y0 ).
When integrating over [x1 , x2 ] we obtain correspondingly
Z x2
y2 = y1 + f (t, y(t)) dt = y1 + h f (x1 , y1 ) + EC
2 .
x1
η2 = η1 + h f (x1 , η1 ).
ηi+1 = ηi + h f (xi , ηi ),
1 2 00 (8.23)
EC
i+1 = h y (ξi ), ξi ∈ [xi , xi+1 ], i = 0, . . . , n − 1.
2
The scheme (8.23) is called forward Euler-Cauchy method. The local discretization error has
order 2, and the global discretization error has order 1. The latter means that if the step
size in the Euler-Cauchy method is reduced by a factor of 21 , we can expect that the global
discretization error will be reduced by a factor of 12 . Thus, the forward Euler-Cauchy method
converges for h → 0, i.e.,
lim (y(x) − η(x)) = 0.
h→0
126 Chapter 8. Numerical Methods for Solving Ordinary Differential Equations
Figure 8.2: Solution space for the ODE y 0 = f (t, y). Exact solutions with various initial
conditions are plotted (black lines). The progress of 4 steps of forward Euler-
Cauchy is shown (red arrows).
Graphically we can plot the path of forward Euler-Cauchy in the solution space, see
Figure 8.2. The black lines in this figure correspond to exact solutions of the ODE with
different initial conditions. The thick black line is the exact solution for the initial condition
we are interested in, ỹ. Using the ODE y 0 = f (x, y) the exact derivative of the solution y can
be computed at every point in this space. Forward Euler starts from the initial condition, and
takes a linear step using the initial derivative. Because the exact solution is curved, forward
Euler lands a little to the side of the exact solution. The next step proceeds from this new
point. Over a number of steps these errors compound.
Example 8.2 (Forward Euler-Cauchy)
Given the initial value problem y 0 = y, y(0) = 1. We seek an approximation to y(0.5) using
the forward Euler-Cauchy formula with step size h = 0.1. Compare the approximations with
the true solution at the nodes.
Solution: With f (x, y(x)) = y, we obtain the recurrence formula
ηi+1 = (1 + h)ηi , i = 0, . . . , 5.
The nodes are xi = x0 + ih = 0.1 i, i = 0, . . . , 5. With h = 0.1 and retaining four decimal
places, we obtain
8.3. Single-step methods 127
i 0 1 2 3 4 5
xi 0 0.1 0.2 0.3 0.4 0.5
ηi 1 1.1 1.21 1.331 1.4641 1.61051
yi 1 1.1052 1.2214 1.3499 1.4918 1.6487
|yi − ηi | 0 5.3(-3) 1.1(-2) 1.9 (-2) 2.8(-2) 3.8 (-2)
The exact solution of the initial value problem is y(x) = ex , the correct value at x = 0.5
is y(0.5) = 1.64872. Thus, the absolute error is eEC (0.5) = 3.8(−2). A smaller step size
yields higher accuracies. Taking e.g. h = 0.005, we obtain η(0.5) = 1.6467, which is correct
to 2.1(−3).
step size h number of steps n η(3) eEC (0.3) O(h) ≈ Ch, C = 0.256
1 3 1.375 0.294390 0.256
1
2 6 1.533936 0.135454 0.128
1
4 12 1.604252 0.065138 0.064
1
8 24 1.637429 0.031961 0.032
1
16 48 1.653557 0.015833 0.016
1
32 96 1.661510 0.007880 0.008
1
64 192 1.665459 0.003931 0.004
1
the approximation of y(3) descreases by about 2 when the step size h is reduced by 12 . The
error constant can be estimated to C ≈ 0.256.
Instead of using the node xi we may use xi+1 yielding the so-called backward Euler-Cauchy
formula:
ηi+1 = ηi + h f (xi+1 , ηi+1 ), i = 0, 1, . . . , n − 1. (8.24)
Obviously it is an implicit formula since ηi+1 also appears on the right-hand side of the
equation, in contrast to the explicit forward Euler-Cauchy formula (8.23). For general f we
have to solve for the solution ηi+1 by iteration. Since equation (8.24) is of the form
ηi+1 = ϕ(ηi+1 ),
we can do that with the aid of the fixed-point iteration of Chapter 1. That means we start
(0) (0)
with an approximation ηi+1 , e.g. ηi+1 = ηi + h f (xi , ηi ), and solve the equation iteratively:
(k+1) (k)
ηi+1 = ϕ ηi+1 , k = 0, 1, . . . , k0 .
128 Chapter 8. Numerical Methods for Solving Ordinary Differential Equations
It can be shown that under the conditions of Theorem 8.1 and for hL = 0.05, . . . , 0.20 the
iteration converges. Mostly, one iteration step is already sufficient.
and, correspondingly,
h
ηi+1 = ηi + f (xi , ηi ) + f (xi+1 , ηi+1 ) . (8.25)
2
Equation (8.25) defines an implicit single-step method which is called the trapezoidal rule.
The local discretization error is
h3 00
H
i+1 = − f (ξi , y(ξi )), ξi ∈ [xi , xi+1 ], i = 0, . . . , n − 1. (8.26)
12
The right-hand side of (8.25) still contains the unknown value ηi+1 . We can solve it using
the fixed-point iteration:
(k) h (k−1)
ηi+1 = ηi + f (xi , ηi ) + f (xi+1 , ηi+1 ) , k = 1, 2, . . . , k0 . (8.27)
2
As starting value for the iteration we use the approximation of the forward Euler-Cauchy
method:
(0)
ηi+1 = ηi + h f (xi , ηi ), i = 0, 1, . . . , n − 1, (8.28)
Equations (8.27),(8.28) define a predictor-corrector method, which is called Method of Heun.
The forward Euler-Cauchy formula (8.28) is used as predictor ; it yields a first approximation
(0)
ηi+1 . The trapezoidal rule is used to correct the first approximation and, therefore, is called
corrector (equation (8.27)). A graphical summary is shown in Figure 8.3.
It can be shown that the corrector converges if hL < 1, where L is the Lipschitz constant
of the function f (cf. (8.11)). As already mentioned, in practical applications the step size is
chosen such that hL ≈ 0.05, . . . , 0.20.
8.3. Single-step methods 129
Figure 8.3: Graphical representation of the predictor-corrector in the solution space, with
exact solutions plotted (block lines). A prediction is made using the forward
Euler-Cauchy formula (thin red line). The solution gradient at the prediction
is evaluated (thick red line). This gradient is averaged with the initial solution
gradient, and on this basis a new correction step is made (blue line). The final
result is much closer to the truth than the predictor.
130 Chapter 8. Numerical Methods for Solving Ordinary Differential Equations
The local discretization error of the Heun method is identical to the quadrature error
of the trapezoidal rule, and given by equation (8.26). Since we do not know exactly the
H := η (k0 )
approximation ηi+1 , an additional iteration error, δi+1 i+1 − ηi+1 , is introduced, and
the total local discretization error becomes H H
i+1 + δi+1 . It can be shown that the total
local discretization error is still of the order O(h3 ), if hL ≈ 0.05, . . . , 0.20 is chosen, even
with k0 = 0. Thus, the global discretization error is of the order O(h2 ) (see e.g. Stoer and
Bulirsch (1993)). The latter means that when reducing the step size by a factor 21 , the global
discretization error will be reduced by a factor of about 14 .
The nodes are xi = x0 + ih = 0.1 i, i = 0, . . . , 5. With h = 0.1 and retaining five decimal
places, we obtain the results shown in the next table. The absolute error is 5.9 × 10−4 ,
i 0 1 2 3 4 5
xi 0 0.1 0.2 0.3 0.4 0.5
ηi 1 1.10525 1.22158 1.35015 1.49225 1.64931
(0)
ηi+1 1.1 1.21578 1.34374 1.48517 1.64148
(1)
ηi+1 1.105 1.22130 1.34985 1.49192 1.64894
(2)
ηi+1 1.10525 1.22158 1.35015 1.49225 1.64931
yi 1 1.1052 1.2214 1.3499 1.4918 1.6487
|yi − ηi | 0 0.8 × 10−5 1.8 × 10−4 2.9 × 10−4 4.3 × 10−4 5.9 × 10−4
thus the approximation is about 2 decimals more accuracte than when using the forward
Euler-Cauchy method.
Exercise 8.7
We seek an error estimate for η(0.2) obtained by the method of Heun. We know ηh (0.2) =
1.2217. Repeat the calculation with step size 2h = h̃ = 0.2. Estimate the global discretization
8.3. Single-step methods 131
error eH
h (0.2) and calculate an improved approximation η̄h (0.2). Compare it with the exact
solution.
The nodes tj and weights wj are determined such that ηi+1 agrees with the Taylor series
expansion of yi+1 at xi up to terms of order O(h4 ). This yields a linear system of equations
which has two equations less than the number of unknown parameters; so two parameters may
be chosen arbitrarily. They are fixed such that the resulting formula has certain symmetry
properties w.r.t. the nodes and the weights. The result is
1 1 1 1
w1 = , w2 = , w3 = , w4 = ,
6 3 3 6
h h
t 1 = xi , t2 = xi + , t3 = xi + , t4 = xi + h = xi+1 .
2 2
Therefore, the classical Runge-Kutta formula to perform the step from xi to xi+1 is given by
1
ηi+1 = ηi + (k1,i + 2k2,i + 2k3,i + k4,i ) , i = 0, . . . , n − 1, η(x0 ) = y(x0 ) = y0 ,
6
132 Chapter 8. Numerical Methods for Solving Ordinary Differential Equations
with
k1,i = h · f (xi , ηi ),
h k1,i
k2,i = h · f (xi + , ηi + ),
2 2
h k2,i
k3,i = h · f (xi + , ηi + ),
2 2
k4,i = h · f (xi + h, ηi + k3,i ).
Obviously, per step four evaluations of the function f are required. That is the price we
have to pay for the favorable discretization error. It may be considerable if the function f is
complicated.
It is very difficult to specify the optimal choice of the step size h. Mostly, hL ≈ 0.05 . . . 0.20
will give good accuracies. After each step it is possible to check whether hL is still in the
given range: we use the estimate
k2,i − k3,i
hL ≈ 2 .
k1,i − k2,i
This enables to vary the step size h during the calculations. It can be shown that, when
f does not depend on y, the classical Runge-Kutta formula corresponds to using Simpson’s
3/8-rule (cf. Chapter 4) for evaluating the integral on the right-hand side of (8.13).
Higher-order Runge-Kutta formulas have also been derived, e.g. the Runge-Kutta-Butcher
formula (m = 6) and the Runge-Kutta-Shanks formula (m = 8). The general structure of all
Runge-Kutta formulas is
m
X
ηi+1 = ηi + wj · kj,i
j=1
m
!
X
kj,i = h · f tj , ηi + aj,n · kn,i , j = 1, . . . , m
n=1
e0.2 = 1.22140, the approximation agrees with the exact solution up to 5 decimal places.
Example 8.9 (Classical Runge-Kutta: Global discretization error and step size)
Consider the initial value problem y 0 = x−y
2 , y(0) = 1 over the interval [0, 3] using the classical
Runge-Kutta method with step sizes h = 1, 21 , . . . , 18 and calculate the global discretization
x
error. The exact solution is y(x) = 3e− 2 − 2 + x. The results are shown in the next table.
step size h number of steps n η(3) eH (0.3) O(h) ≈ Ch4 , C = −6.14 × 10−4
1 3 1.670186 −7.96 × 10−4 −6.14 × 10−4
1
2 6 1.6694308 −4.03 × 10−5 −3.84 × 10−5
1
4 12 1.6693928 −0.23 × 10−5 −0.24 × 10−5
1
8 24 1.6693906 −0.01 × 10−5 −0.01 × 10−5
Since the global discretization error is of the order O(h4 ) we can expect that when reducing
the step size by a factor of 21 , the error will reduce by about 16
1
.
134 Chapter 8. Numerical Methods for Solving Ordinary Differential Equations
Exercise 8.10
Solve the equation y 0 = −2x − y, y(0) = −1 with step size h = 0.1 at x = 0.1, . . . , 0.5
using the classical Runge-Kutta method. The analytical solution is y(x) = −3e−x − 2x + 2.
Calculate the global discretization error at each node.
which is obtained by formally integrating the ODE y 0 = f (x, y(x)). To evaluate the integral
on the right-hand side we replace f by the interpolating polynomial Ps of degree s through
the past s + 1 points (xi−s+j , fi−s+j ), j = 0, . . . , s:
s
X
Ps (x) = fi−s+j · lj (x),
j=0
where the integral is calculated exactly. Note that [xi−s , xi ] is the interpolation interval to
determine Ps , but Ps is used to integrate over [xi , xi+1 ]. That means we extrapolate! The
8.4. Multistep methods 135
step i node xi ηi fi
0 0 1.00000000 1.00000000
1 0.1 1.10517092 1.10517092
2 0.2 1.22140276 1.22140276
3 0.3 1.34985881 1.34985881
4 0.4 1.49182046
The exact solution is y(0.4) = e0.4 = 1.49182470, i.e. the absolute global discretization
error at x = 0.4 is 4.24 × 10−6 . Computing η4 with the classical Runge-Kutta formula,
we obtain 1.491824586, which has an absolute error of 1.14 × 10−7 . That is, in this case
the classical Runge-Kutta method, which has the same local order O(h5 ) than the Adams-
Bashforth method yields a higher accuracy. The reason is that the Adams-Bashforth method
is based on extrapolation which yields larger local errors especially when the step size h is
large.
136 Chapter 8. Numerical Methods for Solving Ordinary Differential Equations
In the same way we can also derive an implicit multistep formula if the node xi+1 is used to
construct the interpolating polynomial Ps instead of the node xi−s . In that case, f (x, y(x))
is approximated by the interpolating polynomial Ps of degree s through the s + 1 points
(xi−s+j , fi−s+j ), j = 1, . . . , s + 1. Then, we avoid to extrapolate but the price we have to
pay is that ηi+1 also appears on the right-hand side, so we have to iterate in order to solve
the equation. One example of an implicit multistep formula is the Adams-Moulton formula
with s = 4:
h
ηi+1 = ηi + (251fi+1 + 646fi − 264fi−1 + 106fi−2 − 19fi−3 ) .
720
The fixed-point iteration yields
(k+1) h (k)
ηi+1 = ηi + 251f (xi+1 , ηi+1 ) + 646fi − 264fi−1 + 106fi−2 − 19fi−3 , k = 0, . . . , k0 .
720
The iteration converges if hL < 360
251 . However, when continuing the iteration on the corrector,
the result will converge to a fixed point of the Adams-Moulton formula rather than to the
ordinary differential equation. Therefore, if a higher accuracy is needed it is more efficient to
reduce the step size. In practical applications, mostly hL ≈ 0.05 . . . 0.20 is chosen and two
iteration steps are sufficient. The Adams-Moulton formula has the local order O(h6 ) and the
global order O(h5 ).
step i node xi ηi fi
0 0 1.00000000 1.00000000
1 0.1 1.10517092 1.10517092
2 0.2 1.22140276 1.22140276
3 0.3 1.34985881 1.34985881
4 (A-B) 0.4 1.49182046 1.49182046
4 (A-M) 0.4 1.49182458
1.49182472
The iteration even stops after k0 = 1. The exact solution is y(0.4) = e0.4 = 1.49182470,
i.e. the absolute global error at x = 0.4 is 0.2 × 10−7 . Thus, Adams-Moulton yields in this
case a higher accuracy than the classical Runge-Kutta and the Adams-Bashforth method.
Explicit multistep methods such as the Adams-Bashforth have the disadvantage that due
to extrapolation the numerical integration error may become large, especially for large step
8.5. Stability and convergence 137
size h. Therefore, such formulas should only be used as predictor and should afterwards be
corrected by an implicit multistep formula which is used as corrector. For instance, when
using Adams-Bashforth as predictor (local order O(h5 )) and Adams-Moulton as corrector
(local order O(h6 )), we obtain to perform the step from xi to xi+1 :
(0)
(a) Compute ηi+1 using the Adams-Bashforth formula,
(0)
(b) Compute f (xi+1 , ηi+1 ),
(k+1)
(c) Compute ηi+1 for k = 0, . . . , k0 using the Adams-Moulton formula.
Other multistep methods can easily be constructed: in equation (8.29) we replace f (x, y(x))
by the interpolation polynomial through the data points (xi−s+j , fi−s+j ), j = 0, . . . , s, and
integrate over [xi−r , xi+1 ] with 0 ≤ r ≤ s. If r = 0 we obtain the Adams-Bashforth formula.
More examples and a detailed error analysis of multistep methods is given in Stoer and
Bulirsch (1993).
For every predictor-corrector method, where the predictor is of order O(hp ) and the
corrector is of order O(hc ), the local discretization error after k + 1 iteration steps is of order
O(hmin(c,p+k+1) ). Therefore, if p = c − 1 one iteration is sufficient to obtain the order of the
corrector. For arbitrary p < c, the order O(hc ) is obtained after k + 1 = c − p iteration steps.
However, since the error constant of the predictor are larger than the error constant of the
corrector, it may happen that some more iterations are needed to guarantee the order of the
corrector. Therefore, in practical applications the order of the corrector is chosen one higher
than the order of the predictor, and one iteration step is performed.
Numerous modifications of the discussed algorithms have been developed so far. One
example is the extrapolation method of Bulirsch and Stoer, which is one of the best algorithms
at all w.r.t. accuracy and stability. It is widely used in planetology and satellite geodesy to
compute long orbits. In fact it is a predictor-corrector method with a multistep method as
predictor. By repeating the predictor for different choices of h a series of approximations is
constructed, and the final approximation is obtained by extrapolation. For more information
we refer to Engeln-Müllges and Reutter (1985); Stoer and Bulirsch (1993).
i.e. how stable the algorithm is. An algorithm is called stable if an error, which is tolerated
in one step, is not amplified when performing the next steps. It is called instable if for an
arbitrary large number of steps the difference between approximation and unknown solution
continuously increases, yielding a totally wrong solution when approaching the upper bound
of the integration interval. Responsible for instability can be the ordinary differential equation
and/or the used algorithm.
Let u be a solution of the same ordinary differential equation (ODE), i.e. u0 = f (x, u) but let
u fulfil a slightly different initial condition u(x0 ) = u0 . If u differs only slightly from y, we
may write
u(x) = y(x) + s(x), u(x0 ) = u0 = y0 + s0 ,
where s is the so-called disturbing function and a small parameter, i.e. 0 < << 1.
Therefore, u fulfils the ODE u0 (x) = y 0 (x) + s0 (x). Taylor series expansion of f (x, u) yields
and, when neglecting terms of order O(2 ), we obtain the so-called differential variational
equation
s0 = fy s.
Assuming fy = c = constant, this equation has the solution
This equation describes the disturbance at x due to a perturbed initial condition. If fy = c < 0
the disturbance descreases with increasing x, and the ODE is called stable, otherwise we call
it unstable. In case of a stable ODE, the solutions w.r.t. the perturbed initial condition
will approach the solutions w.r.t. the unperturbed initial condition. On the other hand,
if the initial value problem is unstable, then, as x increases, the solution that starts with
the perturbed value y0 + s0 will diverge away from the solution that started at y0 . If the
slight difference in initial conditions is due to rounding errors or discretization errors stability
(instability) means that this error is damped (amplified) if the integration process continues.
Example 8.13
Let us consider the ODE y 0 = f (x, y) = λ y, λ ∈ R, and the two initial conditions y(0) = 1
and y(0) = 1 + with a small parameter 0 < << 1. Obviously f is differentiable w.r.t. y
and it is fy = λ. The solution of the unperturbed initial value problem is y(x) = eλx and
8.5. Stability and convergence 139
the solution of the perturbed initial value problem is u(x) = (1 + )eλx ; the difference is
(u − y)(x) = eλx . It is clear that when λ < 0, the inital error is strongly damped, while
when λ > 0 it is amplified for increasing values x.
If the ODE is stable, a stable numerical algorithm provides a stable solution. However,
if the ODE is unstable we can never find a stable numerical algorithm. Therefore, let us
assume in the following that the ODE is stable. Then it remains to address the problem of
stability of the numerical algorithm.
We consider the disturbed initial value of the algorithm, u0 = η0 +H0 , with a small parameter
0 < << 1. The original solution is denoted by ηi , the disturbed solution by ui . Let
δi := ui − ηi . The algorithm (8.30) is called stable if
With ui = ηi + Hi , we easily can derive the difference equation for the disturbances δi ,
sometimes called difference variational equation. We obtain
s
X X s
δi+1 = as−k δi+1−k + h bs−k f (xi+1−k , ηi+1−k + δi+1−k ) − f (xi+1−k , ηi+1−k ) . (8.31)
k=1 k=0
It is very difficult to analyse this equation for general functions f . However, a consideration
of a certain simplified ODE is sufficient, to give an indication of the stability of an algorithm.
This simple ODE is derived from the general equation y 0 = f (x, y(x)) (a) by assuming that
f does not depend on x and (b) by restricting to a neighborhood of a value ȳ. Within this
neighborhood we can approximate y 0 = f (y) by its linear version y 0 = fy (ȳ) y =: c y with
c ∈ C. The corresponding initial value problem y 0 = c y, y(x̄) = ȳ has the exact solution
y(x) = ȳ · ec(x−x̄) . For our simple ODE, the difference variational equation (8.31) becomes
s
X s
X
δi+1 = as−k δi+1−k + hc bs−k δi+1−k . (8.32)
k=1 k=0
This is a homogeneous difference equation of order s with constant coefficients. Its solutions
can be looked for in the form δi = β i for all i. Substituting into (8.32) yields
s
X s
X
β i+1 = as−k β i+1−k + hc bs−k β i+1−k . (8.33)
k=1 k=0
140 Chapter 8. Numerical Methods for Solving Ordinary Differential Equations
or
s
X s
X
s s−k
P (β) := β − as−k β − hc bs−k β s−k = 0. (8.35)
k=1 k=0
with arbitrary constants cj . One of the zeros, say β1i , will tend to zero as h → 0. All
the other zeros are extraneous. If the extraneous zeros satisfy as h → 0 the condition
|βi | < 1, i = 2, . . . , s, then the algorithm is absolutely stable.
Since the zeros are continuous functions of hc, the stability condition |βj | < 1 defines a
region in the hc-plane, where the algorithm is stable. This region is called region of stability
(S), i.e.:
S = {z ∈ C : |β(z)| < 1}, z := hc ∈ C.
That means for any z = hc ∈ S the algorithm is stable.
δi+1 = (1 − 2hηi ) δi .
That means, c = −2ηi . The characteristic polynomial is β = (1 − 2hηi ) and the stability
conditions is |1 − 2hηi | < 1. That is the region of stability is S = {z ∈ C : |1 + z| < 1},
where z := −2hηi . S is a circle with center at the point -1 on the real line and radius 1.
Assuming e.g. ηi > 0, we have stability of the forward Euler-Cauchy method if h < η1i . Since
the stability analysis is based on local linearization, we have to be careful in practise and
have to choose h smaller than the criterium says to be on the safe side.
In a similar way we can determine the region of stability for other methods. We obtain
for instance:
hc
ηi+1 = ηi + h fi (1 + )
2
Figure 8.4: The stability region for (1) forward Euler-Cauchy, (2) Heun, (3) classical
Runge-Kutta, and (4) backward Euler-Cauchy. Within the curve (1), a cir-
cle centered at (−1, 0), we fulfill the condition |1 + z| < 1, which provides
stability of the forward Euler-Cauchy method. The region within curve (2),
defined by |1 + z + 21 z 2 | < 1, guarantees stability of the Heun method.
The classical Runge-Kutta method is stable within region (3), defined by
|1 + z + 12 z 2 + 16 z 3 + 24
1 4
z | < 1. The backward Euler-Cauchy method is stable
outside the circle (4), i.e. if |1 − z| > 1.
8.6. How to choose a suitable method? 143
1+z/2
Figure 8.5: Regions 1−z/2 = b for various values b. The stability region of the trapezoidal
rule is obtained if b = 1. Obviously the trapezoidal rule is stable for Re(z) < 0,
i.e. for the complete left half-plane.
144 Chapter 8. Numerical Methods for Solving Ordinary Differential Equations
moderate accuracies < 10−4 the classical Runge-Kutta formula is a good choice. For lower
accuracies > 10−4 single-step methods with local order < 4 can be used. If the computation
of f is costly, multistep methods are superior to single-step methods, although they are
more costly when the step size has to be changed during the calculations. Very efficient are
implicit Adams methods of variable order. Implicit Runge-Kutta methods, which have not
been discussed, are suitable if very high accuracies (10−10 . . . 10−20 ) are needed.
Independent on the initial value problem we can summarize the following:
(a) Runge-Kutta methods have the advantage that they have not to be initialized, they
have a high local order, they are easy to handle, and an automatic step size control is
easy to implement. A drawback is that each step requires several evaluations of the
function f .
(b) Multistep methods have the advantage that in general only 2-3 function evaluations per
step are needed (including iterations). Moreover, formulas of arbitrarily high order can
easily be constructed. A drawback is that they have to be initialized, especially if the
step size has to be changed since after any change of the step size, a new initialization
is necessary.
Chapter 9
Numerical Optimization
Equivalently: Find
f¯ := min f (x).
x∈Ω
In 1-dimension Ω = [a, b] is typically an interval of the real line. As for all other numerical
methods in these notes, we are mainly interested in the richer and more challenging multi-
dimensional case.
More generally we can consider optimization problems where the design space is partly
or wholly discrete, e.g. find Ω = RN × N. Further we may have problems with an additional
145
146 Chapter 9. Numerical Optimization
where g(·) = 0 is called an equality constraint and h(·) ≥ 0 an inequality constraint. The
problem is now known as a constrained optimization problem, an important example of which
is PDE-constrained optimization, where Ω is a function space and g(x) = 0 is a partial differ-
ential equation. Of course in all cases the minimization can be replaced with a maximization.
But the new shape might result in an undesirable reduction in lift cL compared to the initial
design, in which case we should introduce a constraint that guarantees the lift is not reduced
cL (x) − cL (x(0) ) ≥ 0,
and we now have a constrained optimization problem. Furthermore imagine that we are at
a very early stage in the design of the aircraft, and have a choice a regarding the number of
engines we should install on the wing. The number of engines is a new discrete design vari-
able, Neng ∈ {1, 2, 4}, and the optimization problem becomes continuous-discrete inequality
constrained.
In any case, to solve the problem we need to evaluate cD (·) and cL (·) for different x
representing different wing geometries. This is the job of Computational Fluid Dynamics
(CFD), and may be a computationally expensive operation. We therefore need efficient
numerical methods that find x̄ with as few evaluations of cD (·) as possible.
The numerical methods required for solving each type of optimization problem given above
are quite different. In the following we consider methods for the most common unconstrained
continuous optimization problem only.
9.2. Global and local minima 147
min sin x
x∈[0,2πM ]
it’s easy to convince ourselves respectively that optimization problems may have any number
of solutions, and even infinitely many solutions on a finite interval - so solutions can definitely
not be regarded as unique in general. As for existence, consider the problem
min x,
x∈(0,1)
where (0, 1) indicates the open interval (not including the end points). This problem has no
solution by the following proof by contradiction: assume there exists a solution x̄ = ∈ (0, 1);
now consider /2 which is certainly in (0, 1) and /2 < , so is not the solution. Contra-
diction. QED.1 So we can not establish existence of solutions in general either. However in
engineering practice (e.g. Example 9.1) we rarely deal with open intervals and therefore don’t
encounter issues of existence. And if there are multiple solutions any may be a satisfactory
design, or we may choose one based on other considerations.
In fact we often reduce the scope of the problem by accepting local solutions. In particular
a global optimum is a solution of (9.1), while a local optimum is a solution of the related
problem: Find x ∈ E() ⊂ Ω such that
i.e. the ball of radius surrounding the local solution x̃. In other words a local minima
represents the optimal choice in a local sense – the value of the objective function can not
be reduced without performing a step greater than some small but finite > 0. Note that a
global optimum is necessarily also a local optimum. Needless to say, we may have multiple
local and global optima in a single problem.
The reason we often compromise and only ask for local optima, is that it is easy to find
a local optima, but very difficult to determine if a given optima is also global — one has to
1
This is the reason we need the concept of the infimum in real analysis.
148 Chapter 9. Numerical Optimization
Figure 9.1: One-d objective function f (x) containing multiple local and global optima.
Extrema are marked with circles.
search all Ω and make sure there are no other (potentially very limited) regions where f is
smaller. With the algorithms presented here we can find local optima quickly and cheaply
close to a starting point x(0) . This is often sufficient for engineering purposes, where an
acceptable design is known a priori — e.g. we know roughly what a wing looks like and want
to find another very similar shape with lower drag.
Several algorithms for finding optima will be discussed below. They all find optima close
to the starting point x(0) , and are therefore all local. Optimization algorithms are divided into
gradient-based methods that use f 0 , and gradient-free methods that require only the ability
to evaluate f . In general the former are effective for any N , the latter only for N ∼ O(10).
certainly a minimum in the interval [xL , b] (that is not at xL . See Figure 9.2 for a graphical
interpretation.
This suggests a recursive algorithm where we progressively reduce the size of the interval
on which we know an minimum to exist.
The question remains: how should we choose xL and xR , or α and β in the above algo-
rithm? We specify a desirable property: that we can reuse one of f (xL ) or f (xR ) from the
previous step — that is, if we choose the sub-interval [an , xR,n ] on step n, then xL,n should
be in the position of xR,n+1 on step n + 1. Similarly if we choose the sub-interval [xL,n , bn ]
then xR,n should be in the position of xL,n+1 on step n + 1. Thus we reduce the number of
evaluations of f (·) to 1 per step, rather than 2 per step.
Satisfying this conditions is possible using the Golden section (or Gulden snede in Dutch).
The conditions require that
xR − xL xL − a
= ,
xL − a b − xL
and
xR − xL xL − a
= .
b − (xR − xL ) b − xL
Eliminating xR − xL from these equations we get
ϕ2 = ϕ + 1 (9.6)
150 Chapter 9. Numerical Optimization
where
b − xL
ϕ= . (9.7)
xL − a
The quadratic equation (9.6) has one positive and one negative root, the positive one is
√
1+ 5
ϕ= = 1.618033988...
2
the Golden ratio. Solving (9.7) for xL gives
xL = a + (1 − ϕ−1 )(b − a)
given which
xR = a + ϕ−1 (b − a)
and therefore
α = 1 − ϕ−1 ≈ 0.3820, β = ϕ−1 ≈ 0.6180.
With these values of α and β the above algorithm is known as the golden-section search
method. The interval width decreases by a constant factor ϕ−1 on each iteration, no matter
which side of the interval is choosen. Therefore if we take the midpoint of the interval as our
approxiation of the minimum, we make an error of at most
b−a
0 =
2
on the first step, and
b−a
n = (ϕ−1 )n
2
on the nth step. As for recursive bisection we have therefore linear convergence, in this case
at a rate of ϕ−1 . Applying the method to various hand-sketched functions is a good way of
getting an intuition for the behaviour of the method.
is available, and further that Ω = RN . In this case we can rewrite (9.1) as: Find one x̄ ∈ Ω
such that
f 0 (x̄) = 0, (9.8)
152 Chapter 9. Numerical Optimization
The Hessian is symmetric so all eigenvalues λ are real. If all eigenvalues are positive we
have a local minimum, all negative a local maximum, and mixed implies a saddle point. See
Example 9.3.2
In order to solve (9.8) we can use any of the methods of Chapter 2, for example Newton’s
method: h i−1
x(n+1) = x(n) − f 00 (x(n) ) f 0 (x(n) ), (9.9)
with a suitable choice of starting point. The properties of Newton’s method for root-finding
are transfered to this algorithm. In particular we know that Newton’s method converges
quadratically, so we expect only a few iterations are required for an accurate solution. Fur-
thermore Newton tends to converge to a solution close to the initial guess x(0) , so this
algorithm will have the property of finding a local optimum, and different starting points
may give different optima.
1
f (x) = c + bT x + xT Ax, (9.10)
2
known as a quadratic form, where A = AT is a symmetric matrix, b a vector and c a constant.
This is a useful objective function to consider as it is the first 3 terms of a Taylor expansion
of a general function f :
1 T 00
f (x0 + h) = f (x0 ) + f 0 (x0 )T h + h f (x0 )h + O(khk3 ),
2!
where we can immediately identify
c = f (x0 )
b = f 0 (x0 )
A = f 00 (x0 ).
2
This is a generalization of the 1d principle that if the 1st-derivative of f is zero and the 2nd-derivative is
positive we have a local minimum (consider what this means for the Taylor expansion of f about x̄).
9.4. Newton’s method 153
Therefore for small khk, f is approximated well by a quadratic form. In particular if f has
a stationary point at x̄ then f 0 = 0 and
1
f (x̄ + h) ≈ f (x̄) + hT f 00 (x̄)h. (9.11)
2
Now we would like to understand under what conditions on A = f 00 (x̄) the function f
has a minimum at x̄ (rather than a maximum or something else). Since A is symmetric
(i) all eigenvalues are real, and (ii) we can find an orthonormal basis of eigenvectors vi for
RN −1 , satisfying Avi = λi vi and viT vj = δij . Now consider (9.11), and write h as a sum of
eigenvectors of A:
NX −1
h= ai vi ,
i=0
substituting into the second term in (9.11) we have
N −1 N −1 N −1 N −1 N −1
1 T 1X X 1X X 1X
h Ah = ai vi Avj aj = ai vi λj vj aj = λj a2j .
2 2 2 2
i=0 j=0 i=0 j=0 j=0
Now if λj > 0, ∀j, then this term will be positive no matter what direction h we choose, and
therefore the function increases in every direction, and x̄ is a local minimum. Similarly if
λj < 0, ∀j then this term will be negative for all directions h, and we have a local maximum.
If λj are mixed positive and negative, then in some directions f increases, and in others
it decreases — these are known as saddle-points.3 A quadratic form with λj > 0 in 2d is
sketched in Figure 9.3.
Example 9.4 (Newton’s method applied to a quadratic form)
To apply Newton to the quadratic form
1
f (x) = c + bT x + xT Ax,
2
where we assume A is positive definite, we first differentiate once:
1 T
f 0 (x) = b +
x A + Ax = b + Ax
2
and then again:
f 00 (x) = A,
and apply the Newton iteration (9.9)
But then x(1) satisfies immediately f 0 (x(1) ) = 0. So if f is a quadratic form Newton converges
to the exact optimum in 1 iteration from any starting point x(0) ! Compare this result to the
result from root-finding that if f is linear Newton finds the root f (x̄) = 0 in one iteration.
3
A symmetric matrix A is called strictly positive definite if λj > 0, ∀j, which is equivalent to xT Ax >
0, ∀x ∈ RN −1 . Therefore we are mainly interested in quadratic forms with strictly positive definite A.
154 Chapter 9. Numerical Optimization
and the algorithm repeats with a new search direction. See Figure 9.4 for a sketch of the
progress of this method in 2d. At each step we are guaranteed to reduce the value of the
objective function:
f (x(n+1) ) ≤ f (x(n) ),
and if x(n) = x̄ then x(n+1) = x̄ and the exact solution is preserved, but it unclear how fast
this method converges. For this analysis we return to the example of a quadratic form, which
will describe the behaviour of the algorithm close to the optimum of any function f .
9.5. Steepest descent method 155
rn = −f 0 (x(n) ) = −b − Ax(n) .
Now the function f (·) will have a minimum in this direction when the gradient of f (·) is
orthogonal to the search direction, i.e. when
f 0 (x(n+1) )T · f 0 (x(n) ) = 0.
Starting from this point we derive an expression for the step-size α(n) :
T
rn+1 · rn = 0
(−b − Ax(n+1) )T · rn = 0 Defn. of rn+1
(n) (n) T
(−b − A(x +α rn )) · rn = 0 Defn. of x(n+1)
(−b − Ax(n) )T · rn − α(n) (Arn )T · rn = 0 Linearity of A
rnT · rn − α(n) rnT Arn = 0 Symmetry of A
156 Chapter 9. Numerical Optimization
so that
rnT rn
α(n) = . (9.12)
rnT Arn
So for a step of steepest descent for a quadratic form we know exactly what distance to step in
the direction rn . We could also take this step if we know the Taylor expansion of f including
the quadratic term; though in general this will be a different step than that performed using
a numerical 1d search method (because of the additional higher-order terms neglected).
with exact solution x̄ = 0, f¯ = 0.4 Apply steepest decent with an initial guess x(0) =
(−1, 0.001). The derivative is
! !
0 1 0 x0
f (x) = x= .
0 1000 1000x1
and by (9.12):
r0T r0 2
α(0) = T
= .
r0 Ar0 1001
Note that this is a tiny step in comparision to the distance to the true minimum at 0, which
is ≈ 1. We have
1 T
x(1) = 999
−999 − 1000
1001
which is extremely close to the starting point. The next search direction is
999
r1 = −f 0 (x(1) ) = (1, 1),
1001
which is just 90◦ to the previous direction but slightly shorter. The next step is
r1T r1 2
α(1) = T
= ,
r1 Ar1 1001
i.e. the same as before, but since r1 is slightly shorter the actual step is slightly shorter.
The iteration proceeds in this way, with the search direction alternating between (1, 1) and
9.6. Nelder-Mead simplex method 157
Figure 9.5: Slow progress of the steepest descent method for the stiff problem of Exam-
ple 9.6.
(1, −1), α = 2/1001, and the total step reducing a factor of 999/1001 at each step. See
Figure 9.5
Therefore the error approximate minimum at step n can be approximately written:
999 n
n ≈ .
1001
This is familiar linear convergence, but extremely slow. After 100 steps the error is 100 ≈
0.82, after 1000 steps still 1000 ≈ 0.14. To solve this issue with stiffness causing slow conver-
gence there is a related algorithm called the conjugate gradient method, which make a cleverer
choice of search direction, but this is outside the scope of this course.
i.e. x1 corresponds to the best value of the objective function, and x3 the worst.
6. REDUCE: Nothing is producing an improvement, so replace all the nodes but the best
node with the midpoints of the triangle edges, to create a triangle in the same location
but half the size:
• x2 = x1 + 12 (x2 − x1 )
• x3 = x1 + 12 (x3 − x1 ) and goto 1.
The basic idea is that the values of f at the 3 nodes of the triangle give a clue about the
best direction in which to search for a new point without needing any gradient information. At
each step the algorithm tries to move away from the worst point in the triangle (REFLECT).
If this strategy is successful then it goes even further and stretches the triangle in that
direction (EXPAND); if not successful it is more conservative (CONTRACT); and if none of
this seems to work then it makes the entire triangle smaller (REDUCE), before trying again.
See Figure 9.6
9.6. Nelder-Mead simplex method 159
After a number of iterations the stretching of the simplex corresponds roughly to the
stretching of the objective function in the design space. If the EXPAND operation is per-
formed repeatedly the result will be an increasingly stretched triangle, but this will only
occur if this strategy is producing a consistent reduction in that direction. This flexibility
allows the method to take large steps when necessary, and thereby gain some of the efficiency
advantages of gradient-based methods.
160 Chapter 9. Numerical Optimization
Stoer, J. and Bulirsch, R. (1993). Introduction to numerical Analysis. Springer, New York,
2nd edition.
Vetterling, W. T., Teukolsky, S. A., and Press, W. H. (1992). Numerical Recipes in FOR-
TRAN; the Art of scientific Computing; Numerical Recipes Example Book. Cambridge
University Press, New York.
161