The numerical experiments in the sections diffu:pde1:FE:experiments and diffu:pde1:theta:experiments reveal that there are some numerical problems with the Forward Euler and Crank-Nicolson schemes: sawtooth-like noise is sometimes present in solutions that are, from a mathematical point of view, expected to be smooth. This section presents a mathematical analysis that explains the observed behavior and arrives at criteria for obtaining numerical solutions that reproduce the qualitative properties of the exact solutions. In short, we shall explain what is observed in Figures diffu:pde1:FE:fig:F=0.5-diffu:pde1:CN:fig:F=10.
A particular characteristic of diffusive processes, governed by an equation like
is that the initial shape $u(x,0)=I(x)$ spreads out in space with time, along with a decaying amplitude. Three different examples will illustrate the spreading of $u$ in space and the decay in time.
The diffusion equation (1) admits solutions that depend on $\eta = (x-c)/\sqrt{4\dfc t}$ for a given value of $c$. One particular solution is
where
is the error function, and $a$ and $b$ are arbitrary constants. The error function lies in $(-1,1)$, is odd around $\eta =0$, and goes relatively quickly to $\pm 1$:
As $t\rightarrow 0$, the error function approaches a step function centered at $x=c$. For a diffusion problem posed on the unit interval $[0,1]$, we may choose the step at $x=1/2$ (meaning $c=1/2$), $a=-1/2$, $b=1/2$. Then
where we have introduced the complementary error function $\mbox{erfc}(\eta) = 1-\mbox{erf}(\eta)$. The solution (4) implies the boundary conditions
For small enough $t$, $u(0,t)\approx 1$ and $u(1,t)\approx 0$, but as $t\rightarrow\infty$, $u(x,t)\rightarrow 1/2$ on $[0,1]$.
The standard diffusion equation $u_t = \dfc u_{xx}$ admits a Gaussian function as solution:
At $t=0$ this is a Dirac delta function, so for computational purposes one must start to view the solution at some time $t=t_\epsilon>0$. Replacing $t$ by $t_\epsilon +t$ in (7) makes it easy to operate with a (new) $t$ that starts at $t=0$ with an initial condition with a finite width. The important feature of (7) is that the standard deviation $\sigma$ of a sharp initial Gaussian pulse increases in time according to $\sigma = \sqrt{2\dfc t}$, making the pulse diffuse and flatten out.
Also, (1) admits a solution of the form
A very important feature is that the initial shape $I(x)=Q\sin\left( kx\right)$ undergoes a damping $\exp{(-\dfc k^2t)}$, meaning that rapid oscillations in space, corresponding to large $k$, are very much faster dampened than slow oscillations in space, corresponding to small $k$. This feature leads to a smoothing of the initial condition with time. (In fact, one can use a few steps of the diffusion equation as a method for removing noise in signal processing.) To judge how good a numerical method is, we may look at its ability to smoothen or dampen the solution in the same way as the PDE does.
The following example illustrates the damping properties of (8). We consider the specific problem
The initial condition has been chosen such that adding two solutions like (8) constructs an analytical solution to the problem:
Figure illustrates the rapid damping of rapid oscillations $\sin (100\pi x)$ and the very much slower damping of the slowly varying $\sin (\pi x)$ term. After about $t=0.5\cdot10^{-4}$ the rapid oscillations do not have a visible amplitude, while we have to wait until $t\sim 0.5$ before the amplitude of the long wave $\sin (\pi x)$ becomes very small.
Evolution of the solution of a diffusion problem: initial condition (upper left), 1/100 reduction of the small waves (upper right), 1/10 reduction of the long wave (lower left), and 1/100 reduction of the long wave (lower right).
A counterpart to (8) is the complex representation of the same function:
where $i=\sqrt{-1}$ is the imaginary unit. We can add such functions, often referred to as wave components, to make a Fourier representation of a general solution of the diffusion equation:
where $K$ is a set of an infinite number of $k$ values needed to construct the solution. In practice, however, the series is truncated and $K$ is a finite set of $k$ values needed to build a good approximate solution. Note that (9) is a special case of (10) where $K=\{\pi, 100\pi\}$, $b_{\pi}=1$, and $b_{100\pi}=0.1$.
The amplitudes $b_k$ of the individual Fourier waves must be determined from the initial condition. At $t=0$ we have $u\approx\sum_kb_k\exp{(ikx)}$ and find $K$ and $b_k$ such that
(The relevant formulas for $b_k$ come from Fourier analysis, or equivalently, a least-squares method for approximating $I(x)$ in a function space with basis $\exp{(ikx)}$.)
Much insight about the behavior of numerical methods can be obtained by investigating how a wave component $\exp{(-\dfc k^2 t)}\exp{(ikx)}$ is treated by the numerical scheme. mathcal{I}_t appears that such wave components are also solutions of the schemes, but the damping factor $\exp{(-\dfc k^2 t)}$ varies among the schemes. To ease the forthcoming algebra, we write the damping factor as $A^n$. The exact amplification factor corresponding to $A$ is $\Aex = \exp{(-\dfc k^2\Delta t)}$.
We have seen that a general solution of the diffusion equation can be built as a linear combination of basic components
A fundamental question is whether such components are also solutions of the finite difference schemes. This is indeed the case, but the amplitude $\exp{(-\dfc k^2t)}$ might be modified (which also happens when solving the ODE counterpart $u'=-\dfc u$). We therefore look for numerical solutions of the form
where the amplification factor $A$ must be determined by inserting the component into an actual scheme. Note that $A^n$ means $A$ raised to the power of $n$, $n$ being the index in the time mesh, while the superscript $n$ in $u^n_q$ just denotes $u$ at time $t_n$.
The exact amplification factor is $\Aex=\exp{(-\dfc^2 k^2\Delta t)}$. We should therefore require $|A| < 1$ to have a decaying numerical solution as well. If $-1\leq A<0$, $A^n$ will change sign from time level to time level, and we get stable, non-physical oscillations in the numerical solutions that are not present in the exact solution.
To determine how accurately a finite difference scheme treats one wave component (12), we see that the basic deviation from the exact solution is reflected in how well $A^n$ approximates $\Aex^n$, or how well $A$ approximates $\Aex$. We can plot $\Aex$ and the various expressions for $A$, and we can make Taylor expansions of $A/\Aex$ to see the error more analytically.
As an alternative to examining the accuracy of the damping of a wave component, we can perform a general truncation error analysis as explained in "Truncation error analysis": "" [Langtangen_deqbook_trunc]. Such results are more general, but less detailed than what we get from the wave component analysis. The truncation error can almost always be computed and represents the error in the numerical model when the exact solution is substituted into the equations. In particular, the truncation error analysis tells the order of the scheme, which is of fundamental importance when verifying codes based on empirical estimation of convergence rates.
The Forward Euler finite difference scheme for $u_t = \dfc u_{xx}$ can be written as
Inserting a wave component (12) in the scheme demands calculating the terms
and
Inserting these terms in the discrete equation and dividing by $A^n e^{ikq\Delta x}$ leads to
and consequently
where
is the numerical Fourier number, and $p=k\Delta x/2$. The complete numerical solution is then
We easily see that $A\leq 1$. However, the $A$ can be less than $-1$, which will lead to growth of a numerical wave component. The criterion $A\geq -1$ implies
The worst case is when $\sin^2 (p/2)=1$, so a sufficient criterion for stability is
or expressed as a condition on $\Delta t$:
Note that halving the spatial mesh size, $\Delta x \rightarrow {\frac{1}{2}} \Delta x$, requires $\Delta t$ to be reduced by a factor of $1/4$. The method hence becomes very expensive for fine spatial meshes.
Since $A$ is expressed in terms of $F$ and the parameter we now call $p=k\Delta x/2$, we should also express $\Aex$ by $F$ and $p$. The exponent in $\Aex$ is $-\dfc k^2\Delta t$, which equals $-F k^2\Delta x^2=-F4p^2$. Consequently,
All our $A$ expressions as well as $\Aex$ are now functions of the two dimensionless parameters $F$ and $p$.
Computing
the Taylor series expansion of $A/\Aex$ in terms of $F$
can easily be done with aid of sympy
:
def A_exact(F, p):
return exp(-4*F*p**2)
def A_FE(F, p):
return 1 - 4*F*sin(p)**2
from sympy import *
F, p = symbols('F p')
A_err_FE = A_FE(F, p)/A_exact(F, p)
print(A_err_FE.series(F, 0, 6))
1 + F*(4*p**2 - 4*sin(p)**2) + F**2*(8*p**4 - 16*p**2*sin(p)**2) + F**3*(32*p**6/3 - 32*p**4*sin(p)**2) + F**4*(32*p**8/3 - 128*p**6*sin(p)**2/3) + F**5*(128*p**10/15 - 128*p**8*sin(p)**2/3) + O(F**6)
The result is
Recalling that $F=\dfc\Delta t/\Delta x^2$, $p=k\Delta x/2$, and that $\sin^2p\leq 1$, we realize that the dominating terms in $A/\Aex$ are at most
We follow the theory explained in "Truncation error analysis": "" [Langtangen_deqbook_trunc]. The recipe is to set up the scheme in operator notation and use formulas from "Overview of leading-order error terms in finite difference formulas": "" [Langtangen_deqbook_trunc] to derive an expression for the residual. The details are documented in "Linear diffusion equation in 1D": "" [Langtangen_deqbook_trunc]. We end up with a truncation error
Although this is not the true error $\uex(x_i,t_n) - u^n_i$, it indicates that the true error is of the form
for two unknown constants $C_t$ and $C_x$.
Discretizing $u_t = \dfc u_{xx}$ by a Backward Euler scheme,
and inserting a wave component (12), leads to calculations similar to those arising from the Forward Euler scheme, but since
we get
and then
The complete numerical solution can be written
We see from (18) that $0<A<1$, which means that all numerical wave components are stable and non-oscillatory for any $\Delta t >0$.
The derivation of the truncation error for the Backward Euler scheme is almost identical to that for the Forward Euler scheme. We end up with
The Crank-Nicolson scheme can be written as
or
Inserting (12) in the time derivative approximation leads to
Inserting (12) in the other terms and dividing by $A^ne^{ikq\Delta x}$ gives the relation
and after some more algebra,
The exact numerical solution is hence
The criteria $A>-1$ and $A<1$ are fulfilled for any $\Delta t >0$. Therefore, the solution cannot grow, but it will oscillate if $1-2F\sin^p < 0$. To avoid such non-physical oscillations, we must demand $F\leq\frac{1}{2}$.
The truncation error is derived in "Linear diffusion equation in 1D": "" [Langtangen_deqbook_trunc]:
An attractive feature of the Forward Euler scheme is the explicit time stepping and no need for solving linear systems. However, the accuracy in time is only $\Oof{\Delta t}$. We can get an explicit second-order scheme in time by using the Leapfrog method:
Written out,
We need some formula for the first step, $u^1_q$, but for that we can use a Forward Euler step.
Unfortunately, the Leapfrog scheme is always unstable for the diffusion equation. To see this, we insert a wave component $A^ne^{ikx}$ and get
or
which has roots
Both roots have $|A|>1$ so the amplitude always grows, which is not in accordance with the physics of the problem. However, for a PDE with a first-order derivative in space, instead of a second-order one, the Leapfrog scheme performs very well.
We can plot the various amplification factors against $p=k\Delta x/2$ for different choices of the $F$ parameter. Figures diffu:pde1:fig:A:err:C20, diffu:pde1:fig:A:err:C0.5, and diffu:pde1:fig:A:err:C0.1 show how long and small waves are damped by the various schemes compared to the exact damping. As long as all schemes are stable, the amplification factor is positive, except for Crank-Nicolson when $F>0.5$.
Amplification factors for large time steps.
Amplification factors for time steps around the Forward Euler stability limit.
Amplification factors for small time steps.
The effect of negative amplification factors is that $A^n$ changes sign from one time level to the next, thereby giving rise to oscillations in time in an animation of the solution. We see from Figure that for $F=20$, waves with $p\geq \pi/4$ undergo a damping close to $-1$, which means that the amplitude does not decay and that the wave component jumps up and down (flips amplitude) in time. For $F=2$ we have a damping of a factor of 0.5 from one time level to the next, which is very much smaller than the exact damping. Short waves will therefore fail to be effectively dampened. These waves will manifest themselves as high frequency oscillatory noise in the solution.
A value $p=\pi/4$ corresponds to four mesh points per wave length of $e^{ikx}$, while $p=\pi/2$ implies only two points per wave length, which is the smallest number of points we can have to represent the wave on the mesh.
To demonstrate the oscillatory behavior of the Crank-Nicolson scheme, we choose an initial condition that leads to short waves with significant amplitude. A discontinuous $I(x)$ will in particular serve this purpose: Figures diffu:pde1:CN:fig:F=3 and diffu:pde1:CN:fig:F=10 correspond to $F=3$ and $F=10$, respectively, and we see how short waves pollute the overall solution.
Diffusion in several dimensions is treated later, but it is appropriate to include the analysis here. We first consider the 2D diffusion equation
which has Fourier component solutions of the form
and the schemes have discrete versions of this Fourier component:
For the Forward Euler discretization,
we get
Introducing
we can write the equation for $\xi$ more compactly as
and solve for $\xi$:
The complete numerical solution for a wave component is
For stability we demand $-1\leq\xi\leq 1$, and $-1\leq\xi$ is the critical limit, since clearly $\xi \leq 1$, and the worst case happens when the sines are at their maximum. The stability criterion becomes
For the special, yet common, case $\Delta x=\Delta y=h$, the stability criterion can be written as
where $d$ is the number of space dimensions: $d=1,2,3$.
The Backward Euler method,
results in
and
which is always in $(0,1]$. The solution for a wave component becomes
With a Crank-Nicolson discretization,
we have, after some algebra,
The fraction on the right-hand side is always less than 1, so stability in the sense of non-growing wave components is guaranteed for all physical and numerical parameters. However, the fraction can become negative and result in non-physical oscillations. This phenomenon happens when
A criterion against non-physical oscillations is therefore
which is the same limit as the stability criterion for the Forward Euler scheme.
The exact discrete solution is
The behavior of the solution generated by Forward Euler discretization in time (and centered differences in space) is summarized at the end of the section diffu:pde1:FE:experiments. Can we, from the analysis above, explain the behavior?
We may start by looking at Figure where $F=0.51$. The figure shows that the solution is unstable and grows in time. The stability limit for such growth is $F=0.5$ and since the $F$ in this simulation is slightly larger, growth is unavoidable.
Figure has unexpected features: we would expect the solution of the diffusion equation to be smooth, but the graphs in Figure contain non-smooth noise. Turning to Figure, which has a quite similar initial condition, we see that the curves are indeed smooth. The problem with the results in Figure is that the initial condition is discontinuous. To represent it, we need a significant amplitude on the shortest waves in the mesh. However, for $F=0.5$, the shortest wave ($p=\pi/2$) gives the amplitude in the numerical solution as $(1-4F)^n$, which oscillates between negative and positive values at subsequent time levels for $F>\frac{1}{4}$. Since the shortest waves have visible amplitudes in the solution profile, the oscillations becomes visible. The smooth initial condition in Figure, on the other hand, leads to very small amplitudes of the shortest waves. That these waves then oscillate in a non-physical way for $F=0.5$ is not a visible effect. The oscillations in time in the amplitude $(1-4F)^n$ disappear for $F\leq\frac{1}{4}$, and that is why also the discontinuous initial condition always leads to smooth solutions in Figure, where $F=\frac{1}{4}$.
Turning the attention to the Backward Euler scheme and the experiments in Figure, we see that even the discontinuous initial condition gives smooth solutions for $F=0.5$ (and in fact all other $F$ values). From the exact expression of the numerical amplitude, $(1 + 4F\sin^2p)^{-1}$, we realize that this factor can never flip between positive and negative values, and no instabilities can occur. The conclusion is that the Backward Euler scheme always produces smooth solutions. Also, the Backward Euler scheme guarantees that the solution cannot grow in time (unless we add a source term to the PDE, but that is meant to represent a physically relevant growth).
Finally, we have some small, strange artifacts when simulating the development of the initial plug profile with the Crank-Nicolson scheme, see Figure, where $F=3$. The Crank-Nicolson scheme cannot give growing amplitudes, but it may give oscillating amplitudes in time. The critical factor is $1 - 2F\sin^2p$, which for the shortest waves ($p=\pi/2$) indicates a stability limit $F=0.5$. With the discontinuous initial condition, we have enough amplitude on the shortest waves so their wrong behavior is visible, and this is what we see as small instabilities in Figure. The only remedy is to lower the $F$ value.
This exercise simulates the exact solution (7). Suppose for simplicity that $c=0$.
a) Formulate an initial-boundary value problem that has (7) as solution in the domain $[-L,L]$. Use the exact solution (7) as Dirichlet condition at the boundaries. Simulate the diffusion of the Gaussian peak. Observe that the solution is symmetric around $x=0$.
b) Show from (7) that $u_x(c,t)=0$. Since the solution is symmetric around $x=c=0$, we can solve the numerical problem in frac{1}{2} of the domain, using a symmetry boundary condition $u_x=0$ at $x=0$. Set up the initial-boundary value problem in this case. Simulate the diffusion problem in $[0,L]$ and compare with the solution in a).
Solution.
Filename: diffu_symmetric_gaussian
.
We consider the problem solved in Exercise 1: Explore symmetry in a 1D problem part b). The boundary condition $u_x(0,t)=0$ can be implemented in two ways: 1) by a standard symmetric finite difference $[D_{2x}u]_i^n=0$, or 2) by a one-sided difference $[D^+u=0]^n_i=0$. Investigate the effect of these two conditions on the convergence rate in space.
Hint. If you use a Forward Euler scheme, choose a discretization parameter $h=\Delta t = \Delta x^2$ and assume the error goes like $E\sim h^r$. The error in the scheme is $\Oof{\Delta t,\Delta x^2}$ so one should expect that the estimated $r$ approaches 1. The question is if a one-sided difference approximation to $u_x(0,t)=0$ destroys this convergence rate.
Filename: diffu_onesided_fd
.
We address diffusion of a Gaussian function as in Exercise 1: Explore symmetry in a 1D problem, in the domain $[0,L]$, but now we shall explore different types of boundary conditions on $x=L$. In real-life problems we do not know the exact solution on $x=L$ and must use something simpler.
a) Imagine that we want to solve the problem numerically on $[0,L]$, with a symmetry boundary condition $u_x=0$ at $x=0$, but we do not know the exact solution and cannot of that reason assign a correct Dirichlet condition at $x=L$. One idea is to simply set $u(L,t)=0$ since this will be an accurate approximation before the diffused pulse reaches $x=L$ and even thereafter it might be a satisfactory condition if the exact $u$ has a small value. Let $\uex$ be the exact solution and let $u$ be the solution of $u_t=\dfc u_{xx}$ with an initial Gaussian pulse and the boundary conditions $u_x(0,t)=u(L,t)=0$. Derive a diffusion problem for the error $e=\uex - u$. Solve this problem numerically using an exact Dirichlet condition at $x=L$. Animate the evolution of the error and make a curve plot of the error measure
Is this a suitable error measure for the present problem?
b) Instead of using $u(L,t)=0$ as approximate boundary condition for letting the diffused Gaussian pulse move out of our finite domain, one may try $u_x(L,t)=0$ since the solution for large $t$ is quite flat. Argue that this condition gives a completely wrong asymptotic solution as $t\rightarrow 0$. To do this, integrate the diffusion equation from $0$ to $L$, integrate $u_{xx}$ by parts (or use Gauss' divergence theorem in 1D) to arrive at the important property
implying that $\int_0^Ludx$ must be constant in time, and therefore
The integral of the initial pulse is 1.
c) Another idea for an artificial boundary condition at $x=L$ is to use a cooling law
where $q$ is an unknown heat transfer coefficient and $u_S$ is the surrounding temperature in the medium outside of $[0,L]$. (Note that arguing that $u_S$ is approximately $u(L,t)$ gives the $u_x=0$ condition from the previous subexercise that is qualitatively wrong for large $t$.) Develop a diffusion problem for the error in the solution using (27) as boundary condition. Assume one can take $u_S=0$ "outside the domain" since $\uex\rightarrow 0$ as $x\rightarrow\infty$. Find a function $q=q(t)$ such that the exact solution obeys the condition (27). Test some constant values of $q$ and animate how the corresponding error function behaves. Also compute $E(t)$ curves as defined above.
Filename: diffu_open_BC
.
a) Generalize (7) to multi dimensions by assuming that one-dimensional solutions can be multiplied to solve $u_t = \dfc\nabla^2 u$. Set $c=0$ such that the peak of the Gaussian is at the origin.
b) One can from the exact solution show that $u_x=0$ on $x=0$, $u_y=0$ on $y=0$, and $u_z=0$ on $z=0$. The approximately correct condition $u=0$ can be set on the remaining boundaries (say $x=L$, $y=L$, $z=L$), cf. Exercise 3: Experiment with open boundary conditions in 1D. Simulate a 2D case and make an animation of the diffused Gaussian peak.
c) The formulation in b) makes use of symmetry of the solution such that we can solve the problem in the first quadrant (2D) or octant (3D) only. To check that the symmetry assumption is correct, formulate the problem without symmetry in a domain $[-L,L]\times [L,L]$ in 2D. Use $u=0$ as approximately correct boundary condition. Simulate the same case as in b), but in a four times as large domain. Make an animation and compare it with the one in b).
Filename: diffu_symmetric_gaussian_2D
.
Consider a diffusion equation with a linear $u$ term:
a) Derive in detail the Forward Euler, Backward Euler, and Crank-Nicolson schemes for this type of diffusion model. Thereafter, formulate a $\theta$-rule to summarize the three schemes.
b) Assume a solution like (8) and find the relation between $a$, $k$, $\dfc$, and $\beta$.
Hint. Insert (8) in the PDE problem.
c) Calculate the stability of the Forward Euler scheme. Design numerical experiments to confirm the results.
Hint. Insert the discrete counterpart to (8) in the numerical scheme. Run experiments at the stability limit and slightly above.
d) Repeat c) for the Backward Euler scheme.
e) Repeat c) for the Crank-Nicolson scheme.
f) How does the extra term $bu$ impact the accuracy of the three schemes?
Hint. For analysis of the accuracy, compare the numerical and exact amplification factors, in graphs and/or by Taylor series expansion.
Filename: diffu_stability_uterm
.