Diffusion equations

Linear diffusion equation in 1D

The standard, linear, 1D diffusion equation takes the form

ut=α2ux2+f(x,t),x(0,L), t(0,T],

where α>0 is the constant diffusion coefficient. A more compact form of the diffusion equation is ut=αuxx+f.

The spatial derivative in the diffusion equation, αuxx, is commonly discretized as [DxDxu]ni. The time-derivative, however, can be treated by a variety of methods.

The Forward Euler scheme in time

Let us start with the simple Forward Euler scheme:

[D+tu=αDxDxu+f]n.

The truncation error arises as the residual R when inserting the exact solution ue in the discrete equations:

[D+tue=αDxDxue+f+R]ni.

Now, using (664)-(665) and (670)-(671), we can transform the difference operators to derivatives:

ue,t(xi,tn)+12ue,tt(tn)Δt+O(Δt2)=αue,xx(xi,tn)+α12ue,xxxx(xi,tn)Δx2+O(Δx4)+f(xi,tn)+Rni.

The terms ue,t(xi,tn)αue,xx(xi,tn)f(xi,tn) vanish because ue solves the PDE. The truncation error then becomes

Rni=12ue,tt(tn)Δt+O(Δt2)α12ue,xxxx(xi,tn)Δx2+O(Δx4).

The Crank-Nicolson scheme in time

The Crank-Nicolson method consists of using a centered difference for ut and an arithmetic average of the uxx term:

[Dtu]n+12i=α12([DxDxu]ni+[DxDxu]n+1i+fn+12i.

The equation for the truncation error is

[Dtue]n+12i=α12([DxDxue]ni+[DxDxue]n+1i)+fn+12i+Rn+12i.

To find the truncation error, we start by expressing the arithmetic average in terms of values at time tn+12. According to (674)-(675),

12([DxDxue]ni+[DxDxue]n+1i)=[DxDxue]n+12i+18[DxDxue,tt]n+12iΔt2+O(Δt4).

With (670)-(671) we can express the difference operator DxDxu in terms of a derivative:

[DxDxue]n+12i=ue,xx(xi,tn+12)+112ue,xxxx(xi,tn+12)Δx2+O(Δx4).

The error term from the arithmetic mean is similarly expanded,

18[DxDxue,tt]n+12iΔt2=18ue,ttxx(xi,tn+12)Δt2+O(Δt2Δx2)

The time derivative is analyzed using (658)-(659):

[Dtu]n+12i=ue,t(xi,tn+12)+124ue,ttt(xi,tn+12)Δt2+O(Δt4).

Summing up all the contributions and notifying that

ue,t(xi,tn+12)=αue,xx(xi,tn+12)+f(xi,tn+12),

the truncation error is given by

Rn+12i=18ue,xx(xi,tn+12)Δt2+112ue,xxxx(xi,tn+12)Δx2+124ue,ttt(xi,tn+12)Δt2++O(Δx4)+O(Δt4)+O(Δt2Δx2)

Nonlinear diffusion equation in 1D

We address the PDE

ut=x(α(u)ux)+f(u),

with two potentially nonlinear coefficients q(u) and α(u). We use a Backward Euler scheme with arithmetic mean for α(u),

[Du=Dx¯α(u)xDxu+f(u)]ni.

Inserting ue defines the truncation error R:

[Due=Dx¯α(ue)xDxue+f(ue)]ni.

The most computationally challenging part is the variable coefficient with α(u), but we can use the same setup as in the section Extension to variable coefficients and arrive at a truncation error O(Δx2) for the x-derivative term. The nonlinear term [f(ue)]=ni=f(ue(xi,tn)) matches x and t derivatives of ue in the PDE. We end up with

Rni=122t2ue(xi,tn)Δt+O(Δx2).

Exercises

Exercise B.1: Truncation error of a weighted mean

Derive the truncation error of the weighted mean in (672)-(673).

Hint. Expand uen+1 and uen around tn+θ.

Filename: trunc_weighted_mean.

Exercise B.2: Simulate the error of a weighted mean

We consider the weighted mean

ue(tn)θuen+1+(1θ)uen.

Choose some specific function for ue(t) and compute the error in this approximation for a sequence of decreasing Δt=tn+1tn and for θ=0,0.25,0.5,0.75,1. Assuming that the error equals CΔtr, for some constants C and r, compute r for the two smallest Δt values for each choice of θ and compare with the truncation error (672)-(673). Filename: trunc_theta_avg.

Exercise B.3: Verify a truncation error formula

Set up a numerical experiment as explained in the section Empirical verification of the truncation error for verifying the formulas (668)-(669). Filename: trunc_backward_2level.

Problem B.4: Truncation error of the Backward Euler scheme

Derive the truncation error of the Backward Euler scheme for the decay ODE u=au with constant a. Extend the analysis to cover the variable-coefficient case u=a(t)u+b(t). Filename: trunc_decay_BE.

Exercise B.5: Empirical estimation of truncation errors

Use the ideas and tools from the section Empirical verification of the truncation error to estimate the rate of the truncation error of the Backward Euler and Crank-Nicolson schemes applied to the exponential decay model u=au, u(0)=I.

Hint. In the Backward Euler scheme, the truncation error can be estimated at mesh points n=1,,N, while the truncation error must be estimated at midpoints tn+12, n=0,,N1 for the Crank-Nicolson scheme. The 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 t[i] is the time point corresponding to the quantities R[i] and R_a[i].

Filename: trunc_decay_BNCN.

Exercise B.6: Correction term for a Backward Euler scheme

Consider the model u=au, u(0)=I. Use the ideas of the section Increasing the accuracy by adding correction terms to add a correction term to the ODE such that the Backward Euler scheme applied to the perturbed ODE problem is of second order in Δt. Find the amplification factor. Filename: trunc_decay_BE_corr.

Problem B.7: Verify the effect of correction terms

Make a program that solves u=au, u(0)=I, by the θ-rule and computes convergence rates. Adjust a such that it incorporates correction terms. Run the program to verify that the error from the Forward and Backward Euler schemes with perturbed a is \OofΔt2, while the error arising from the Crank-Nicolson scheme with perturbed a is O(Δt4). Filename: trunc_decay_corr_verify.

Problem B.8: Truncation error of the Crank-Nicolson scheme

The variable-coefficient ODE u=a(t)u+b(t) can be discretized in two different ways by the Crank-Nicolson scheme, depending on whether we use averages for a and b or compute them at the midpoint tn+12:

[Dtu=a¯ut+b]n+12,
[Dtu=¯au+bt]n+12.

Compute the truncation error in both cases. Filename: trunc_decay_CN_vc.

Problem B.9: Truncation error of u=f(u,t)

Consider the general nonlinear first-order scalar ODE

u(t)=f(u(t),t).

Show that the truncation error in the Forward Euler scheme,

[D+tu=f(u,t)]n,

and in the Backward Euler scheme,

[Dtu=f(u,t)]n,

both are of first order, regardless of what f is.

Showing the order of the truncation error in the Crank-Nicolson scheme,

[Dtu=f(u,t)]n+12,

is somewhat more involved: Taylor expand uen, uen+1, f(uen,tn), and f(uen+1,tn+1) around tn+12, and use that

dfdt=fuu+ft.

Check that the derived truncation error is consistent with previous results for the case f(u,t)=au. Filename: trunc_nonlinear_ODE.

Exercise B.10: Truncation error of [DtDtu]n

Derive the truncation error of the finite difference approximation (670)-(671) to the second-order derivative. Filename: trunc_d2u.

Exercise B.11: Investigate the impact of approximating u(0)

The section Linear model without damping describes two ways of discretizing the initial condition u(0)=V for a vibration model u: a centered difference [D_{2t}u=V]^0 or a forward difference [D_t^+u=V]^0. The program vib_undamped.py solves u''+\omega^2u=0 with [D_{2t}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 [D_t^+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.

Problem B.12: Investigate the accuracy of a simplified scheme

Consider the ODE

mu'' + \beta |u'|u' + s(u) = F(t){\thinspace .}

The term |u'|u' quickly gives rise to nonlinearities and complicates the scheme. Why not simply apply a backward difference to this term such that it only involves known values? That is, we propose to solve

[mD_tD_tu + \beta |D_t^-u|D_t^-u + s(u) = F]^n{\thinspace .}

Drop the absolute value for simplicity and find the truncation error of the scheme. Perform numerical experiments with the scheme and compared with the one based on centered differences. Can you illustrate the accuracy loss visually in real computations, or is the asymptotic analysis here mainly of theoretical interest? Filename: trunc_vib_bw_damping.