Академический Документы
Профессиональный Документы
Культура Документы
7 Exercises 37
Contents
1 Overview of truncation error analysis 4
1.1 Abstract problem setting . . . . . . . . . . . . . . . . . . . . . . 4
1.2 Error measures . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2
List of Exercises Purpose.
Exercise 1 Truncation error of a weighted mean p. 37 Truncation error analysis provides a widely applicable framework for ana-
Exercise 2 Simulate the error of a weighted mean p. 37 lyzing the accuracy of finite difference schemes. This type of analysis can
Exercise 3 Verify a truncation error formula p. 37 also be used for finite element and finite volume methods if the discrete
Exercise 4 Truncation error of the Backward Euler scheme p. 37 equations are written in finite difference form. The result of the analysis
Exercise 5 Empirical estimation of truncation errors p. 38 is an asymptotic estimate of the error in the scheme on the form Chr ,
Exercise 6 Correction term for a Backward Euler scheme p. 38 where h is a discretization parameter (t, x, etc.), r is a number, known
Exercise 7 Verify the effect of correction terms p. 38 as the convergence rate, and C is a constant, typically dependent on the
Exercise 8 Truncation error of the Crank-Nicolson scheme p. 38 derivatives of the exact solution.
Exercise 9 Truncation error of u0 = f (u, t) p. 39 Knowing r gives understanding of the accuracy of the scheme. But maybe
Exercise 10 Truncation error of [Dt Dt u]n p. 39 even more important, a powerful verification method for computer codes
Exercise 11 Investigate the impact of approximating u0 (0) ... p. 39 is to check that the empirically observed convergence rates in experiments
Exercise 12 Investigate the accuracy of a simplified scheme ... p. 40 coincide with the theoretical value of r found from truncation error analysis.
The analysis can be carried out by hand, by symbolic software, and also
numerically. All three methods will be illustrated. From examining the
symbolic expressions of the truncation error we can add correction terms to
the differential equations in order to increase the numerical accuracy.
In general, the term truncation error refers to the discrepancy that arises
from performing a finite number of steps to approximate a process with infinitely
many steps. The term is used in a number of contexts, including truncation
of infinite series, finite precision arithmetic, finite differences, and differential
equations. We shall be concerned with computing truncation errors arising in
finite difference formulas and in finite difference discretizations of differential
equations.
L(u) = 0,
where L(u) is some formula involving the unknown u and its derivatives. One
example is L(u) = u0 (t) + a(t)u(t) b(t), where a and b are contants or functions
of time. We can discretize the differential equation and obtain a corresponding
discrete model, here written as
L (u) = 0 .
3 4
numerical solution, is that the truncation error analysis reveals the accuracy
of the various building blocks in the numerical method and how each building
L(ue ) = 0, block impacts the overall accuracy. The analysis can therefore be used to detect
L (u) = 0 . building blocks with lower accuracy than the others.
Knowing the truncation error or other error measures is important for verifica-
Initial and/or boundary conditions can usually be left out of the truncation error tion of programs by empirically establishing convergence rates. The forthcoming
analysis and are omitted in the following. text will provide many examples on how to compute truncation errors for finite
The numerical solution u is in a finite difference method computed at a collec- difference discretizations of ODEs and PDEs.
tion of mesh points. The discrete equations represented by the abstract equation
L (u) = 0 are usually algebraic equations involving u at some neighboring mesh
points. 2 Truncation errors in finite difference formulas
1.2 Error measures The accuracy of a finite difference formula is a fundamental issue when discretizing
differential equations. We shall first go through a particular example in detail
A key issue is how accurate the numerical solution is. The ultimate way of and thereafter list the truncation error in the most common finite difference
addressing this issue would be to compute the error ue u at the mesh points. approximation formulas.
This is usually extremely demanding. In very simplified problem settings we
may, however, manage to derive formulas for the numerical solution u, and
therefore closed form expressions for the error ue u. Such special cases can 2.1 Example: The backward difference for u0 (t)
provide considerable insight regarding accuracy and stability, but the results are
Consider a backward finite difference approximation of the first-order derivative
established for special problems.
u0 :
The error ue u can be computed empirically in special cases where we know
ue . Such cases can be constructed by the method of manufactured solutions, un un1
where we choose some exact solution ue = v and fit a source term f in the [Dt u]n = u0 (tn ) . (1)
t
governing differential equation L(ue ) = f such that ue = v is a solution (i.e., f =
L(v)). Assuming an error model of the form Chr , where h is the discretization Here, un means the value of some function u(t) at a point tn , and [Dt u]n is
parameter, such as t or x, one can estimate the convergence rate r. This is a the discrete derivative of u(t) at t = tn . The discrete derivative computed by a
widely applicable procedure, but the valididity of the results is, strictly speaking, finite difference is not exactly equal to the derivative u0 (tn ). The error in the
tied to the chosen test problems. approximation is
Another error measure is to ask to what extent the exact solution ue fits the
discrete equations. Clearly, ue is in general not a solution of L (u) = 0, but we Rn = [Dt u]n u0 (tn ) . (2)
can define the residual
The common way of calculating Rn is to
R = L (ue ),
1. expand u(t) in a Taylor series around the point where the derivative is
and investigate how close R is to zero. A small R means intuitively that the evaluated, here tn ,
discrete equations are close to the differential equation, and then we are tempted
to think that un must also be close to ue (tn ). 2. insert this Taylor series in (2), and
The residual R is known as the truncation error of the finite difference scheme
L (u) = 0. It appears that the truncation error is relatively straightforward 3. collect terms that cancel and simplify the expression.
to compute by hand or symbolic software without specializing the differential
equation and the discrete model to a special case. The resulting R is found The result is an expression for Rn in terms of a power series in t. The error Rn
as a power series in the discretization parameters. The leading-order terms is commonly referred to as the truncation error of the finite difference formula.
in the series provide an asymptotic measure of the accuracy of the numerical The Taylor series formula often found in calculus books takes the form
solution method (as the discretization parameters tend to zero). An advantage
X 1 di f
of truncation error analysis compared empricial estimation of convergence rates f (x + h) = (x)hi .
i! dx i
or detailed analysis of a special problem with a mathematical expression for the i=0
5 6
In our application, we expand the Taylor series around the point where the finite 2.3 Example: The central difference for u0 (t)
difference formula approximates the derivative. The Taylor series of un at tn
is simply u(tn ), while the Taylor sereis of un1 at tn must employ the general For the central difference approximation,
formula,
1 1
un+ 2 un 2
X 1 di u u0 (tn ) [Dt u]n , [Dt u]n = ,
u(tn1 ) = u(t t) = (tn )(t)i t
i=0
i! dti
we write
1
= u(tn ) u0 (tn )t + u00 (tn )t2 + O(t3 ),
2
Rn = [Dt u]n u0 (tn ),
where O(t3 ) means a power-series in t where the lowest power is t3 . We
assume that t is small such that tp tq if p is smaller than q. The details and expand u(tn+ 12 ) and u(tn1/2 ) in Taylor series around the point tn where
of higher-order terms in t are therefore not of much interest. Inserting the the derivative is evaluated. We have
Taylor series above in the left-hand side of1 (2) gives rise to some algebra:
1 1 1
u(tn+ 12 ) =u(tn ) + u0 (tn ) t + u00 (tn )( t)2 +
u(tn ) u(tn1 ) 2 2 2
[Dt u]n u0 (tn ) = u0 (tn ) 1 000 1 1 0000 1
t u (tn )( t) + u (tn )( t)4 +
3
6 2 24 2
u(tn ) (u(tn ) u0 (tn )t + 21 u00 (tn )t2 + O(t3 )) 1 0000 1
= u0 (tn ) 5
u (tn )( t) + O(t ), 6
t 120 2
1 1 1 1
= u00 (tn )t + O(t2 )), u(tn1/2 ) =u(tn ) u0 (tn ) t + u00 (tn )( t)2
2 2 2 2
which is, according to (2), the truncation error: 1 000 1 1 0000 1
u (tn )( t) + u (tn )( t)4
3
6 2 24 2
1 1 00000 1
Rn = u00 (tn )t + O(t2 )) . (3) 5
u (tn )( t) + O(t ) . 6
2 120 2
The dominating term for small t is 21 u00 (tn )t, which is proportional to t,
Now,
and we say that the truncation error is of first order in t.
1 000 1 00000
2.2 Example: The forward difference for u0 (t) u(tn+ 12 ) u(tn1/2 ) = u0 (tn )t + u (tn )t3 + u (tn )t5 + O(t7 ) .
24 960
We can analyze the approximation error in the forward difference
By collecting terms in [Dt u]n u(tn ) we find the truncation error to be
n+1 n
u u
u0 (tn ) [Dt+ u]n = , 1 000
t Rn = u (tn )t2 + O(t4 ), (4)
by writing 24
Rn = [Dt+ u]n u0 (tn ),
with only even powers of t. Since R t2 we say the centered difference is of
n+1
and expanding u in a Taylor series around tn , second order in t.
1
u(tn+1 ) = u(tn ) + u0 (tn )t + u00 (tn )t2 + O(t3 ) .
2 2.4 Overview of leading-order error terms in finite differ-
The result becomes ence formulas
1 00
R= u (tn )t + O(t2 ),
2 Here we list the leading-order terms of the truncation errors associated with
showing that also the forward difference is of first order. several common finite difference formulas for the first and second derivatives.
7 8
The geometric mean also has an error O(t2 ):
n+ 12 n 12
u u
[Dt u]n = = u0 (tn ) + Rn , (5) t,g n 1 1
t [u2 ] = un 2 un+ 2 = (un )2 + Rn , (23)
1 000
Rn = u (tn )t2 + O(t4 ) (6) 1 1
24 Rn = u0 (tn )2 t2 + u(tn )u00 (tn )t2 + O(t4 ) . (24)
4 4
un+1 un1
[D2t u] n
= = u0 (tn ) + Rn , (7) The harmonic mean is also second-order accurate:
2t
1
R n
= u000 (tn )t2 + O(t4 ) (8)
6 2 1
un un1 [ut,h ]n = un = 1 1 + Rn+ 2 , (25)
[Dt u]n = = u0 (tn ) + Rn , (9) 1 + 1
t un 2 un+ 2
1 u0 (tn )2 2 1 00
Rn = u00 (tn )t + O(t2 ) (10) Rn = t + u (tn )t2 . (26)
2 4u(tn ) 8
un+1 un
[Dt+ u]n = = u0 (tn ) + Rn , (11)
t 2.5 Software for computing truncation errors
1
R n
= u00 (tn )t + O(t2 ) (12) We can use sympy to aid calculations with Taylor series. The derivatives can
2
un+1 un be defined as symbols, say D3f for the 3rd derivative of some function f . A
[Dt u]n+ = = u0 (tn+ ) + Rn+ , (13) truncated Taylor series can then be written as f + D1f*h + D2f*h**2/2. The
t
1 1 following class takes some symbol f for the function in question and makes a list
Rn+ = (1 2)u00 (tn+ )t ((1 )3 3 )u000 (tn+ )t2 + O(t3 ) of symbols for the derivatives. The __call__ method computes the symbolic
2 6
(14) form of the series truncated at num_terms terms.
3un 4un1 + un2 import sympy as sp
[Dt2 u]n = = u0 (tn ) + Rn , (15)
2t
1 class TaylorSeries:
Rn = u000 (tn )t2 + O(t3 ) (16) """Class for symbolic Taylor series."""
3 def __init__(self, f, num_terms=4):
n+1
u 2un + un1 self.f = f
[Dt Dt u]n = = u00 (tn ) + Rn , (17) self.N = num_terms
t2 # Introduce symbols for the derivatives
1 0000 self.df = [f]
Rn = u (tn )t2 + O(t4 ) (18) for i in range(1, self.N+1):
12
self.df.append(sp.Symbol(D%d%s % (i, f.name)))
It will also be convenient to have the truncation errors for various means or
def __call__(self, h):
averages. The weighted arithmetic mean leads to """Return the truncated Taylor series at x+h."""
terms = self.f
[ut, ]n+ = un+1 + (1 )un = u(tn+ ) + Rn+ , (19) for i in range(1, self.N+1):
1 terms += sp.Rational(1, sp.factorial(i))*self.df[i]*h**i
Rn+ = u00 (tn+ )t2 (1 ) + O(t3 ) . (20) return terms
2
The standard arithmetic mean follows from this formula when = 1/2. Expressed We may, for example, use this class to compute the truncation error of the
at point tn we get Forward Euler finite difference formula:
9 10
>>> FE = (u_Taylor(dt) - u)/dt which inserted in (29) results in
>>> FE
(D1u*dt + D2u*dt**2/2 + D3u*dt**3/6 + D4u*dt**4/24)/dt 1
>>> simplify(FE) u0e (tn ) + u00e (tn )t + O(t2 ) + aue (tn ) = Rn .
D1u + D2u*dt/2 + D3u*dt**2/6 + D4u*dt**3/24 2
Now, u0e (tn ) + aune = 0 since ue solves the differential equation. The remaining
The truncation error consists of the terms after the first one (u0 ).
terms constitute the residual:
The module file trunc/truncation_errors.py1 contains another class DiffOp
with symbolic expressions for most of the truncation errors listed in the previous 1 00
Rn = u (tn )t + O(t2 ) . (30)
section. For example: 2 e
>>> from truncation_errors import DiffOp This is the truncation error Rn of the Forward Euler scheme.
>>> from sympy import *
>>> u = Symbol(u) Because Rn is proportional to t, we say that the Forward Euler scheme
>>> diffop = DiffOp(u, independent_variable=t) is of first order in t. However, the truncation error is just one error measure,
>>> diffop[geometric_mean] and it is not equal to the true error une un . For this simple model problem
-D1u**2*dt**2/4 - D1u*D3u*dt**4/48 + D2u**2*dt**4/64 + ...
>>> diffop[Dtm] we can compute a range of different error measures for the Forward Euler
D1u + D2u*dt/2 + D3u*dt**2/6 + D4u*dt**3/24 scheme, including the true error une un , and all of them have dominating terms
>>> >>> diffop.operator_names()
[geometric_mean, harmonic_mean, Dtm, D2t, DtDt, proportional to t.
weighted_arithmetic_mean, Dtp, Dt]
The indexing of diffop applies names that correspond to the operators: Dtp for 3.2 Truncation error of the Crank-Nicolson scheme
Dt+ , Dtm for Dt , Dt for Dt , D2t for D2t , DtDt for Dt Dt . For the Crank-Nicolson scheme,
1
[Dt u = au]n+ 2 , (31)
3 Truncation errors in exponential decay ODE
we compute the truncation error by inserting the exact solution of the ODE and
We shall now compute the truncation error of a finite difference scheme for a adding a residual R,
differential equation. Our first problem involves the following the linear ODE
modeling exponential decay, 1
[Dt ue + aue t = R]n+ 2 . (32)
n+ 12 1
u0 (t) = au(t) . (27) The term [Dt ue ] is easily computed from (5)-(6) by replacing n with n + 2
in the formula,
3.1 Truncation error of the Forward Euler scheme 1 1 000
[Dt ue ]n+ 2 = u0 (tn+ 21 ) +
u (t 1 )t2 + O(t4 ) .
We begin with the Forward Euler scheme for discretizing (27): 24 e n+ 2
The arithmetic mean is related to u(tn+ 21 ) by (21)-(22) so
[Dt+ u = au]n . (28)
1 1
The idea behind the truncation error computation is to insert the exact solution [aue t ]n+ 2 = u(tn+ 12 ) + u00 (tn )t2 + +O(t4 ) .
8
ue of the differential equation problem (27) in the discrete equations (28) and n+ 1
find the residual that arises because ue does not solve the discrete equations. Inserting these expressions in (32) and observing that u0e (tn+ 12 ) + aue 2 = 0,
Instead, ue solves the discrete equations with a residual Rn : because ue (t) solves the ODE u0 (t) = au(t) at any point t, we find that
[Dt+ ue + aue = R]n . (29) 1 1 000 1
Rn+ 2 = ue (tn+ 12 ) + u00 (tn ) t2 + O(t4 ) (33)
24 8
From (11)-(12) it follows that
Here, the truncation error is of second order because the leading term in R is
1 proportional to t2 .
[Dt+ ue ]n = ue (tn ) + u00e (tn )t + O(t2 ),
0
2 At this point it is wise to redo some of the computations above to establish
1 http://tinyurl.com/jvzzcfn/trunc/truncation errors.py the truncation error of the Backward Euler scheme, see Exercise 4.
11 12
3.3 Truncation error of the -rule theta: -D2u*a*dt**2*theta**2/2 + D2u*a*dt**2*theta/2 -
D2u*dt*theta + D2u*dt/2 + D3u*a*dt**3*theta**3/3 -
We may also compute the truncation error of the -rule, D3u*a*dt**3*theta**2/2 + D3u*a*dt**3*theta/6 +
D3u*dt**2*theta**2/2 - D3u*dt**2*theta/2 + D3u*dt**2/6,
}
[Dt u = aut, ]n+ .
The results are in correspondence with our hand-derived expressions.
Our computational task is to find Rn+ in
13 14
stride = N[i]/N_0 stride = N[i]/N_0
R[i] = R[i][::stride] R[i] = R[i][::stride] # restrict to coarsest mesh
R_a[i] = R_a[i][::stride]
The quantity R[i][n] now corresponds to Rin . if makeplot:
In addition to estimating r for the pointwise values of R = Ctr , we may plt.figure(1)
also consider an integrated quantity on mesh i, plt.plot(t_coarse, R[i], log=y)
legends_R.append(N=%d % N[i])
Ni
! 12 Z plt.hold(on)
X T
RI,i = ti (Rin )2 Ri (t)dt . (38) plt.figure(2)
n=0 0 plt.plot(t_coarse, R_a[i] - R[i], log=y)
plt.hold(on)
The sequence RI,i , i = 0, . . . , m 1, is also expected to behave as Ctr , with legends_R_a.append(N=%d % N[i])
the same r as for the pointwise quantity R, as t 0. if makeplot:
The function below computes the Ri and RI,i quantities, plots them and plt.figure(1)
compares with the theoretically derived truncation error (R_a) if available. plt.xlabel(time)
plt.ylabel(pointwise truncation error)
plt.legend(legends_R)
import numpy as np plt.savefig(R_series.png)
import scitools.std as plt plt.savefig(R_series.pdf)
plt.figure(2)
def estimate(truncation_error, T, N_0, m, makeplot=True): plt.xlabel(time)
""" plt.ylabel(pointwise error in estimated truncation error)
Compute the truncation error in a problem with one independent plt.legend(legends_R_a)
variable, using m meshes, and estimate the convergence plt.savefig(R_error.png)
rate of the truncation error. plt.savefig(R_error.pdf)
The user-supplied function truncation_error(dt, N) computes # Convergence rates
the truncation error on a uniform mesh with N intervals of r_R_I = convergence_rates(dt, R_I)
length dt:: print R integrated in time; r:,
print .join([%.1f % r for r in r_R_I])
R, t, R_a = truncation_error(dt, N) R = np.array(R) # two-dim. numpy array
r_R = [convergence_rates(dt, R[:,n])[-1]
where R holds the truncation error at points in the array t, for n in range(len(t_coarse))]
and R_a are the corresponding theoretical truncation error
values (None if not available).
The first makeplot block demonstrates how to build up two figures in parallel,
The truncation_error function is run on a series of meshes using plt.figure(i) to create and switch to figure number i. Figure numbers
with 2**i*N_0 intervals, i=0,1,...,m-1.
The values of R and R_a are restricted to the coarsest mesh. start at 1. A logarithmic scale is used on the y axis since we expect that R
and based on these data, the convergence rate of R (pointwise) as a function of time (or mesh points) is exponential. The reason is that the
and time-integrated R can be estimated empirically. theoretical estimate (30) contains u00e , which for the present model goes like eat .
"""
N = [2**i*N_0 for i in range(m)] Taking the logarithm makes a straight line.
The code follows closely the previously stated mathematical formulas, but the
R_I = np.zeros(m) # time-integrated R values on various meshes statements for computing the convergence rates might deserve an explanation.
R = [None]*m # time series of R restricted to coarsest mesh
R_a = [None]*m # time series of R_a restricted to coarsest mesh The generic help function convergence_rate(h, E) computes and returns ri1 ,
dt = np.zeros(m) i = 1, . . . , m 1 from (37), given ti in h and Rin in E:
legends_R = []; legends_R_a = [] # all legends of curves
def convergence_rates(h, E):
for i in range(m): from math import log
dt[i] = T/float(N[i]) r = [log(E[i]/E[i-1])/log(h[i]/h[i-1])
R[i], t, R_a[i] = truncation_error(dt[i], N[i]) for i in range(1, len(h))]
return r
R_I[i] = np.sqrt(dt[i]*np.sum(R[i]**2))
15 16
R = np.array(R) # two-dim. numpy array
r_R = [convergence_rates(dt, R[:,n])[-1]
for n in range(len(t_coarse))]
compute the final rate rm2 for Rn tr at each mesh point tn in the coarsest
mesh. This latter computation deserves more explanation. Since R[i][n] holds
the estimated truncation error Rin on mesh i, at point tn in the coarsest mesh,
R[:,n] picks out the sequence Rin for i = 0, . . . , m 1. The convergence_rate
function computes the rates at tn , and by indexing [-1] on the returned array
from convergence_rate, we pick the rate rm2 , which we believe is the best
estimation since it is based on the two finest meshes.
The estimate function is available in a module trunc_empir.py2 . Let us
apply this function to estimate the truncation error of the Forward Euler scheme.
We need a function decay_FE(dt, N) that can compute (35) at the points in a
mesh with time step dt and N intervals:
import numpy as np
import trunc_empir
17 18
1
un+1 = Aun , A = 1 at = 1 p + p2 , p = at,
2
The amplification factor A as a function of p = at is seen to be the first three
terms of the Taylor series for the exact amplification factor ep . The Forward
Euler scheme for u = au gives only the first two terms 1 p of the Taylor series
for ep . That is, using a increases the order of the accuracy in the amplification
factor.
Instead of replacing u00 by a2 u, we use the relation u00 = au0 and add a
term 12 atu0 in the ODE:
1 1
u0 = au atu0 1 + at u0 = au .
2 2
Using a Forward Euler method results in
n+1
1 u un
1 + at = aun ,
2 t
which after some algebra can be written as
1 12 at n
Figure 2: Difference between theoretical and estimated truncation error at mesh un+1 = u .
points for different meshes. 1 + 12 at
This is the same formula as the one arising from a Crank-Nicolson scheme applied
to u0 = au! It now recommended to do Exercise 6 and repeat the above steps
u0 = au u00 = au0 = a(au) = a2 u . to see what kind of correction term is needed in the Backward Euler scheme to
make it second order.
This means that we can simply set C n = 12 a2 tun . We can then either solve The Crank-Nicolson scheme is a bit more challenging to analyze, but the
the discrete equation ideas and techniques are the same. The discrete equation reads
1
[Dt+ u = au + a2 tu]n , (40) 1
[Dt u = au]n+ 2 ,
2
or we can equivalently discretize the perturbed ODE and the truncation error is defined through
1 1
u0 = au, a = a(1 at), (41) [Dt ue + aue t = C + R]n+ 2 ,
2
by a Forward Euler method. That is, we replace the original coefficient a by the where we have added a correction term. We need to Taylor expand both the
perturbed coefficient a. Observe that a a as t 0. discrete derivative and the arithmetic mean with aid of (5)-(6) and (21)-(22),
The Forward Euler method applied to (41) results in respectively. The result is
1
[Dt+ u = a(1 at)u]n . 1 000 a
2 1 1
u (t 1 )t2 + O(t4 ) + u00e (tn+ 12 )t2 + O(t4 ) = C n+ 2 + Rn+ 2 .
We can control our computations and verify that the truncation error of the 24 e n+ 2 8
scheme above is indeed O(t2 ). 1
The goal now is to make C n+ 2 cancel the t2 terms:
Another way of revealing the fact that the perturbed ODE leads to a more
accurate solution is to look at the amplification factor. Our scheme can be 1 1 000 a
written as C n+ 2 = u (t 1 )t2 + u00e (tn )t2 .
24 e n+ 2 8
19 20
Using u0 = au, we have that u00 = a2 u, and we find that u000 = a3 u. We can geometric and harmonic means where the truncation error involves u0e and even
therefore solve the perturbed ODE problem ue in case of the harmonic mean. So, apart from these two means, choosing ue
to be a linear function of t, ue = ct + d for constants c and d, will make the
1 2 2 truncation error vanish since u00e = 0. Consqeuently, the truncation error of a
u0 = au, a = a(1
a t ),
12 finite difference scheme will be zero since the various approximations used will
by the Crank-Nicolson scheme and obtain a method that is of fourth order all be exact. This means that the linear solution is an exact solution of the
in t. Exercise 7 encourages you to implement these correction terms and discrete equations.
calculate empirical convergence rates to verify that higher-order accuracy is In a particular differential equation problem, the reasoning above can be
indeed obtained in real computations. used to determine if we expect a linear ue to fulfill the discrete equations. To
actually prove that this is true, we can either compute the truncation error and
3.7 Extension to variable coefficients see that it vanishes, or we can simply insert ue (t) = ct + d in the scheme and
see that it fulfills the equations. The latter method is usually the simplest. It
Let us address the decay ODE with variable coefficients, will often be necessary to add some source term to the ODE in order to allow a
linear solution.
u0 (t) = a(t)u(t) + b(t), Many ODEs are discretized by centered differences. From Section 2.4 we
discretized by the Forward Euler scheme, see that all the centered difference formulas have truncation errors involving
u000 2
e or higher-order derivatives. A quadratic solution, e.g., ue (t) = t + ct + d,
[Dt+ u = au + b]n . (42) will then make the truncation errors vanish. This observation can be used
to test if a quadratic solution will fulfill the discrete equations. Note that a
The truncation error R is as always found by inserting the exact solution ue (t) quadratic solution will not obey the equations for a Crank-Nicolson scheme for
in the discrete scheme: u0 = au + b because the approximation applies an arithmetic mean, which
involves a truncation error with u00e .
[Dt+ ue + aue b = R]n . (43)
Using (11)-(12), 3.9 Computing truncation errors in nonlinear problems
1 The general nonlinear ODE
u0e (tn ) u00e (tn )t + O(t2 ) + a(tn )ue (tn ) b(tn ) = Rn .
2
Because of the ODE, u0 = f (u, t), (45)
can be solved by a Crank-Nicolson scheme
u0e (tn ) + a(tn )ue (tn ) b(tn ) = 0,
t 1
so we are left with the result [Dt u0 = f ]n+ 2 . (46)
1 The truncation error is as always defined as the residual arising when inserting
Rn = u00e (tn )t + O(t2 ) . (44) the exact solution ue in the scheme:
2
We see that the variable coefficients do not pose any additional difficulties in this t 1
case. Exercise 8 takes the analysis above one step further to the Crank-Nicolson [Dt u0e f = R]n+ 2 . (47)
scheme. t
Using (21)-(22) for f results in
21 22
The truncation error of approximating u0 (0). The initial conditions for
(48) are u(0) = I and u0 (0) = V . The latter involves a finite difference approxi-
1 000 n+ 1 1 1
mation. The standard choice
u0e (tn+ 21 )+ ue (tn+ 21 )t2 f (ue 2 , tn+ 12 ) u00 (tn+ 12 )t2 +O(t4 ) = Rn+ 2 .
24 8
[D2t u = V ]0 ,
0 n+ 12
Since ue (tn+ 12 ) f (ue , tn+ 21 ) = 0, the truncation error becomes
where u1 is eliminated with the aid of the discretized ODE for n = 0, involves
a centered difference with an O(t2 ) truncation error given by (7)-(8). The
1 1 000 1
Rn+ 2 = ( u (t 1 ) u00 (tn+ 12 ))t2 . simpler choice
24 e n+ 2 8
The computational techniques worked well even for this nonlinear ODE. [Dt+ u = V ]0 ,
is based on a forward difference with a truncation error O(t). A central
question is if this initial error will impact the order of the scheme throughout the
4 Truncation errors in vibration ODEs simulation. Exercise 11 asks you to quickly perform an experiment to investigate
this question.
4.1 Linear model without damping
Truncation error of the equation for the first step. We have shown that
The next example on computing the truncation error involves the following ODE the truncation error of the difference used to approximate the initial condition
for vibration problems: u0 (0) = 0 is O(t2 ), but can also investigate the difference equation used for
the first step. In a truncation error setting, the right way to view this equation
u00 (t) + 2 u(t) = 0 . (48) is not to use the initial condition [D2t u = V ]0 to express u1 = u1 2tV
in order to eliminate u1 from the discretized differential equation, but the
Here, is a given constant. other way around: the fundamental equation is the discretized initial condition
[D2t u = V ]0 and we use the discretized ODE [Dt Dt + 2 u = 0]0 to eliminate
u1 in the disretized initial condition. From [Dt Dt + 2 u = 0]0 we have
The truncation error of a centered finite difference scheme. Using a
standard, second-ordered, central difference for the second-order derivative time, u1 = 2u0 u1 t2 2 u0 ,
we have the scheme
which inserted in [D2t u = V ]0 gives
23 24
1 000 1 2 2 2
Rn = u (0)t2 + O(t3 ) . [u00 + ((1 t )) u = 0 .
6 e 24
There is another way of analyzing the discrete initial condition, because Expanding the squared term and omitting the higher-order term t4 gives
eliminating u1 via the discretized ODE can be expressed as exactly the ODE (54). Experiments show that un is computed to 4th order in
t.
[D2t u + t(Dt Dt u 2 u) = V ]0 . (53)
Writing out (53) shows that the equation is equivalent to (52). The truncation 4.2 Model with damping and nonlinearity
error is defined by The model (48) can be extended to include damping u0 , a nonlinear restoring
(spring) force s(u), and some known excitation force F (t):
[D2t ue + t(Dt Dt ue 2 ue ) = V + R]0 .
Replacing the difference via (7)-(8) and (17)-(18), as well as using u0e (0) = V mu00 + u0 + s(u) = F (t) . (55)
and u00e (0) = 2 ue (0), gives The coefficient m usually represents the mass of the system. This governing
equation can by discretized by centered differences:
1 000
Rn = u (0)t2 + O(t3 ) .
6 [mDt Dt u + D2t u + s(u) = F ]n . (56)
Computing correction terms. The idea of using correction terms to increase The exact solution ue fulfills the discrete equations with a residual term:
the order of Rn can be applied as described in Section 3.6. We look at
[mDt Dt ue + D2t ue + s(ue ) = F + R]n . (57)
2 n
[Dt Dt ue + ue = C + R] ,
Using (17)-(18) and (7)-(8) we get
and observe that C n must be chosen to cancel the t2 term in Rn . That is,
25 26
m 0000
m[Dt Dt ue ]n = mu00e (tn ) +u (tn )t2 + O(t4 ),
000 1 12 e
u = (F 0 u00 s0 (u)u0 ),
m and using the differential equation on the form mu00 + (u0 )2 + s(u) = F , we
1 end up with
u0000 = (F 00 u000 s00 (u)(u0 )2 s0 (u)u00 ),
m
1 1 m 0000
= (F 00 (F 0 u00 s0 (u)u0 ) s00 (u)(u0 )2 s0 (u)u00 ) . Rn = ( u (tn ) + ue (tn )u000 2 4
e (tn ))t + O(t ) .
m m 12 e 12
This result demonstrates that we have second-order accuracy also with quadratic
It is not impossible to discretize the resulting modified ODE, but it is up to damping. The key elements that lead to the second-order accuracy is that the
debate whether correction terms are feasible and the way to go. Computing with difference approximations are O(t2 ) and the geometric mean approximation is
a smaller t is usually always possible in these problems to achieve the desired also of O(t2 ).
accuracy.
27 28
Application of (11)-(12) and (9)-(10) in (68) and (69), respectively, gives
1 1
[Dt u]n 2 = v n 2 , (74)
1
0
ue (tn ) + u00e (tn )t + O(t2 ) = ve (tn ) + Run , (70) 1 1 1
2 [Dt v]n = (F (tn ) |v n 2 |v n+ 2 s(un )) . (75)
1 1 m
ve0 (tn+1 ) ve00 (tn+1 )t + O(t2 ) = (F (tn+1 ) |ve (tn )|ve (tn+1 )+
2 m The truncation error in each equation fulfills
s(ue (tn+1 )) + Rvn . (71)
Since u0e = ve , (70) gives 1 n 12
[Dt ue ]n 2 = ve (tn 21 ) + Ru ,
1 00 1
u (tn )t + O(t2 ) .
Run = [Dt ve ]n = (F (tn ) |ve (tn 12 )|ve (tn+ 12 ) s(un )) + Rvn .
2 e m
In (71) we can collect the terms that constitute the ODE, but the damping term The truncation error of the centered differences is given by (5)-(6), and the
has the wrong form. Let us drop the absolute value in the damping term for geometric mean approximation analysis can be taken from (23)-(24). These
simplicity. Adding a substracting the right form v n+1 v n+1 helps: results lead to
1 1 000 n 1
ve0 (tn+1 ) (F (tn+1 ) ve (tn+1 )ve (tn+1 ) + s(ue (tn+1 ))+ u0e (tn 12 ) + ue (tn 21 )t2 + O(t4 ) = ve (tn 12 ) + Ru 2 ,
m 24
(ve (tn )ve (tn+1 ) ve (tn+1 )ve (tn+1 ))), and
1
ve0 (tn ) =(F (tn ) |ve (tn )|ve (tn ) + O(t2 ) s(un )) + Rvn .
which reduces to m
The ODEs fulfilled by ue and ve are evident in these equations, and we achieve
second-order accuracy for the truncation error in both equations:
ve (tn+1 (ve (tn ) ve (tn+1 )) = ve (tn+1 [Dt ve ]n+1 t
m m n 12
1 Ru = O(t2 ), Rvn = O(t2 ) .
= ve (tn+1 (ve0 (tn+1 )t + ve000 (tn+1 )t+ O(t3 )) .
m 2 Comparing (74)-(75) with (66)-(67), we can hopefully realize that these
We end with Run and Rvn+1 as O(t), simply because all the building blocks in schemes are equivalent (which becomes clear when we implement both). The
the schemes (the forward and backward differences and the linearization trick) obvious advantage with the staggered mesh approach is that we can all the way
are only first-order accurate. However, this analysis is misleading: the building use second-order accurate building blocks and in this way concince ourselves
blocks play together in a way that makes the scheme second-order accurate. This that the resulting scheme has an error of O(t2 ).
is shown by considering an alternative, yet equivalent, formulation of the above
scheme.
5 Truncation errors in wave equations
A centered scheme on a staggered mesh. We now introduce a staggered 5.1 Linear wave equation in 1D
mesh where we seek u at mesh points tn and v at points tn+ 21 in between the u
points. The staggered mesh makes it easy to formulate centered differences in The standard, linear wave equation in 1D for a function u(x, t) reads
the system (62)-(62):
2u 2
2 u
= c + f (x, t), x (0, L), t (0, T ], (76)
1 t2 x2
[Dt u = v]n 2 , (72) where c is the constant wave velocity of the physical medium [0, L]. The equation
1 can also be more compactly written as
[Dt v = (F (t) |v|v s(u))]n . (73)
m
1
The term |v n |v n causes trouble since v n is not computed, only v n 2 and v n+ 2 .
1 utt = c2 uxx + f, x (0, L), t (0, T ], (77)
Using geometric mean, we can express |v n |v n in terms of known quantities: Centered, second-order finite differences are a natural choice for discretizing the
1 1
|v n |v n |v n 2 |v n+ 2 . We then have derivatives, leading to
29 30
The idea is to let Cin cancel the t2 and x2 terms to make Rin = O(t4 , x4 ):
[Dt Dt u = c2 Dx Dx u + f ]ni . (78)
1 1
Inserting the exact solution ue (x, t) in (78) makes this function fulfill the Cin = ue,tttt (xi , tn )t2 c2 ue,xxxx (xi , tn )x2 .
12 12
equation if we add the term R: Essentially, it means that we add a new term
spacing.
Now the unknown values un+1 i+1 , ui
n+1
, and un+1
i1 are coupled, and we must solve
a tridiagonal system to find them. This is in principle straightforward, but it
5.2 Finding correction terms results in an implicit finite difference schemes, while we had a convenient explicit
Can we add correction terms to the PDE and increase the order of Rin in (80)? scheme without the correction terms.
The starting point is
5.3 Extension to variable coefficients
[Dt Dt ue = c2 Dx Dx ue + f + C + R]ni (81)
Now we address the variable coefficient version of the linear 1D wave equation,
From the previous analysis we simply get (80) again, but now with C:
2u u
= (x) ,
t2 x x
1 1
Rin + Cin = ue,tttt (xi , tn )t2 c2 ue,xxxx (xi , tn )x2 + O(t4 , x4 ) . (82) or written more compactly as
12 12
31 32
utt = (ux )x . (83) x 1 x x
[Dx Dx ue ]ni = ([ Dx ue ]ni+ 1 [ Dx ue ]ni 1 )
The discrete counterpart to this equation, using arithmetic mean for and x 2 2
1
centered differences, reads = ([ue,x ]ni+ 1 + Gni+ 1 x2 [ue,x ]ni 1 Gni 1 x2 + O(x4 ))
x 2 2 2 2
1
With the aid of (5)-(6) and (21)-(22) we have [Dx G]ni x2 = Gx (xi , tn )x2 + Gxxx (xi , tn )x4 + O(x4 ) .
24
There will be a number of terms with the x2 factor. We lump these now
1 into O(x2 ). The result of the truncation error analysis of the spatial derivative
[Dx ue ]ni+ 1 = ue,x (xi+ 12 , tn ) + ue,xxx (xi+ 12 , tn )x2 + O(x4 ),
2 24 is therefore summarized as
x 1
[ ]i+ 21 = (xi+ 12 ) + 00 (xi+ 21 )x2 + O(x4 ), x
8 [Dx Dx ue ]ni = (xi )ue,x (xi , tn ) + O(x2 ) .
x 1 x
[ Dx ue ]i+ 1 = ((xi+ 12 ) + 00 (xi+ 21 )x2 + O(x4 ))
n
2 8 After having treated the [Dt Dt ue ]ni term as well, we achieve
1
(ue,x (xi+ 21 , tn ) + ue,xxx (xi+ 21 , tn )x2 + O(x4 )) 1
24 Rin = O(x2 ) + ue,tttt (xi , tn )t2 .
1 12
= (xi+ 12 )ue,x (xi+ 21 , tn ) + (xi+ 12 ) ue,xxx (xi+ 21 , tn )x2 +
24 The main conclusion is that the scheme is of second-order in time and space
1 also in this variable coefficient case. The key ingredients for second order are
ue,x (xi+ 21 ) 00 (xi+ 12 )x2 + O(x4 )
8 the centered differences and the arithmetic mean for : all those building blocks
= [ue,x ]ni+ 1 + Gni+ 1 x2 + O(x4 ), feature second-order accuracy.
2 2
where we have introduced the short form 5.4 1D wave equation on a staggered mesh
5.5 Linear wave equation in 2D/3D
1 1
Gni+ 1 = ( ue,xxx (xi+ 12 , tn )((xi+ 21 ) + ue,x (xi+ 12 , tn ) 00 (xi+ 12 ))x2 . The two-dimensional extension of (76) takes the form
2 24 8
Similarly, we find that
2u 2u 2u
= c2 + 2 + f (x, y, t), (x, y) (0, L) (0, H), t (0, T ], (87)
x t2 x2 y
[ Dx ue ]ni 1 = [ue,x ]ni 1 + Gni 1 x2 4
+ O(x ) .
2 2 2
where now c(x, y) is the constant wave velocity of the physical medium [0, L]
Inserting these expressions in the outer operator (86) results in [0, H]. In the compact notation, the PDE (87) can be written
33 34
[Dt+ ue = Dx Dx ue + f + R]ni .
2
utt = c (uxx + uyy ) + f (x, y, t), (x, y) (0, L) (0, H), t (0, T ], (88)
Now, using (11)-(12) and (17)-(18), we can transform the difference operators to
in 2D, while the 3D version reads derivatives:
n 1 1 1
n+ 12 n+ 1
Ri,j,k =[ ue,tttt t2 c2 ue,xxxx x2 + ue,yyyy x2 + ue,zzzz z 2 ]ni,j,k + [Dt u]i= ([Dx Dx u]ni + [Dx Dx u]n+1 + fi 2 .
12 12 2 i
(92)
The equation for the truncation error is
O(t4 , x4 , y 4 , z 4 ) .
n+ 12 1 n+ 1 n+ 1
[Dt ue ]i = ([Dx Dx ue ]ni + [Dx Dx ue ]n+1
i ) + fi 2 + Ri 2 .
2
6 Truncation errors in diffusion equations To find the truncation error, we start by expressing the arithmetic average in
terms of values at time tn+ 12 . According to (21)-(22),
6.1 Linear diffusion equation in 1D
The standard, linear, 1D diffusion equation takes the form
1 n+ 1 1 n+ 1
([Dx Dx ue ]ni +[Dx Dx ue ]n+1
i ) = [Dx Dx ue ]i 2 + [Dx Dx ue,tt ]i 2 t2 +O(t4 ) .
u u2 2 8
= 2 + f (x, t), x (0, L), t (0, T ], (93)
t x With (17)-(18) we can express the difference operator Dx Dx u in terms of a
where > 0 is the constant diffusion coefficient. A more compact form of the derivative:
diffusion equation is ut = uxx + f .
The spatial derivative in the diffusion equation, ux x, is commonly discretized
n+ 12 1
as [Dx Dx u]ni . The time-derivative, however, can be treated by a variety of [Dx Dx ue ]i = ue,xx (xi , tn+ 12 ) + ue,xxxx (xi , tn+ 12 )x2 + O(x4 ) .
methods. 12
The error term from the arithmetic mean is similarly expanded,
The Forward Euler scheme in time. Let us start with the simple Forward
Euler scheme: 1 n+ 1 1
[Dx Dx ue,tt ]i 2 t2 = ue,ttxx (xi , tn+ 12 )t2 + O(t2 x2 )
8 8
[Dt+ u = Dx Dx u + f ]n . The time derivative is analyzed using (5)-(6):
The truncation error arises as the residual R when inserting the exact solution n+ 12 1
ue in the discrete equations: [Dt u]i = ue,t (xi , tn+ 12 ) + ue,ttt (xi , tn+ 12 )t2 + O(t4 ) .
24
35 36
Summing up all the contributions and notifying that Exercise 5: Empirical estimation of truncation errors
ue,t (xi , tn+ 21 ) = ue,xx (xi , tn+ 21 ) + f (xi , tn+ 12 ), Use the ideas and tools from Section 3.5 to estimate the rate of the trunca-
tion error of the Backward Euler and Crank-Nicolson schemes applied to the
the truncation error is given by exponential decay model u0 = au, u(0) = I.
n+ 21 1 1 Hint. In the Backward Euler scheme, the truncation error can be estimated
Ri = ue,xx (xi , tn+ 12 )t2 + ue,xxxx (xi , tn+ 12 )x2 +
8 12 at mesh points n = 1, . . . , N , while the truncation error must be estimated
1 at midpoints tn+ 12 , n = 0, . . . , N 1 for the Crank-Nicolson scheme. The
ue,ttt (xi , tn+ 21 )t + +O(x4 ) + O(t4 ) + O(t2 x2 )
2
24 truncation_error(dt, N) function to be supplied to the estimate function
needs to carefully implement these details and return the right t array such that
6.2 Linear diffusion equation in 2D/3D t[i] is the time point corresponding to the quantities R[i] and R_a[i].
Filename: trunc_decay_BNCN.py.
6.3 A nonlinear diffusion equation in 2D
Derive the truncation error of the Backward Euler scheme for the decay ODE Compute the truncation error in both cases. Filename: trunc_decay_CN_vc.pdf.
u0 = au with constant a. Extend the analysis to cover the variable-coefficient
case u0 = a(t)u + b(t). Filename: trunc_decay_BE.py. 4 http://tinyurl.com/jvzzcfn/decay/decay convrate.py
37 38
Exercise 9: Truncation error of u0 = f (u, t) Exercise 12: Investigate the accuracy of a simplified scheme
Consider the general nonlinear first-order scalar ODE Consider the ODE
df f 0 f
= u + .
dt u t
Check that the derived truncation error is consistent with previous results for
the case f (u, t) = au. Filename: trunc_nonlinear_ODE.pdf.
39 40
Index
correction terms, 17
decay ODE, 11
finite differences
backward, 6
centered, 8
forward, 7
truncation error
Backward Euler scheme, 6
correction terms, 17
Crank-Nicolson scheme, 8
Forward Euler scheme, 7
general, 4
table of formulas, 8
verification, 21
41