Вы находитесь на странице: 1из 21

Truncation Error Analysis 5 Truncation errors in wave equations 30

5.1 Linear wave equation in 1D . . . . . . . . . . . . . . . . . . . . . 30


5.2 Finding correction terms . . . . . . . . . . . . . . . . . . . . . . . 31
5.3 Extension to variable coefficients . . . . . . . . . . . . . . . . . . 32
Hans Petter Langtangen1,2 5.4 1D wave equation on a staggered mesh . . . . . . . . . . . . . . . 34
5.5 Linear wave equation in 2D/3D . . . . . . . . . . . . . . . . . . . 34
1
Center for Biomedical Computing, Simula Research Laboratory
2
Department of Informatics, University of Oslo
6 Truncation errors in diffusion equations 35
6.1 Linear diffusion equation in 1D . . . . . . . . . . . . . . . . . . . 35
6.2 Linear diffusion equation in 2D/3D . . . . . . . . . . . . . . . . . 37
Dec 12, 2013 6.3 A nonlinear diffusion equation in 2D . . . . . . . . . . . . . . . . 37

7 Exercises 37

WARNING: Preliminary version (expect typos!)

Contents
1 Overview of truncation error analysis 4
1.1 Abstract problem setting . . . . . . . . . . . . . . . . . . . . . . 4
1.2 Error measures . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5

2 Truncation errors in finite difference formulas 6


2.1 Example: The backward difference for u0 (t) . . . . . . . . . . . . 6
2.2 Example: The forward difference for u0 (t) . . . . . . . . . . . . . 7
2.3 Example: The central difference for u0 (t) . . . . . . . . . . . . . . 8
2.4 Overview of leading-order error terms in finite difference formulas 8
2.5 Software for computing truncation errors . . . . . . . . . . . . . . 10

3 Truncation errors in exponential decay ODE 11


3.1 Truncation error of the Forward Euler scheme . . . . . . . . . . . 11
3.2 Truncation error of the Crank-Nicolson scheme . . . . . . . . . . 12
3.3 Truncation error of the -rule . . . . . . . . . . . . . . . . . . . . 13
3.4 Using symbolic software . . . . . . . . . . . . . . . . . . . . . . . 13
3.5 Empirical verification of the truncation error . . . . . . . . . . . 14
3.6 Increasing the accuracy by adding correction terms . . . . . . . . 17
3.7 Extension to variable coefficients . . . . . . . . . . . . . . . . . . 21
3.8 Exact solutions of the finite difference equations . . . . . . . . . 21
3.9 Computing truncation errors in nonlinear problems . . . . . . . . 22

4 Truncation errors in vibration ODEs 23


4.1 Linear model without damping . . . . . . . . . . . . . . . . . . . 23
4.2 Model with damping and nonlinearity . . . . . . . . . . . . . . . 26
4.3 Extension to quadratic damping . . . . . . . . . . . . . . . . . . 27
4.4 The general model formulated as first-order ODEs . . . . . . . . 28

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.

1 Overview of truncation error analysis


1.1 Abstract problem setting
Consider an abstract differential equation

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 .

The solution u of this equation is the numerical solution. To distinguish the


numerical solution from the exact solution of the differential equation problem,
we denote the latter by ue and write the differential equation and its discrete
counterpart as

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:

>>> from truncation_errors import TaylorSeries


1 1 1 >>> from sympy import *
[u ] = (un 2 + un+ 2 ) = u(tn ) + Rn ,
t n
(21) >>> u, dt = symbols(u dt)
2
>>> u_Taylor = TaylorSeries(u, 4)
1 1 0000
Rn = u00 (tn )t2 + u (tn )t4 + O(t6 ) . (22) >>> u_Taylor(dt)
8 384 D1u*dt + D2u*dt**2/2 + D3u*dt**3/6 + D4u*dt**4/24 + u

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

[Dt ue + aue t, = R]n+ . 3.5 Empirical verification of the truncation error


From (13)-(14) and (19)-(20) we get expressions for the terms with ue . Using The task of this section is to demonstrate how we can compute the truncation
that u0e (tn+ ) + aue (tn+ ) = 0, we end up with error R numerically. For example, the truncation error of the Forward Euler
scheme applied to the decay ODE u0 = ua is

1 1 Rn = [Dt+ ue + aue ]n . (35)


Rn+ =( )u00e (tn+ )t + (1 )u00e (tn+ )t2 +
2 2 n
1 2 If we happen to know the exact solution ue (t), we can easily evaluate R from
( + 3)u000 2
e (tn+ )t + O(t )
3
(34) the above formula.
2
To estimate how R varies with the discretization parameter t, which has
For = 1/2 the first-order term vanishes and the scheme is of second order, been our focus in the previous mathematical derivations, we first make the
while for 6= 1/2 we only have a first-order scheme. assumption that R = Ctr for appropriate constants C and r and small enough
t. The rate r can be estimated from a series of experiments where t is varied.
3.4 Using symbolic software Suppose we have m experiments (ti , Ri ), i = 0, . . . , m 1. For two consecutive
experiments (ti1 , Ri1 ) and (ti , Ri ), a corresponding ri1 can be estimated
The previously mentioned truncation_error module can be used to automate by
the Taylor series expansions and the process of collecting terms. Here is an
example on possible use: ln(Ri1 /Ri )
ri1 = , (36)
ln(ti1 /ti )
from truncation_error import DiffOp
from sympy import * for i = 1, . . . , m 1. Note that the truncation error Ri varies through the mesh,
so (36) is to be applied pointwise. A complicating issue is that Ri and Ri1 refer
def decay():
u, a = symbols(u a) to different meshes. Pointwise comparisons of the truncation error at a certain
diffop = DiffOp(u, independent_variable=t, point in all meshes therefore requires any computed R to be restricted to the
num_terms_Taylor_series=3) coarsest mesh and that all finer meshes contain all the points in the coarsest
D1u = diffop.D(1) # symbol for du/dt
ODE = D1u + a*u # define ODE mesh. Suppose we have N0 intervals in the coarsest mesh. Inserting a superscript
n in (36), where n counts mesh points in the coarsest mesh, n = 0, . . . , N0 , leads
# Define schemes to the formula
FE = diffop[Dtp] + a*u
CN = diffop[Dt ] + a*u n
BE = diffop[Dtm] + a*u n ln(Ri1 /Rin )
ri1 = . (37)
theta = diffop[barDt] + a*diffop[weighted_arithmetic_mean] ln(ti1 /ti )
theta = sm.simplify(sm.expand(theta))
# Residuals (truncation errors) Experiments are most conveniently defined by N0 and a number of refinements
R = {FE: FE-ODE, BE: BE-ODE, CN: CN-ODE, m. Suppose each mesh have twice as many cells Ni as the previous one:
theta: theta-ODE}
return R
Ni = 2i N0 , ti = T Ni1 ,
The returned dictionary becomes
where [0, T ] is the total time interval for the computations. Suppose the computed
decay: { Ri values on the mesh with Ni intervals are stored in an array R[i] (R being a
BE: D2u*dt/2 + D3u*dt**2/6, list of arrays, one for each mesh). Restricting this Ri function to the coarsest
FE: -D2u*dt/2 + D3u*dt**2/6,
CN: D3u*dt**2/24, mesh means extracting every Ni /N0 point and is done as follows:

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))

if i == 0: Calling r_R_I = convergence_rates(dt, R_I) computes the sequence of


t_coarse = t # the coarsest mesh rates r0 , r1 , . . . , rm2 for the model RI tr , while the statements

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

def decay_FE(dt, N):


dt = float(dt)
t = np.linspace(0, N*dt, N+1)
u_e = I*np.exp(-a*t) # exact solution, I and a are global
u = u_e # naming convention when writing up the scheme Figure 1: Estimated truncation error at mesh points for different meshes.
R = np.zeros(N)
for n in range(0, N): the Forward Euler scheme for u0 = au, insert the exact solution ue , include a
R[n] = (u[n+1] - u[n])/dt + a*u[n]
residual R, but also include new terms C:
# Theoretical expression for the trunction error
R_a = 0.5*I*(-a)**2*np.exp(-a*t)*dt [Dt+ ue + aue = C + R]n . (39)
return R, t[:-1], R_a[:-1] Inserting the Taylor expansions for [Dt+ ue ]n and keeping terms up to 3rd order
if __name__ == __main__: in t gives the equation
I = 1; a = 2 # global variables needed in decay_FE
trunc_empir.estimate(decay_FE, T=2.5, N_0=6, m=4, makeplot=True)
1 00 1 1
u (tn )t u000 (tn )t2 + u0000 (tn )t3 + O(t4 ) = C n + Rn .
The estimated rates for the integrated truncation error RI become 1.1, 1.0, 2 e 6 e 24 e
and 1.0 for this sequence of four meshes. All the rates for Rn , computed as Can we find C n such that Rn is O(t2 )? Yes, by setting
r_R, are also very close to 1 at all mesh points. The agreement between the
theoretical formula (30) and the computed quantity (ref(35)) is very good, as 1 00
Cn = u (tn )t,
illustrated in Figures 1 and 2. The program trunc_decay_FE.py3 was used to 2 e
perform the simulations and it can easily be modified to test other schemes (see we manage to cancel the first-order term and
also Exericse 5).
1 000
Rn = u (tn )t2 + O(t3 ) .
6 e
3.6 Increasing the accuracy by adding correction terms
The correction term C n introduces 12 tu00 in the discrete equation, and
Now we ask the question: can we add terms in the differential equation that we have to get rid of the derivative u00 . One idea is to approximate u00 by a
can help increase the order of the truncation error? To be precise, let us revisit second-order accurate finite difference formula, u00 (un+1 2un + un1 )/t2 ,
2 http://tinyurl.com/jvzzcfn/trunc/trunc empir.py but this introduces an additional time level with un1 . Another approach is to
3 http://tinyurl.com/jvzzcfn/trunc/trunc decay FE.py rewrite u00 in terms of u0 or u using the ODE:

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

3.8 Exact solutions of the finite difference equations


t 1 1
Having a mathematical expression for the numerical solution is very valuable in [f ]n+ 2 = (f (une , tn ) + f (un+1
e , tn+1 ))
2
program verification since we then know the exact numbers that the program n+ 1 1
should produce. Looking at the various formulas for the truncation errors in = f (ue 2 , tn+ 12 ) + u00e (tn+ 12 )t2 + O(t4 ) .
8
(5)-(6) and (25)-(26) in Section 2.4, we see that all but two of the R expressions
contains a second or higher order derivative of ue . The exceptions are the With (5)-(6) the discrete equations (47) lead to

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

[Dt Dt u + 2 u = 0]n . (49) u1 u0 1


+ 2 tu0 = V . (52)
t 2
Inserting the exact solution ue in this equation and adding a residual R so The first term can be recognized as a forward difference such that the equation
that ue can fulfill the equation results in can be written in operator notation as
1
[Dt Dt ue + 2 ue = R]n . (50) [Dt+ u + 2 tu = V ]0 .
2
To calculate the truncation error Rn , we use (17)-(18), i.e., The truncation error is defined as
1
1 [Dt+ ue + 2 tue V = R]0 .
[Dt Dt ue ] = ue (tn ) + u0000
n 00
(tn )t2 , 2
12 e
Using (11)-(12) with one more term in the Taylor series, we get that
and the fact that u00e (t) + 2 ue (t) = 0. The result is
1 1 1 2
u0e (0) + u00e (0)t + u000 2 3 n
e (0)t + O(t ) + tue (0) V = R .
2 6 2
1 0000
Rn = u (tn )t2 + O(t4 ) . (51)
12 e Now, u0e (0) = V and u00e (0) = 2 ue (0) so we get

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,

1 0000 [mDt Dt ue + D2t ue ]n = mu00e (tn ) + u0e (tn )+


Cn = u (tn )t2 .  
12 e m 0000
To get rid of the 4th-order derivative we can use the differential equation: ue (tn ) + u000 (t n ) t2 + O(t4 )
12 6 e
u00 = u , which implies u0000 = 4 u. Adding the correction term to the ODE
results in Combining this with the previous equation, we can collect the terms

1 2 2 mu00e (tn ) + u0e (tn ) + 2 ue (tn ) + s(ue (tn )) F n ,


u00 + 2 (1 t )u = 0 . (54)
12
Solving this equation by the standard scheme and set this sum to zero because ue solves the differential equation. We are left
with the truncation error
1 2 2  
[Dt Dt u + 2 (1 t )u = 0]n , n m 0000 000
12 R = u (tn ) + ue (tn ) t2 + O(t4 ), (58)
12 e 6
will result in a scheme with trunction error O(t4 ).
We can use another set of arguments to justify that (54) leads to a higher-order so the scheme is of second order.
method. Mathematical analysis of the scheme (49) reveals that the numerical According to (58), we can add correction terms
frequency is (approximately as t 0)  
n m 0000 000
C = u (tn ) + ue (tn ) t2 ,
1 2 2 12 e 6
= (1 + t ) .
24 to the right-hand side of the ODE to obtain a fourth-order scheme. However,
One can therefore attempt to replace in the ODE by a slightly smaller since expressing u0000 and u000 in terms of lower-order derivatives is now harder because
the numerics will make it larger: the differential equation is more complicated:

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.

4.4 The general model formulated as first-order ODEs


4.3 Extension to quadratic damping
The second-order model (59) can be formulated as a first-order system,
Instead of the linear damping term u0 in (55) we now consider quadratic
damping |u0 |u0 :
u0 = v, (62)
00 0 0
mu + |u |u + s(u) = F (t) . (59) 1
v0 = (F (t) |v|v s(u)) . (63)
A centered difference for u0 gives rise to a nonlinearity, which can be linearized m
1 1
using a geometric mean: [|u0 |u0 ]n |[u0 ]n 2 |[u0 ]n+ 2 . The resulting scheme The system (62)-(62) can be solved either by a forward-backward scheme or a
becomes centered scheme on a staggered mesh.
1 1
[mDt Dt u]n + |[Dt u]n 2 |[Dt u]n+ 2 + s(un ) = F n . (60) The forward-backward scheme. The discretization is based on the idea of
stepping (62) forward in time and then using a backward difference in (63) with
The truncation error is defined through
the recently computed (and therefore known) u:
1 1
[mDt Dt ue ]n + |[Dt ue ]n 2 |[Dt ue ]n+ 2 + s(une ) F n = Rn . (61)
[Dt+ u = v]n , (64)
We start with expressing the truncation error of the geometric mean. Ac-
cording to (23)-(24), 1
[Dt v = (F (t) |v|v s(u))]n+1 . (65)
m
1 1 1 1 The term |v|v gives rise to a nonlinearity |v n+1 |v n+1 , which can be linearized as
|[Dt ue ]n 2 |[Dt ue ]n+ 2 = [|Dt ue |Dt ue ]n u0 (tn )2 t2 + u(tn )u00 (tn )t2 +O(t4 ) . |v n |v n+1 :
4 4
Using (5)-(6) for the Dt ue factors results in
[Dt+ u = v]n , (66)
1
1 000 1 [Dt v]n+1 = (F (tn+1 ) |v n |v n+1 s(un+1 )) . (67)
[|Dt ue |Dt ue ]n = |u0e + ue (tn )t2 + O(t4 )|(u0e + u000 (tn )t2 + O(t4 )) m
24 24 e
Each ODE will have a truncation error when inserting the exact solutions ue
We can remove the absolute value since it essentially gives a factor 1 or -1 only. and ve in (64)-(65):
Calculating the product, we have the leading-order terms
1 [Dt+ ue = ve + Ru ]n , (68)
[Dt ue Dt ue ]n = (u0e (tn ))2 + ue (tn )u000 2 4
e (tn )t + O(t ) .
12 1
[Dt ve ]n+1 = (F (tn+1 ) |ve (tn )|ve (tn+1 ) s(ue (tn+1 ))) + Rvn+1 . (69)
With m

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

[Dt Dt ue = c2 Dx Dx ue + f + R]ni (79) 1 


C= utttt t2 c2 uxxxx x2 ,
12
Our purpose is to calculate the truncation error R. From (17)-(18) we have
that to the right-hand side of the PDE. We must either discretize these 4th-order
derivatives directly or rewrite them in terms of lower-order derivatives with the
1 aid of the PDE. The latter approach is more feasible. From the PDE we have
[Dt Dt ue ]ni = ue,tt (xi , tn ) +
ue,tttt (xi , tn )t2 + O(t4 ), that
12
when we use a notation taking into account that ue is a function of two variables 2 2
2
and that derivatives must be partial derivatives. The notation ue,tt means = c ,
t2 x2
2 ue /t2 . so
The same formula may also be applied to the x-derivative term:
1 utttt = c2 uxxtt , uxxxx = c2 uttxx .
[Dx Dx ue ]ni = ue,xx (xi , tn ) + ue,xxxx (xi , tn )x2 + O(x4 ),
12 Assuming u is smooth enough that uxxtt = uttxx , these relations lead to
Equation (81) now becomes 1
C= ((c2 t2 x2 )uxx )tt .
12
1 1 A natural discretization is
ue,tt + ue,tttt (xi , tn )t2 = c2 ue,xx + c2 ue,xxxx (xi , tn )x2 + f (xi , tn )+
12 12 1
O(t4 , x4 ) + Rin . Cin = ((c2 t2 x2 )[Dx Dx Dt Dt u]ni .
12
Because ue fulfills the partial differential equation (PDE) (77), the first, third, Writing out [Dx Dx Dt Dt u]ni as [Dx Dx (Dt Dt u)]ni gives
and fifth terms cancel out, and we are left with

1 un+1 n n1
i+1 2ui+1 + ui+1
2
1 1 t2 x 2
Rin = ue,tttt (xi , tn )t2 c2 ue,xxxx (xi , tn )x2 + O(t4 , x4 ), (80) 
12 12 un+1 2uni + un1 un+1 n n1
i i i1 2ui1 + ui1
+
showing that the scheme (78) is of second order in the time and space mesh x2 x 2

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

x = [Dx ue,x ]ni + [Dx G]ni x2 + O(x4 ) .


[Dt Dt u = Dx Dx u]ni . (84)
The reason for O(x4 ) in the remainder is that there are coefficients in front
The truncation error is the residual R in the equation of this term, say Hx4 , and the subtraction and division by x results in
x
[Dx H]ni x4 .
[Dt Dt ue = Dx Dx ue + R]ni . (85) We can now use (5)-(6) to express the Dx operator in [Dx ue,x ]ni as a
derivative and a truncation error:
The difficulty in the present is how to compute the truncation error of the term
x
[Dx Dx ue ]ni .
We start by writing out the outer operator: 1
[Dx ue,x ]ni = (xi )ue,x (xi , tn ) + (ue,x )xxx (xi , tn )x2 + O(x4 ) .
x 24
x 1  x x

[Dx Dx ue ]ni = [ Dx ue ]ni+ 1 [ Dx ue ]ni 1 . (86) Expressions like [Dx G]ni x2 can be treated in an identical way,
x 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:

utt = c2 (uxx + uyy + uzz ) + f (x, y, z, t), (89) 1


ue,t (xi , tn ) + ue,tt (tn )t + O(t2 ) = ue,xx (xi , tn )+
for (x, y, z) (0, L) (0, H) (0, B) and t (0, T ]. 2
Approximating the second-order derivatives by the standard formulas (17)-
ue,xxxx (xi , tn )x2 + O(x4 ) + f (xi , tn ) + Rin .
(18) yields the scheme 12
The terms ue,t (xi , tn ) ue,xx (xi , tn ) f (xi , tn ) vansih because ue solves the
[Dt Dt u = c2 (Dx Dx u + Dy Dy u) + f ]ni,j,k . (90)
PDE. The truncation error then becomes
The truncation error is found from
1
[Dt Dt ue = c2 (Dx Dx ue + Dy Dy ue ) + f + R]n . (91) Rin = ue,tt (tn )t + O(t2 ) ue,xxxx (xi , tn )x2 + O(x4 ) .
2 12
The calculations from the 1D case can be repeated to the terms in the y and z
directions. Collecting terms that fulfill the PDE, we end up with The Crank-Nicolson scheme in time. The Crank-Nicolson method consists
of using a centered difference for ut and an arithmetic average of the ux x term:

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

7 Exercises Exercise 6: Correction term for a Backward Euler scheme


Exercise 1: Truncation error of a weighted mean Consider the model u0 = au, u(0) = I. Use the ideas of Section 3.6 to add a
correction term to the ODE such that the Backward Euler scheme applied to the
Derive the truncation error of the weighted mean in (19)-(20). perturbed ODE problem is of second order in t. Find the amplification factor.
Filename: trunc_decay_BE_corr.pdf.
Hint. Expand un+1
e and une around tn+ .
Filename: trunc_weighted_mean.pdf.
Exercise 7: Verify the effect of correction terms
Exercise 2: Simulate the error of a weighted mean The program decay_convrate.py4 solves u0 = au, u(0) = I, by the -rule and
computes convergence rates. Copy this file and adjust a in the solver function
We consider the weighted mean
such that it incorporates correction terms. Run the program to verify that
ue (tn ) un+1 + (1 )une . the error from the Forward and Backward Euler schemes with perturbed a is
e
O(t2 ), while the error arising from the Crank-Nicolson scheme with perturbed
Choose some specific function for ue (t) and compute the error in this approxima- a is O(t4 ). Filename: trunc_decay_corr_verify.py.
tion for a sequence of decreasing t = tn+1 tn and for = 0, 0.25, 0.5, 0.75, 1.
Assuming that the error equals Ctr , for some constants C and r, compute r for
the two smallest t values for each choice of and compare with the truncation Exercise 8: Truncation error of the Crank-Nicolson scheme
error (19)-(20). Filename: trunc_theta_avg.py.
The variable-coefficient ODE u0 = a(t)u+b(t) can be discretized in two different
ways by the Crank-Nicolson scheme, depending on whether we use averages for
Exercise 3: Verify a truncation error formula a and b or compute them at the midpoint tn+ 21 :
Set up a numerical experiment as explained in Section 3.5 for verifying the
formulas (15)-(16). Filename: trunc_backward_2level.py. 1
[Dt u = aut + b]n+ 2 , (94)
t n+ 1
Exercise 4: Truncation error of the Backward Euler scheme [Dt u = au + b ] 2 . (95)

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

u0 (t) = f (u(t), t) . mu00 + |u0 |u0 + s(u) = F (t) .


The term |u0 |u0 quickly gives rise to nonlinearities and complicates the scheme.
Show that the truncation error in the Forward Euler scheme, Why not simply apply a backward difference to this term such that it only
involves known values? That is, we propose to solve
[Dt+ u = f (u, t)]n ,
[mDt Dt u + |Dt u|Dt u + s(u) = F ]n .
and in the Backward Euler scheme,
Drop the absolute value for simplicity and find the truncation error of the scheme.
[Dt u = f (u, t)]n , Perform numerical experiments with the scheme and compared with the one
based on centered differences. Can you illustrate the accuracy loss visually in real
both are of first order, regardless of what f is. computations, or is the asymptotic analysis here mainly of theoretical interest?
Filename: trunc_vib_bw_damping.pdf.
Showing the order of the truncation error in the Crank-Nicolson scheme,
1
[Dt u = f (u, t)]n+ 2 ,

is somewhat more involved: Taylor expand une , un+1 n n+1


e , f (ue , tn ), and f (ue , tn+1 )
around tn+ 12 , and use that

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.

Exercise 10: Truncation error of [Dt Dt u]n


Derive the truncation error of the finite difference approximation (17)-(18) to
the second-order derivative. Filename: trunc_d2u.pdf.

Exercise 11: Investigate the impact of approximating u0 (0)


Section 4.1 describes two ways of discretizing the initial conditon u0 (0) = V for
a vibration model u00 + 2 u = 0: a centered difference [D2t u = V ]0 or a forward
difference [Dt+ u = V ]0 . The program vib_undamped.py5 solves u00 + 2 u = 0
with [D2t u = 0]0 and features a function convergence_rates for computing the
order of the error in the numerical solution. Modify this program such that it
applies the forward difference [Dt+ u = 0]0 and report how this simpler and more
convenient approximation impacts the overall convergence rate of the scheme.
Filename: trunc_vib_ic_fw.py.
5 http://tinyurl.com/jvzzcfn/vib/vib undamped.py

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

Вам также может понравиться