Chapter 27

Numerical Methods for Solving Differential Equations

27.1 Introduction

In the last sections, we have studied numerical methods for solving linear and nonlinear systems of equations. At this point we will turn to numerical methods for solving differential equations. These methods form the basis of what is generally referred to as computational fluid dynamics (CFD). We will study these methods in more detail in section 29. As we will see, there are numerous methods for solving ordinary first-order differential equations. These methods can, with slight modifications, also be used to solver higher-order ordinary differential equations, as well as systems of ordinary differential equations. Before turning to solving partial differential equations, e.g., the Navier-Stokes equation, it is important to study these methods in detail.

27.2 Numerical Solutions to Ordinary Differential Equations

27.2.1 Introduction

Until now we have discussed analytical and numerical approaches to linear systems of equations and numerical approaches to nonlinear systems of equations. However, as we have seen, the most tricky equations we come across are differential equations. In this section, we will look into ways of solving ODEs. This will already account for a number of important equations we have come across and that we solved by using suitable approaches (see section 8.2 and section 8.3). The most important types of ODEs for which we already derived solutions are discussed in section 8.2. We will begin by revisiting some mathematical concepts which we have introduced during the derivation of the Taylor series in section 4.2. We are specifically interested in the concept of the finite differences which we introduced in section 4.2.4. In the following, we will assume a function f which uses the independent variable x.

Infinitesimal Changes. We know from Eq. 4.3 that the Taylor series allows us to write the change in f as x is increased by the infinitesimally small value of Δx as

fx+Δx=n=0nmax1n!dnfdxnΔxn+On,max+1

si28_e  (Eq. 27.1a)

Likewise we know from Eq. 4.4 that the Taylor series allows us to write the change in f as x is decreased by the infinitesimally small value of Δx as

fxΔx=n=0nmax1n1n!dnfdxnΔxn+On,max+1

si29_e  (Eq. 27.1b)

If we expand this series to the expansion order nmax = ∞, we would obtain a correct result. The lower nmax, the less correct we would assume our solution to be. Therefore, we would intuitively assume that small values of nmax will result in high values for the error On,max+1. The higher nmax, the smaller the error On,max+1 will become.

Wide Choice of Numerical Methods. There is a wide range of numerical methods for solving ODEs that are based on these two equations from the Taylor series expansion (see Eq. 27.1). These methods mainly differ on the value of nmax, i.e., on the number of terms they take into account. We will begin with the most simple methods, which are referred to as first-order methods.

27.2.2 First-Order Methods: Euler’s Method

27.2.2.1 Introduction

The first method we will look into is the Euler method that was introduced by Euler in 1768 [1]. This method is one of the methods that are referred to as first-order methods because they use an expansion degree of nmax = 1 for Eq. 27.1. For the first step, we will only restrict ourselves to Eq. 27.1a. Expanding this equation to nmax = 1 yields

fx+Δx=10!d0fdx0Δx0+11!df1dx1Δx1+O2=fx+dfdxxΔx+O2fx+dfdxxΔx

si30_e  (Eq. 27.2)

As you can see, this approximation states that the value of the function f (x + Δx) is equal to the value of the function f at x plus the slope of the function in x times the distance Δx. If you look at Fig. 27.1, you can see this is identical to putting the tangent to the function in x0 and extrapolating for a distance Δx. As you can also see, this approximation makes little mistakes if the curve is smooth and the values of Δx are small (see Fig. 27.1a). For more strongly curved functions f and bigger values for Δx, the approximation becomes inexact (see Fig. 27.1b). In this example, the algorithm would predict a lower value of f (x1) than actually found.

f27-01-9781455731411
Fig. 27.1 Visualization of the Euler method on a function f. The point x1is approximated using the distance Δx from x0 and the slope of the function in x0. For small values of Δx and hardly curved functions f this method approximates f (x1) exactly (a). As the values of Δx increase or the curve becomes strongly curved, the values deviate (b).

The Euler method will allow us predicting the (unknown) value of a function f (x1) at the location x1, starting from an initial (known or assumed) value f (x0) at x0 simply by using the gradient or slope of the function at dfdxx0si31_e at x0. Once this value has been found, we are able to calculate the unknown value of the function f (x2) at x2 from the value of f (x1) and so forth. By repeating this scheme, we are able to derive the entire function, i.e., the values of the function f (xi) for different discreet values xi.

27.2.2.2 Using Euler’s Method to Approximate a Known Function

In order to get a better feeling for what the Euler method actually does, we will look at an example which is not yet an ODE. However, it will allow understanding of what exactly Eq. 27.2 does and what the individual parameters influencing the exactness of the solution actually are. For this we will assume an arbitrary function

fx=14x412x332x2

si32_e  (Eq. 27.3)

For which we find the first derivative as

dfdx=x332x23x

si33_e  (Eq. 27.4)

According to Eq. 27.2 we should be able to reconstruct discreet values of this function using an iterative scheme starting from x0 = 0 and f (0) = 0. The derivative at x0 = 0 is dfdx0=0si34_e. We will assume a step width of Δx = 1. As we will find out in a moment, this value is significantly too big. Applying Eq. 27.2 we find the next value

f0+Δx=f1=f0+dfdx0Δx=0

si35_e

If we look at Eq. 27.3 we can see that the value should be f1=74=1.75si36_e. If we continue with the iteration, we find the values diverging further (see Tab. 27.1). If we decrease Δx to a value of 0.4, we start getting significantly better approximation of the function (see Tab. 27.1).

Tab. 27.1

Computational output of the Euler method for calculating a function iteratively using a constant step width Δx = 1 and Δx = 0.4. The table shows the values from the analytical equation as comparison. As you can see, the numerical approximation is not good for high values of Δx, but becomes significantly better for smaller values of Δx

Xif(Xi) (Euler)f(xi) (correct)
Δx = 1
0.00.0000.000
1.00.000− 1.750
2.0− 5.250− 6.000
3.0− 10.000− 6.750
4.0− 2.2508.000
Δx = 0.4
0.00.0000.000
0.40.000− 0.266
0.8− 0.816− 1.114
1.2− 2.253− 2.506
1.6− 4.118− 4.250
2.0− 6.067− 6.000
2.4− 7.600− 7.258
2.8− 8.064− 7.370
3.2− 6.653− 5.530
3.6− 2.406− 0.778
4.05.7898.000

t0010

Fig. 27.2 shows the influence Δx has on the exactness of the solution. For Δx = 1 we already found the function to be only poorly approximated, which we can confirm from Fig. 27.2a. Decreasing Δx to Δx = 0.4 already catches on significantly better to the function. Starting from Δx = 0.2 we get a close-to-correct approximation and at Δx = 0.08 there is hardly any error anymore.

f27-02-9781455731411
Fig. 27.2 Example of using Euler’s method to approximate a function f (x) iteratively starting from x0 = 0 by applyingEq. 27.3. The obtained values f (xi) are plotted as circles, the solid line is the function f (x) for comparison. As you can see, the smaller Δx, the more closely the function is approximated.

This serves to illustrate that the Euler method is very sensitive to values of Δx which are too big. The higher the required accuracy the smaller the value of Δx should be.

Comparison to Higher-Order Methods. Compared to first-order methods, higher-order methods use higher expansion orders, thus we would find a more complicated expression as Eq. 27.2. However, these methods will provide more stable results at larger values of Δx. In general, this is the trade-off: If taking into account higherorder terms of Eq. 27.1a, the solution is more stable and can tolerate larger values for Δx.

Backwards Finite Distance. As we have worked with Eq. 27.1a, the equations we derived are referred to as the forward finite distance approximation of f (x). Obviously, we could just as well have worked with Eq. 27.1b in which case we would have found

fxΔx=1n10!d0fdx0Δx0+11!df1dx1Δx1+O2=fxdfdxxΔx+O2fxdfdxxΔx

si37_e  (Eq. 27.5)

instead of Eq. 27.2. Eq. 27.5 is referred to as the backwards finite distance approximation of f (x).

27.2.2.3 Using Euler’s Method to Solve an ODE

We will now use Euler’s method to actually solve an ODE. For this we turn to the following example differential equation that we obtained when deriving the discharging characteristics of a capacitor

Q=CU=CRI=CRdQdtdQdt=1CRQ

si38_e

where C is the capacitance, U the voltage, Q the charge, and R the Ohmic resistance. For the sake of simplicity, we will combine the constants to a fixed value and assume the capacitor was full at the beginning of the experiment, i.e., Q(t = 0) = 1. The equation can thus be rewritten to

dQdt=12Q,Qt=0=1

si39_e  (Eq. 27.6)

Obviously, this equation can be solved immediately by separation of variables and partial integration (see section 8.2.2) yielding

Qt=et2

si40_e  (Eq. 27.7)

However, we will now derive this solution numerically.

Iterative Scheme. For this we will employ the iterative scheme given by Eq. 27.2. We know the first value Q(0) = 1 that was given as initial value. With this value, we can calculate the derivative at t = 0 using Eq. 27.6 to be

dQdtt=0=12

si41_e

Employing Eq. 27.2 we can then calculate the next value Q(t + Δt). We will assume a value of Δt = 0.1 in which case we find

QΔt=Q0+dQdt00.1=1120.1=0.95

si42_e

We would then proceed to insert the calculated derivative at 2Δt using Eq. 27.6 as

dQdtΔt=12QΔt=120.95=0.475

si43_e

whereupon we could calculate the value as

Q2Δt=QΔt+dQdtΔt0.1=0.950.4750.1=0.9025

si44_e

We would proceed with this scheme until we have found sufficient discreet values ti of Q(t) to accept as a solution.

Maple Worksheet. Obviously, at this point it makes sense to turn to Maple and implement Euler’s method. Listing 27.1 shows the implementation. We will briefly walk through the code. As usual, we first restart the Maple server and read Core.txt (see line 1). Next, we define the function DoEuler which implements Euler’s method. It accepts the differential equation df and the numerical range of the solution in the parameters _from (lower bound) and _to (upper bound). It also expects the initial value of the function at the lower bound given as parameter initialvalue. The number of points is given as points. It is important to note that the parameter df requires a function both of the value of the function Q and of the independent variable t, as both may be required to evaluate the derivative. Using our example equation (Eq. 27.6), we actually would not require the independent variable for the right-hand side of the equation. However, it is required in general.

Listing 27.1

[Maple] Listing for Euler’s method for solving ODEs. A digital version of the listing can be found under the name ExampleEulerMethod.mw in the supplementary material.

1  restart;read("Core.txt"):
2
3  DoEuler:=proc(df,_from,_to,initialvalue,points) local ret,delta,i:
4    ret:=Matrix(points,2):
5    delta:=(_to-_from)/(points-1):
6    ret[1,1]:=_from:ret[1,2]:=initialvalue:
7    for i from 2 to points do:
8     ret[i,1]:=evalf(ret[i−1,1]+delta):
9     ret[i,2]:=evalf(ret[i−1,2]+delta*df(ret[i−1,1],ret[i−1,2])):
10    end do:
11  return ret;
12 end proc:
13
14 display(quickplot([t->exp(-t/2)],0..4,"x","f(x)"),pointplot(DoEuler((t,Q)->-1/2*Q,0,4,1,41)));

The function first prepares the return value ret that is a points × 2 matrix (see line 4). It then calculates delta and sets the initial values as the first entries in the matrix (see line 6). It then iterates starting from the second line in the matrix (thus the starting value of 2 for i), as we already inserted the initial conditions at the first line. Each iteration calculates another data point.

We now want to use this code for calculating values of ti (see line 8) and Qi (see line 9). The latter equation uses the derivative as a function of Q that must be given as the parameter df. As you can see from line 14, we supply Eq. 27.6 for this function. If this equation is evaluated with a given value Qi at ti it allows calculating dQdttisi45_e. This procedure is repeated for a total of points iterations.

In line 14 we call DoEuler creating a point plot from the resulting matrix. As stated, we supply Eq. 27.6 for df and evaluate the function in the range 0 ≤ t ≤ 4. The initial value for t = 0 is given as 1 and we request a total of 41 points that amounts to a Δt = 0.1 as used in the first step of our manual calculation. We combine this pointplot with the plot of the analytical solution function given by Eq. 27.7 in order to compare the output. The result of this line is shown in Fig. 27.3. The first 10 data points are also shown in Tab. 27.2. As you can see, the numerical solution is pretty close to the analytical solution.

f27-03-9781455731411
Fig. 27.3 Numerical solution using Euler’s method andlisting 27.1. The output shows the discreet numerical solution and the analytical solution using Δt = 0.1. As you can see, the numerical output is a good approximation of the actual solution.

Tab. 27.2

Computational results of the Euler method applied to solving the ODE given byEq. 27.6. The table shows only the first ten values. As can be seen, the numerical results are very close to the actual solution. A constant stepwidth Δt = 0.1 was used during this calculation

tiQ(ti) (Euler)Q(ti) (correct)
0.001.0001.000
0.100.9500.951
0.200.9030.905
0.300.8570.861
0.400.8150.819
0.500.7740.779
0.600.7350.741
0.700.6980.705
0.800.6630.670
0.900.6300.638
1.000.5990.607

Standard Notation. At this point we will quickly introduce a standard notation for iterative schemes, which makes the formulas a little more compact. In this notation, the values of the function f (x) at point xi are abbreviated as F(i), whereas the derivative dfdxxsi46_e at point xi is abbreviated X(i). The stepwidth Δx is generally abbreviated as h. In this notation Eq. 27.2 would be written as

Fi+1=Fi+hXi

si47_e  (Eq. 27.8)

Likewise, the equation for backward finite distance Eq. 27.5 would be written as

Fi1=FihXi

si48_e  (Eq. 27.9)

27.2.2.4 Summary

As you can see, the Euler method can be used conveniently to solve ODEs. We have discussed a simple example, but more complicated ones would not pose a significant problem for the function DoEuler we implemented. However, as we will see in just a moment, the applicability of the Euler method is quite limited due to the fact that it tends to be numerically not very stable and therefore, often fails to find solutions even to pretty simple ODEs.

27.2.3 First-Order Methods: Backward Euler Method

27.2.3.1 Introduction

We have studied the Euler’s method as a very convenient tool for solving ODEs. However, the simplicity of the method comes at a disadvantage which is bad stability. Stability of a numerical method is strongly dependent on the magnitude of the error. During the iterative solving, the error function may grow substantially as a consequence of parasitic solutions, i.e., solutions which are not sought. We have experienced a similar problem during the study of the LORAN positioning system where we have found that our algorithm aborted for wrong initial values (see Tab. 26.2). This is a consequence of a parasitic solution becoming predominant over the actual solution we are looking for. For first-order methods, we usually try to suppress these solutions by using small stepwidth values and choosing suitable initial values. As a quick example, we will numerically solve the ODE

dfdt=af,f0=0

si49_e  (Eq 27.10)

which has the obvious solution obtained by separation of variables and partial integration (see section 8.2.2)

fx=eax

si50_e

We will apply the function DoEuler from listing 27.1 to solve this equation numerically for different values of a. Fig. 27.4 lists the output of the algorithm for different values of a. As you can see, the solution is relatively exact for a = 1, whereas the results for a = 2 are incorrect. For higher values of a, the algorithm becomes numerically unstable. This is a clear example of the numerical instability of this first-order method. As we will see in a moment, multistep or higher-order methods have smaller error functions, which is why they tend to be less likely to grow due to the parasitic solutions. However, we can apply a small trick to Euler’s method, increasing its stability significantly.

f27-04-9781455731411
Fig. 27.4 Numerical instability of the Euler method for solvingEq. 27.10for different values of a. As can be seen, the values are incorrect for a = 2 already and become unstable for values above a > 2 yielding incorrect results. The stepwidth wasx = 0.5. Dots show the numerically obtained discreet solution points, whereas the line shows the analytical solution for comparison.

27.2.3.2 Derivation

The starting equation is Eq. 27.1b. We expand this series to nmax = 1 finding

fxΔx=fxdfdxxΔx+O2fxdfdxxΔx

si51_e  (Eq. 27.11)

Now if we evaluate Eq. 27.11 at the discreet value xi we would find

fxiΔx=fxidfdxxiΔx

si52_e

For the value x(i+ 1) = xi + ΔxEq. 27.11 would amount to

fxi+1=fxi+ΔxΔx=fxi=fxi+Δxdfdxxi+ΔxΔx

si53_e  (Eq. 27.12)

from which we can derive

fxi+1=fxi+dfdtxi+1Δx

si54_e  (Eq. 27.13)

where we have derived an implicit equation for the value of the function at the location x(i+ 1). In standard notation, this equation would be written as

Fi+1=Fi+hXi+1

si2_e  (Eq. 27.14)

This method is commonly referred to as backward Euler method or implicit Euler method. Even though it has the same order as the regular (often referred to as forward) Euler method, it is vastly superior in terms of stability. The equation is implicit, which means it requires the values of the next step in order to calculate the previous. The forward Euler method proceeds by calculating the next value from the previous. The backward Euler method is implicit because values at x(i+ 1) appear on both sides of the equation. Thus for each step, one would need to solve an algebraic equation or potentially (if more than one unknown are used) a system of equations. Once this equation is solved, the value f (x(i+ 1)) is found.

The obvious disadvantage of having to solve an algebraic equation (or a set of equations) comes with one major advantage: stability. The forward Euler method is significantly more stable and thus a very good candidate for ODEs that are troublesome to solve with less stable (but computationally less expensive) algorithms.

27.2.3.3 Applying the Backward Euler Method

Let us return to the example of Eq. 27.10 for which the forward Euler method has failed. We will quickly go through the first steps of solving this ODE using the backward Euler method for a = 10 and Δx = 0.5. We first begin by noting that our ODE is not very complicated and thus, we can write down

dfdtxi+1=10fxi+1

si56_e

directly from Eq. 27.10. We can therefore rewrite Eq. 27.8 to

fxi+1=fxi10xi+1fxi+1Δxfxi+1=fxi1+10Δx

si57_e  (Eq. 27.15)

where we have already obtained our iteration scheme as this solution is already solved for f (xi + Δx). As stated, if the right-hand side of Eq. 27.10 was a bit more complex, we would not be able to directly derive a solution analytically, but would be required to solve the equation for f (xi + Δx) in each step.

We begin with the first value x0 = 0 for which the initial condition gives us f (x0) = 1. From Eq. 27.15 we find for the point f (xi + Δx) as

f0+Δx=fx01+10Δxf0.5=11+100.5=0.166

si58_e

The fact that we are able to write this out so conveniently is by accident. In a moment, we will look at an example of a function where this is not the case, and we need to solve an equation for each iteration.

27.2.3.4 Maple Worksheet

At this point, we will derive a listing for the backward Euler method. The code is shown in listing 27.2. For the most part, this listing is analogous to the forward Euler method (see listing 27.1). The main difference is in line 9 where we inserted a try/catch block. In line 10 we encounter Eq. 27.8 in which f (x(i+ 1)), stored as the variable Fi, is set equal to the last value f (x(i)) plus the evaluated function df at x(i+ 1). As in listing 27.2, df must be given as a function of the independent variable x and of the function value at x(i+ 1) (which is f(x(i+ 1)) or Fi). This will give rise to an algebraic equation that is solved in the next line. In case this solving process is not successful, the algorithm aborts printing out the equation that could not be solved. In line 20 the example function is solved and plotted for a = 10. This is one of the plots shown in Fig. 27.5 where the function was applied to the four equations for which the forward Euler method failed in Fig. 27.4. As you can see, in all of the four cases, the backward Euler method shows stable solutions. This is where the superior stability compared to the forward Euler method becomes obvious. The algorithm is still of first order which means that the solution will not be exceedingly good for large stepwidths, but we will obtain a stable solution in almost all cases.

Listing 27.2

[Maple] Listing for the backward Euler method for solving ODEs. A digital version of the listing can be found under the name ExampleBackwardEulerMethod.mw in the supplementary material.

1 restart;read("Core.txt"):
2
3 DoBackwardsEuler:=proc(df,_from,_to,initialvalue,points) local ret,delta,i,Fi,tosolve:
4  ret:=Matrix(points,2):
5  delta:=(_to-_from)/(points-1):
6  ret[1,1]:=_from:ret[1,2]:=initialvalue:
7  for i from 2 to points do:
8    ret[i,1]:=evalf(ret[i-1,1]+delta):
9    try
10     tosolve:=Fi=ret[i-1,2]+df(ret[i,1],Fi)*delta:
11     ret[i,2]:=rhs(solve({tosolve},{Fi})[1]):
12   catch:
13      print(sprintf("Cannot solve %a",tosolve));
14      return;
15    end try:
16   end do:
17  return ret;
18 end proc:
19
20 display(quickplot([x->exp(-10*x)],0..4,"x","f(x)"),pointplot(DoBackwardsEuler((x,f)->-10*f,0,4,1,9)) );
f27-05-9781455731411
Fig. 27.5 Numerical stability of the backward Euler method for solvingEq. 27.10for different values of a. As can be seen, in all cases stable solutions are obtained. The stepwidth was Δx = 0.5. Data points show the numerically obtained discreet solution points whereas the line shows the analytical solution for comparison.

27.2.3.5 Nonlinear Example

As a short example for a case where we would require to solve individual equations for each iteration, we will consider the following ODE

dfdx=xef

si59_e  (Eq. 27.16)

When applying Eq. 27.8, we would run into equations with f (x(i+ 1)) on the left-hand side and within the exponent of the right-hand side. Here, we would need to apply numerical methods to solve these equations, e.g., Newton’s method (see section 26.3). Luckily, Maple will take care of finding solutions to these equations as we call solve on them. Therefore, our function DoBackwardsEuler will handle this ODE without any problem. The analytical solution to Eq. 27.16 is the following function

fx=lnx22+e

si60_e  (Eq. 27.17)

which is best derived using an Algebra tool. If we apply our function DoBackwardsEuler to Eq. 27.16, we obtain the output shown in Fig. 27.6, which is obviously a very good approximation to the analytical result.

f27-06-9781455731411
Fig. 27.6 Example of nonlinear ODE solved by the backward Euler method using DoBackwardsEuler implemented inlisting 27.2. The solid line is the analytical solutionEq. 27.17, whereas the dots are the output obtained from the backward Euler method. The stepwidth used wasx = 0.5.

27.2.3.6 Conclusion

In this section we have studied the backward Euler method that is a slight modification of the classical forward Euler method. The backward Euler method is a numerically very stable method and can be used to find solutions, even in cases where the forward Euler method fails. The clear disadvantage of the method is the fact that it requires solving an algebraic equation for each iteration, which is computationally more expensive. However, in many applications, especially if less computationally expensive algorithms fail to obtain stable results, the backward Euler method can be used as a very convenient tool.

27.2.4 Other Important First-Order Methods: Forward and Backward Finite Distance

27.2.4.1 Introduction

In this section we will briefly recapitulate two first-order numerical schemes that we already introduced while discussing the Taylor series in section 4.2.4.4. As they are computationally inexpensive and relatively easy to implement, they form the basis of some of the more rudimentary solvers.

27.2.4.2 Forward Finite Distance

The first scheme we will discuss is the forward finite distance scheme given by Eq. 4.7 as

dfdxx=fx+ΔxfxΔx

si61_e

We originally developed this method with the aim of having access to the first derivative. Obviously, we can also use it to calculate subsequent data points by rewriting as

fx+Δx=fx+dfdxxΔx

si62_e

or in standard notation

Fi+1=Fi+hXi

si63_e  (Eq. 27.18)

By comparison with Eq. 27.2, you will find that this is effectively the forward Euler scheme. This scheme is a first-order method with an error function of order two.

27.2.4.3 Backward Finite Distance

The second scheme is the backward finite distance scheme given by Eq. 4.8 as

dfdxx=fxfxΔxΔx

si64_e

which we rewrite to

fx=fxΔx+dfdxxΔx

si65_e

or in standard notation as

Fi=Fi1+hXi

si66_e  (Eq. 27.19)

which again yields a first-order method for approximating F(i).

27.2.5 Important Second-Order Methods

27.2.5.1 Introduction

Besides first-order methods, there are a number of important second-order methods that are commonly used. In this section, we will quickly introduce the most common schemes. Although they are computationally somewhat more expensive to implement, the added accuracy and numerical stability of these methods is generally an advantage to consider. In many cases, it makes sense to default to a second-order scheme, especially in cases where the numerical stability may become critical. All of these methods are one-step methods. We will look at some of the more important multistep methods in just a moment.

27.2.5.2 Crank-Nicolson

We will begin with one method which is especially common in computational fluid dynamics (CFD) and is widely used, e.g., in solving the diffusion equation (Eq. 8.4). We have studied the two one-step first-order Euler methods in section 27.2.2 and section 27.2.3. The forward and the backward Euler methods are two special cases of a wider variety of first-order methods that use weighted averages of these methods. In general these schemes are given as

Fi+1=1ωFi+hXi+ωFi+hXi+1=Fi+h1ωXi+ωXi+1

si67_e  (Eq. 27.20)

where we assume the next value F(i+ 1) to be a weighted average of the value obtained from the forward and the backward Euler methods. Please note that this procedure is very similar to SOR where we also use a weighted average of the last and the next value. The weighting factor is the relaxation parameter ω. For Eq. 27.20 this parameter is 0 ≤ ω ≤ 1 where we obtain the forward Euler scheme for ω = 0 and the backward Euler scheme for ω = 1.

In general, every value of ω can be used, but by far the most common scheme uses ω = 0.5. This scheme is referred to as the Crank-Nicolson method named after John Crank1 and Phyllis Nicolson2 who introduced the method in 1947 [2]. It is given by

Fi+1=Fi+h2Xi+Xi+1

si68_e  (Eq. 27.21)

This linear combination of two first-order methods yields a second-order scheme, i.e., it has an error function of order three. As a consequence, the Crank-Nicolson scheme is significantly more stable than the forward Euler method. Being of second-order, it also yields numerically better results than the backward Euler method which, although being stable, suffers from the imprecision inherent to first-order methods.

Maple Worksheet. We will illustrate the Crank-Nicolson method returning to the example we used when studying the forward (see listing 27.1) and the backward Euler method (see listing 27.2). The implementation of the method is shown in listing 27.3. If you look carefully, you will see that the implementation is identical with the implementation of DoBackwardsEuler in listing 27.2 with the exception of a single line, which is line 10. Here you see that we implement Eq. 27.21 by taking the mean value of the values obtained from the forward and the backward Euler methods. As a consequence, we obtain again an implicit scheme that needs to be solved for the value F(i+ 1).

Listing 27.3

[Maple] Listing for the Crank-Nicolson method for solving ODEs. A digital version of the listing can be found under the name ExampleCrankNicolsonMethod.mw in the supplementary material.

1 restart;read("Core.txt"):
2
3 DoCrankNicolson:=proc(df,_from,_to,initialvalue,points) local ret,delta,i,Fi,tosolve:
4  ret:=Matrix(points,2):
5  delta:=(_to-_from)/(points-1):
6  ret[1,1]:=_from:ret[1,2]:=initialvalue:
7  for i from 2 to points do:
8    ret[i,1]:=evalf(ret[i-1,1]+delta):
9    try
10     tosolve:=Fi=ret[i-1,2]+(df(ret[i-1,1],ret[i-1,2])+df(ret[i,1],Fi))*delta/2:
11     ret[i,2]:=rhs(solve({tosolve},{Fi})[1]):
12    catch:
13     print(sprintf("Cannot solve %a",tosolve));
14     return;
15    end try:
16   end do:
17  return ret;
18 end proc:
19
20 display(quickplot([x->exp(-2*x)],0..4,"x","f(x)"),pointplot(DoCrankNicolson((x,f)->-2*f,0,4,1,9 )) );

If we use the Crank-Nicolson method for solving our example ODE Eq. 27.10, we obtain the results shown in Fig. 27.7. In all cases, we obtain stable solutions. In direct comparison to the solutions obtained from the backward Euler method (see Fig. 27.5) the Crank-Nicolson method achieves better precision.

f27-07-9781455731411
Fig. 27.7 Numerical stability and precision of the Crank-Nicolson method in solvingEq. 27.10for different values of a. As can be seen in all cases, stable solutions are obtained that are closer to the correct analytical solution. The stepwidth wasx = 0.5. Data points show the numerically obtained discreet solution points, whereas the line shows the analytical solution for comparison.

The Crank-Nicolson method is in many cases a good balance if good precision, together with numerical stability, are required. Usually, whenever small stepwidths h are acceptable, forward Euler is the method preferred, as it is sufficiently precise. However, if numerical stability is a problem because larger values of h must be used, implicit schemes must be used. In most of these cases, the Crank-Nicolson method is a good compromise between precision and stability and is often preferred over the pure backward Euler method.

27.2.5.3 Central Finite Distance Scheme

We have already derived the central finite distance scheme while studying some of the most important properties of Taylor series in section 4.2. Originally we derived this equation in order to calculate the derivative dfdxsi69_e at xi from the values f(x(i–1)) and f(x(i+ 1)). For this we derived Eq. 4.6 which reads

dfdxx=fx+ΔxfxΔx2Δx

si70_e

which we can rewrite as a function of f (x + ∆x) as

fx+Δx=fxΔx+2Δxdfdxx

si71_e

or in the standard notation

Fi+1=Fi1+2hXi

si72_e  (Eq. 27.22)

Referring to Eq. 4.6 we find that this is a second-order method, i.e., the error function is of order three, which grants the scheme decent numerical stability. However, it comes with the disadvantage of requiring access to the two most recent data points, a problem known from other schemes, e.g., Adams-Bashforth methods. However, in many applications, a central finite distance scheme can be started, e.g., with an Adams-Moulton scheme in order to obtain the first values. It has the advantage of being computationally inexpensive, which makes it a very convenient method, especially for application where a large number of points must be calculated.

27.2.6 Multistep Methods: Adams-Bashforth Methods

27.2.6.1 Introduction

As we have seen, first-order methods have the advantage of being relatively easy to implement, but the disadvantage of requiring small increments ∆x in order to obtain good solution. We have seen that the backward Euler method can be a convenient solution in these cases, as it is (despite the fact that it is first-order) very stable and often yields results where other algorithms fail. However, it is still a first-order method, i.e., it requires relatively small stepwidths in order to obtain results which approximate the solution correctly.

Obviously, by including higher-order terms in the Taylor series expansions we expect to reduce these errors and come up with methods that allow stable solutions with larger stepwidths. These methods are generally referred to as higher-order or multistep methods. The term higher-order refers to the fact that they take more terms of the Taylor series into account. Multistep refers to the fact that, as we will see, these methods require more than one recent points, i.e., multiple steps in order to produce a result.

In this section we will look at a family of such multistep methods, which are referred to as Adams-Bashforth methods. Interestingly, we have already encountered these methods (see section 24.3), i.e., the application for which these methods were originally developed by Adams and Bashforth while studying the shape of drops [3]. Adams and Bashforth used these schemes to derive the drop contour from a rather tricky differential equation (see Eq. 24.10) that was unsolvable analytically in 1833 and still is today.

27.2.6.2 Derivation

In the following we are again considering a function f (x) that we want to solve numerically. For this we expand Eq. 27.1a and Eq. 27.1b to nmax = 2, respectively. We know from Eq. 4.3 that the Taylor series allows us to write the change in f as x is increased by the infinitesimally small value of ∆x as

fx+Δx=10!d0fdx0Δx0+11!d1fdx1Δx1+12!d2fdx2Δx2+O3

si73_e  (Eq. 27.23a)

fxΔx=10!d0fdx0Δx011!d1fdx1Δx1+12!d2fdx2Δx2+O3

si74_e  (Eq. 27.23b)

Eq. 27.23 are the equations from which all Adams-Bashforth methods are derived. The trick here is to use different functions f (x) for the expansion in the negative direction. The obvious way would be to use f (x) directly. However, we could also use the derivative dfdxxsi75_e when performing the first step. That would effectively mean that we make the Taylor-Series expansion of the derivative.

A Simple Second-Order Formula. We start with the most basic method by simply using f (x) for expanding the Taylor series in the negative direction. Expanding Eq. 27.23b to nmax = 2 we find

fxΔx=fxdfdxxΔx+12d2fdx2xΔx2+O3d2fdx2xΔx2=2fxΔxfx+dfdxxΔxO3

si76_e

which we reinsert into Eq. 27.23a expanded to nmax = 2 to find

fx+Δx=fx+dfdxxΔx+fxΔxfx+dfdxxΔx+O3=fxΔx+2dfdxxΔx+O3

si77_e

or in standard notation

Fi+1=Fi1+2hXi

si72_e

where we now have an equation that allows us to calculate the value of f (x + ∆x) if we know the value of f (x − ∆x) and of the derivative dfdxxsi46_e. This equation is of second order and the error function of third order. This is already one step better than both of the Euler methods. The main disadvantage is that we require information about the two most recent points in order to predict the next value. As we will see in a moment, this is inherent to multistep methods, and it is the main disadvantage of these methods. If you look closely you will see that this is the equation for the central finite distance scheme (see Eq. 27.22).

Second-Order Adams-Bashforth Formulae. Having derived a first second-order formula, we will now turn to the class of formulae referred to as Adams-Bashforth formulae. We will begin by deriving the formulae of second order. For this, we use the first derivative dfdxxsi46_e instead of f (x) for the Taylor series expansion in the negative step direction. Therefore we expand Eq. 27.23b to nmax = 1 in which case we find

dfdxxΔx=dfdxxd2fdx2xΔx+O2d2fdx2xΔx=dfdxxdfdxxΔx+O2

si81_e  (Eq. 27.24)

Now we reinsert this into Eq. 27.23a. We will use the Taylor series of f (x), not of the derivative as we have done for Eq. 27.23b. Please note that this perfectly allowed: We expanded the Taylor series of the derivative. Inserting this equation into a Taylor series of the function itself is obviously perfectly allowed. Again, we expand Eq. 27.23a to nmax = 2 finding

fx+Δx=fx+dfdxxΔx+12d2fdx2xΔx2+O3

si82_e

into which we insert Eq. 27.24 to find

fx+Δx=fx+dfdxxΔx+Δx2dfdxxdfdxxΔx+O2+O3=fx+Δx23dfdxxdfdxxΔx+O3

si83_e  (Eq. 27.25)

or in standard notation

Fi+1=Fi+h23XiXi1

si84_e  (Eq. 27.26)

Here again, we have derived a formula that is of second-order, which means that the error function is of order 3. As stated, this is an advantage over the Euler method where we found an error of order 2 (see Eq. 27.2). However, it may not be directly obvious why Eq. 27.24 should be helpful at all, as this equation contains the derivative of f (x) that we presumably do not know. In fact this is true, but this is not really a problem. If you look closely, you will see that we require only the value of the derivative at x and x − ∆x, i.e., at positions which we have already calculated. The iterative scheme is set up such that we calculate the next value of f at x + ∆x. This means that we already know the value of f (x) and of f (x − ∆x). As the ODE we want to solve will give us an equation which puts f (x) in correlation with dfdxxsi46_e, we can calculate these values of the derivative if we know the values of the function. We therefore replaced the requirements of having access to the full derivative (as required for the Taylor series expansion) to the necessity of having access to the value of the derivative at the last discreet points. As you can see from Eq. 27.26, we must know these values for x and for x − ∆x. Obviously, once the iterative scheme is running, these values are available. However, what happens at the very beginning? Usually we will be provided with an initial value or a boundary condition, therefore we know the value f (xmin) (if xmin was the lower boundary). Using the ODE, we would be able to calculate the value of the derivative, which we shall call X (xmin). But in order to calculate the next value f (xmin + ∆x), we require not only the value of X (xmin), but also of X (xmin − ∆x). As this is below the lower boundary, we are not likely to obtain physically meaningful values at all. This means that in order to start this Adams-Bashforth scheme, we must be provided with more than just the value f (xmin). We also require the value of f (xmin + ∆x). If this value was given, then we can obtain both X (xmin) and X (xmin + ∆x) and thus be able to initiate the scheme.

The requirement of having to have access to more than one recent value of f (x) is a disadvantage of the Adams-Bashforth methods. This is inherent to all multistep methods. They take into account the derivative of the function at several previous points, thus making better approximations of the next point of the function. This makes for significantly better approximations as we will see in just a moment. The Euler method we have been studying only takes into account the most recent point to make a prediction about the value of the function f (x) for the next point. This, in turn, requires only one initial value, whereas the Adams-Bashforth methods require more than one point.

Local and Global Truncation Error. You may have noticed that we dropped the O2x term during the derivation of Eq. 27.26 and only kept the O3 term in the final equation. There are two types of errors that we usually consider in numerical methods. The local truncation error is the error we make when deriving the next data point from the previous data point, i.e., while advancing the calculation by one increment. The global truncation error is the accumulation of all errors made during the calculation. Obviously, it would have been necessary to combine the two error terms in Eq. 27.26 as they combine to form the local truncation error.

However, the main cause of errors in Eq. 27.26 results from the incomplete Taylor series expansion, which is why this is assumed to be the main cause of truncation errors. The error introduced by O2x can be made arbitrarily small by choosing small values for ∆x. By studying the local truncation errors, it is often possible to estimate the number of steps, i.e., the required minimal stepwidth h to keep the result exact to a given degree.

Adams-Bashforth Formulae. We will not derive more than the Adams-Bashforth formula of second order here. It is sufficient to note that these formulae form a series of equations of increasing order. Obviously, the higher the order of the formulae, the smaller the error function. Tab. 27.3 lists the first members of the Adams-Bashforth family. As you can see, these formulae include one additional previous value for each additional degree. The coefficients can be derived numerically [4]. However, in most cases, it is easier to simply look them up.

Tab. 27.3

Family of the Adams-Moulton formulae. The standard notation used is: function f, independent variable x, discreet point xi, discreet point x(i± 1) = xi ± h, function value at discreet point F(i) = f(xi), derivative of the function at discreet pointXi=dfdxxisi1_e, stepwidth h

EquationFormula orderOrder of error function
Fi+1=Fi+hXi+1si2_e12
Fi+1=Fi+h2Xi+1Xisi3_e23
Fi+1=Fi+h125Xi+1+8XiXi1si4_e34
Fi+1=Fi+h2419Xi+1+9Xi5Xi1+Xi2si5_e45
Fi+1=Fi+h720251Xi+1+646Xi264Xi1+106Xi219Xi3si6_e56
Fi+1=Fi+h1440475Xi+1+1427Xi798Xi1+482Xi2173Xi3+27Xi4si7_e67

t0020

The Problem With the First Values. As you can see from Tab. 27.3 the number of recent points required for the Adams-Bashforth formulae increases with the order of the formula. This makes the Adams-Bashforth formulae difficult to apply at the beginning of a calculation where these points are missing. The most commonly employed way around this problem is to use a different (potentially slower or less stable) algorithm to find the first points and then employ an Adams-Bashforth formula to continue the calculation.

27.2.6.3 An Example

Let us proceed by testing Eq. 27.26, i.e., the second-order Adams-Bashforth formula with our previous example of Eq. 27.6. We seek to find a solution to the ODE Eq. 27.6. We have the initial value Q(0) = F(0) = 1, which was sufficient for the Euler method. For the Adams-Bashforth method, we require one additional initial point. We will assume a stepwidth of ∆t = 1. As we will see, the Adams-Bashforth method yields significantly better results than the Euler method for this comparatively high stepwidth. As stated, we require the additional value Q(1) = F(1) = 0.61 for the Adams-Bashforth method which we obtained using the analytical solution to the ODE.

Having access to F(0) and F(1) we can calculate X(0) and X(1) using Eq. 27.6 as

X0=12F0=12Q0=12X1=12F1=12QΔt=120.606531=0.31

si86_e

We can now use Eq. 27.26 to find F(2) as

Q2Δt=F2=F1+Δt23X1X0=0.61+Δt20.93+0.5=0.40

si87_e

Once this value has been derived, we can proceed calculating Q(3∆t), etc.

Maple Worksheet. At this point we implement this scheme quickly as a Maple function in order to make working with it more convenient. The code is shown in listing 27.4. It is very similar to listing 27.1 in which we illustrated the Euler method. Again we first start the Maple server and include Core.txt (see line 1). The implementation of Eq. 27.26 starts at line 4 where we first prepare the return value ret and calculate delta, which is h of Eq. 27.26. The function expects the derivative of the function we want to solve, i.e., the right-hand side of Eq. 27.6 as parameter df. This function is used to calculate X(i). It also requires the boundaries of the range for which the solution is to be obtained as _from and _to. As stated, Eq. 27.26 requires two initial values F(0) and F(1) which must be given to the function as parameters F1 and F2. The number of points from which the stepwidth is calculated is given as parameter points.

Listing 27.4

[Maple] Listing for the second-order Adams-Bashforth method for solving ODEs. A digital version of the listing can be found under the name ExampleAdamsBashforthMethod.mw in the supplementary material.

1 restart;read("Core.txt"):
2
3 #this function implements the second Adams-Bashforth method
4 DoAdamsBashforth2:=proc(df,_from,_to,F0,F1,points) local ret,delta,i:
5   ret:=Matrix(points,2):
6   delta:=(_to-_from)/(points-1):
7   ret[1,1]:=_from:ret[1,2]:=F0:
8   ret[2,1]:=_from+delta:ret[2,2]:=F1:
9   for i from 3 to points do:
10     ret[i,1]:=evalf(ret[i-1,1]+delta):
11     ret[i,2]:=evalf(ret[i-1,2]+delta/2*(3*df(ret[i-1,1],ret[i-1,2])-df(ret[i-2,1],ret[i-2,2]))):
12   end do:
13  return ret;
14 end proc:
15
16 #solve the ODE numerically
17 display(quickplot([t->exp(-t/2)],0..4,"x","f(x)"),pointplot(DoAdamsBashforth2((t,Q)->-1/2*Q  ,0,4,1,0.606531,4)));

Before starting the iteration, the initial values are inserted as the first two lines of entry in the matrix ret (see line 7). In the loop of the iteration, we first calculate the current value for t (see line 10) before applying Eq. 27.26 in line 11. Here we calculate X(i) and X(i− 1) using the derivative df given as a function. This is analogous to the calculation of the derivative in listing 27.1.

Line 17 calls the function obtaining a plot for a stepwidth of h = ∆t = 1 and plotting it alongside the analytical solution to Eq. 27.6. The values obtained during the iteration are shown in Tab. 27.4 alongside the values obtained from the Euler method and the correct values from the analytical equation. As you can see, even for this relatively large stepwidth, the Adams-Bashforth method does a very good job approximating the solution. Fig. 27.8 shows the two methods side-by-side together with the analytical solution.

f27-08-9781455731411
Fig. 27.8 Comparison of the forward Euler and the second-order Adams-Bashforth method for solving the ODE given byEq. 27.6at a constant stepwidth oft = 1. As you can see, the Adams-Bashforth method finds better approximations at these large stepwidths.

Tab 27.5

Family of the Adams-Moulton formulae. The standard notation used is: function f, independent variable x, discreet point xi, discreet point x(i± 1) = xi ± h, function value at discreet point F(i) = f(xi), derivative of the function at discreet point X(i) = dfdxxisi8_e, stepwidth h

EquationFormula orderOrder of error function
Fi+1=Fi+hXi+1si9_e12
Fi+1=Fi+h2Xi+1Xisi10_e23
Fi+1=Fi+h125Xi+1+8XiXi1si11_e34
Fi+1=Fi+h2419Xi+1+9Xi5Xi1+Xi2si12_e45
Fi+1=Fi+h720251Xi+1+646Xi264Xi1+106Xi219Xi3si13_e56
Fi+1=Fi+h1440475Xi+1+1427Xi798Xi1+482Xi2173Xi3+27Xi4si14_e67

t0030

27.2.6.4 Summary

The obvious advantage of the second-order Adams-Bashforth formula is the fact that it is of second-order compared to the Euler method, which is only of first order. This means that the error function of the Adams-Bashforth formula is of third order (see Eq. 27.26) compared to the Euler function, which has an error term of second order (see Eq. 27.2). Therefore the Adams-Bashforth method will produce results that are closer to the actual solution, even for large stepwidths. The second advantage is the fact that because the error functions are of higher order, the formula is less likely to become numerically unstable as we have seen with the Euler method. The obvious downside of the Adams-Bashforth method is the fact that we need more initial values.

27.2.7 Predictor-Corrector Methods: Adams-Moulton Methods

27.2.7.1 Introduction

As we have seen, multistep methods have the advantage of good conversion and high stability with the downside of requiring multiple recent points, which is a problem especially at the beginning of a calculation where (potentially) only one data point may be available from the initial or boundary conditions. In this case, we would require methods which would to “find those values themselves”. This is the regime of predictor-corrector methods. These methods work in two steps

1. They first predict a value with which a preliminary calculation is carried out, after which

2. the value obtained is corrected in case the prediction was not entirely correct.

27.2.7.2 Derivation

We have already seen that all schemes for numerical solvers are derived from the Taylor series (Eq. 27.1), which is either evaluated for f (x) or for the derivative dfdxsi69_e, as we have done during the derivation of the second-order Adams-Bashforth formula. However, we are left perfectly free to expand these series around any points we want, not just x + ∆x or x − ∆x. As an example, take the expansion of Eq. 27.1 for f (x) around x + ∆x expanded to nmax = 2 and the expansion of dfdxsi69_e around x + ∆x expanded to nmax = 1 which yields

fx+Δx=fx+dfdxxΔx+12d2fdx2xΔx2+O3

si82_e  (Eq. 27.27)

dfdxx+Δx=dfdxx+d2fdx2xΔx+O2

si91_e  (Eq. 27.28)

From Eq. 27.28 we find

d2fdx2xΔx2=Δxdfdxx+ΔxdfdxxO2

si92_e

If we now subtract Eq. 27.28 from Eq. 27.27 we find

fx+Δx=fx+dfdxxΔx+Δx2dfdxx+Δxdfdxx+O3=fx+Δx2dfdxx+Δx+dfdxx+O3

si93_e  (Eq. 27.29)

where we have again dropped O2. If we convert Eq. 27.29 to the general notation, we find

Fi+1=Fi+h2Xi+1+Xi+O3Fi+h2Xi+1+Xi

si94_e  (Eq. 27.30)

We note that the equation we obtained is of second-order, which is a good start. However, we also note that we require X(i+ 1) and F(i+ 1) for this equation. In all schemes we derived so far, we could use recent points for the derivative, i.e., X(i) or F(i) in order to calculate F(i+ 1). This seems to be a problem because we do not know F(i+ 1) and therefore, we also do not know X(i+ 1). This seems like a circular reference: We need an unknown first value to calculate an unknown second value, which in turns is required to calculate the first value, etc. This is another example of an implicit scheme as we have already encountered for the backward Euler method (see section 27.2.3).

However, if we were able, by any suitable trick, to circumvent this circular reference, we would be left with one obvious advantage: We do not need any previous values. Therefore this scheme could be an ideal starting algorithm at the beginning of a numerical solution process, as we really only require one value pair F(0) and X(0) (both of which we can calculate from the initial or boundary values) to start the algorithm.

Untangling the Circular Reference. We begin by first finding a prediction F^i+1si95_e for F(i+ 1). In this first step, we want to use an equation which does not require values we do not know yet. For this we note from Eq. 27.27 that a somewhat rough first estimation for F^i+1si95_e would be given by

Fi+1=fx+Δxfx+dfdxxΔx=Fi+hXi

si97_e  (Eq. 27.31)

where we simply expanded the Taylor series to only nmax = 1. Obviously, we know already that this is not a good approximation, as it is first-order. However, we may start our algorithm using this first guess. Once we calculated the prediction value F^i+1si98_e, we can use this value in Eq. 27.30 and calculate the corrected value. The predictor-corrector scheme therefore consists of the following steps

1. Calculate F^i+1si95_e using Eq. 27.31

2. Use this value as value F(i+ 1) for the right-hand side of Eq. 27.30 to calculate X(i+ 1) hence, obtaining the corrected value for F(i+ 1) as the left-hand side of Eq. 27.29

Adams-Moulton Formulae. The method we have just derived is one of a family of methods that are referred to as Adams-Moulton formulae. These methods are predictor-corrector methods, which use information about recent points. They are therefore also multistep methods. The first members of the family of Adams-Moulton formulae are shown in Tab. 27.5. The scheme we just derived is the second-order formula.

Tab. 27.6

Family of the Runge-Kutta formulae. The standard notation used is: function f, independent variable x, discreet point xi, discreet point x(i±1) = xi ± h, function value at discreet point F(i) = f(xi), derivative of the function at discreet pointXi=dfdxxisi1_estepwidth h

OrderEquationc1=dfdx,si16_ec2=dfdx,si17_ec3=dfdx,si18_ec4=dfdx,si19_eOrder of error function
1F(i+ 1)F(i) + h .c1x(i),F(i)2
2F(i+ 1)F(i) + h .c2x(i),F(i)xi+h2,Fi+c12si20_e3
3Fi+1=Fi+h6c1+c2+c3si21_ex(i),F(i)xi+h2,Fi+c12si22_exi+h,Fic1+2c2si23_e4
4Fi+1=Fi+h6c1+2c2+2c3+c4si24_ex(i),F(i)xi+h2,Fi+c12si22_exi+h2,Fi+c22si26_exi+h,Fi+c3si27_e5

t0035

Adams-Bashforth Predictors. If you look at Eq. 27.31, you can see that this is actually the first-order Adams-Bashforth formula (see Tab. 27.3). This, in fact, is a very common procedure: Combining a Adams- Bashforth formula of order p – 1 as predictor method with an Adams-Moulton formula of order p. Obviously, there are other predictor methods that could also be used, e.g., any first-order method, such as Euler or central difference schemes.

27.2.7.3 Example

In order to illustrate the usage of this scheme we will return to the example that we used during the illustration of the Euler and the Adams-Bashforth methods. Again, we seek to find a solution to the ODE Eq. 27.6, again using a relatively large stepwidth Δt = 1. We have found that this yielded quite bad values when using the Euler method, but decent values when using the second Adams-Bashforth scheme. However, the latter required two points to start the algorithm. Considering that we only evaluated a total of four points (see Tab. 27.4) we already provided the algorithm with half the solution. We expect the implicit scheme we just derived to perform better, as we only require the initial condition which is given.

Tab. 27.4

Comparison of computational results of forward Euler method, second-order Adams-Moulton, second-order Adams-Bashforth, and fourth-order Runge-Kutta methods applied to the ODE given byEq. 27.6. The values are Q(ti) for discreet values of t. A constant stepwidtht = 1 was used during this calculation. As can be seen, the methods perform better in increasing order: forward Euler, second-order Adams-Bashforth, second-order Adams-Moulton, and fourth-order Runge-Kutta at constant stepwidth

tiAnalytical solution (correct)Forward EulerSecond-order Adams-BashforthSecond-order Adams-MoultonFourth-order Runge-Kutta
0.0a1.0001.0001.0001.0001.000
1.00.6070.5000.607b0.6250.607
2.00.3680.2500.4020.3910.368
3.00.2230.1250.2520.2440.223
4.00.1350.0630.1630.1530.136

t0025

a initial value (given)

b given

First Prediction. The initial condition gives us F(0) = 1 from which we can calculate X(0) = − 0.5 using Eq. 27.6. For our first prediction we use Eq. 27.31 to find

F^1=F0+hX0=10.5=0.5

si100_e

We already know from the analytical solution that this is not the correct value. The correct value should be 0.606531 (see Tab. 27.4). However, this was only the prediction.

First Correction. We will now use Eq. 27.30 to correct this first prediction. For this we calculate X(1) = − 0.25 using Eq. 27.6. We then use this value for the right-hand side of Eq. 27.30 to find F(1) as

F1=F0+h2X1+X0=1+0.50.250.5=0.625

si101_e

which is significantly closer to the actual solution.

Iterative Scheme. Obviously, we would now use the value of F(1) to reiterate this scheme. At this point, we will once again turn to Maple to create a worksheet that will perform these calculations for us.

27.2.7.4 Maple Worksheet

The listing which implements the second-order Adams-Moulton formula is shown in listing 27.5. As usual, we first restart the Maple server and include Core.txt line 1. The function AdamsMoulton2 implements the scheme we just performed manually. It accepts the differential equation as a function of the independent variable t and the function value Q as the parameter df. It also requires the range given by _from and _to, as well as the initial value F0. Please note that compared to the Adams-Bashforth method we do not require a second initial value here. The number of points required is given as parameter points.

Listing 27.5

[Maple] Listing for the second-order Adams-Moulton method for solving ODEs. A digital version of the listing can be found under the name ExampleAdamsMoultonMethod.mw in the supplementary material.

 1 restart;read("Core.txt"):
 2
 3 #this function implements the second Adams-Moulton method
 4 AdamsMoulton2:=proc(df,_from,_to,FO,points) local ret,delta,i,Fpred:
 5  ret:=Matrix(points,2):
 6  delta:=(_to-_from)/(points-1):
 7  ret [1,1] : = _from:ret [1,2]:=F0:
 8  for i from 2 to points do:
 9    ret[i,1]:=evalf(ret[i-1,1]+delta):
10    Fpred:=ret[i-1,2]+delta*df(ret[i-1,1] ,ret[i-1,2]) :
11    ret[i,2]:=evalf(ret[i-1,2]+delta/2*(df(ret[i,1],Fpred)+df(ret[i-1,1],ret[i-1,2]))):
12   end do:
13  return ret;
14 end proc:
15
16 #solve the ODE numerically
17. display(quickplot([t->exp(-t/2)],0..4,"x","f(x)"), pointplot(AdamsMoulton2((t,Q)->-1/2*Q,0,4,1,4))));

The function then proceeds by calculating the stepwidth h as variable delta and inserts the initial value (see line 7). The prediction according to Eq. 27.31 is implemented in line 10. Here, F^i+1si95_e is calculated based on the values t(i) and F(i) both of which we know. The prediction is then corrected in line 11 according to Eq. 27.30.

The listing plots the numerically obtained function in comparison to the analytical solution in line 17. The plot is shown in Fig. 27.9a. As you can see, the solution is very close to the correct solution. Fig. 27.9b shows the results of the Euler, second-order Adams-Bashforth and second-order Adams-Moulton methods in comparison. As you can see, the output of the Adams-Bashforth and the Adams-Moulton methods are very close to the

f27-09-9781455731411
Fig. 27.9 a) Numerical output of the second-order Adams-Moulton method applied to the ODE Eq. 27.6. Points show the discreet numerical output, the solid line is the analytical solution. b) Comparison of the solution obtained from the forward Euler, second-order Adams-Bashforth, and second-order Adams-Moulton methods when applied to Eq. 27.6. The Adams-Moulton method yields slightly better results than the Adams-Bashforth and significantly better results than the Euler method. The Adams-Moulton method has the additional advantage of only requiring one initial value, whereas the Adams-Bashforth equation requires two initial values. Points show the discreet solution of the respective methods, the solid line is the analytical solution.

solution with the Adams-Moulton method yielding slightly better results. In addition this method comes with the benefit of not having to provide two initial values, as we did for the Adams-Bashforth method. Tab. 27.4 shows the output values of the three methods for better comparison.

27.2.7.5 Summary

In this section we have studied predictor-corrector schemes, which are implicit methods. Here, the term implicit refers to the fact that these schemes do not allow us to calculate the next data point directly, as the formulae give the expression of the next point implicitly. This means that we need a first prediction for the value that we correct in a second step. The most important class of corrector formulae are the Adams-Moulton formulae which we have derived and studied. Usually, the predictor methods of choice are Adams-Bashforth formulae of order p – 1, which are then combined with a Adams-Moulton corrector formula of order p. We have studied examples of such a combination.

As we have shown previously, implicit schemes are significantly more stable than explicit schemes, e.g., the Adams-Bashforth methods alone. However, just as with the forward and the backward Euler method, the implicit methods are significantly more expensive computationally. Here, we require two evaluations per step. In contrast, the backward Euler method required finding solutions to an algebraic equation or a system of algebraic equations. The Adams-Moulton schemes are less tricky to use, as we do not have to solve an equation, but merely evaluate each point twice.

27.2.8 Modern One-Step Methods: Runge-Kutta Methods

27.2.8.1 Introduction

One particularly interesting family of schemes for numerical solutions are the Runge-Kutta methods. They originate from the seminal work of Runge, which is considered the starting point of modern one-step methods. The method was significantly improved first by Heun [5] and later by Kutta [6], who derived the most famous of the Runge-Kutta method, i.e., the fourth-order scheme.

Rationale. Being one-step methods, the Runge-Kutta methods only require the most recent point in order to calculate the next. However, compared to the other methods discussed so far, they make use of the fact that the ODE to solve can also be evaluated at points in between the discreet points for which a solution must be obtained. As an example, the forward Euler method (see Eq. 27.8) approximates the next point F(i+ 1) by taking into account the derivative X(i) at the current point. Obviously, this approximation may become significantly better, if we could take into account the derivative at several points in between xi and xi + h. Obviously, this is possible, as the ODE can be evaluated for any intermediary point xi + img with 0 ≤ imgh.

The Runge-Kutta formulae are a family of methods which take into account the derivative of f (x) at several intermediary points. The second-order method uses two intermediary points, the third-order method, three, and the fourth-order, four. In general, a Runge-Kutta method of order n will require n separate evaluations, thus the methods become more expensive computationally. However, with increasing order they obviously become more exact and more stable as the degree of the error function increases. For the second-order Runge-Kutta method, you will commonly find the abbreviation “RK2”, for the third-order the abbreviation “RK3”, and “RK4” for the fourth-order equation.

Runge-Kutta Formulae. The derivation of these equations is a bit more complex and beyond the scope of this book. They all follow the same concept of using additional intermediary points when evaluating the gradient of the function f (x). Tab. 27.6 gives the equations of the first Runge-Kutta methods. We already know one of these formulae: If you compare the first-order Runge-Kutta method (see Tab. 27.6) with the forward Euler equation (see Eq. 27.2) you will see that they are identical.

In the following, we will use the fourth-order Runge-Kutta method which is the one you will most likely come across.

27.2.8.2 Example

For this example, we return to the ODE of the capacitor discharge given by Eq. 27.6. We apply the fourth-order Runge-Kutta method to solve this equation. For this, we need to calculate the four coefficients given by Tab. 27.6 using the values F(0) = 1 at t0 = 0 and X(0) = − 0.5, which we can calculate from Eq. 27.6. Again, we will use a stepwidth of h = ∆t = 1. We then proceed to calculate the coefficients as

c1=dQdtt0F0=dQdt01=-0.5c2=dQdtt0+h2,F0+c12=dQdt0.5,1-0.25=-0.375c3=dQdtt0+h2,F0+c22=dQdt0.5,1-0.189=-0.406c4=dQdtt0+h,F0+c3=dQdt0.5,1-0.406=-0.297

si103_e

Here you can already see the disadvantage of the Runge-Kutta method: We have to calculate four coefficients, evaluating the ODE at four distinct locations. Obviously, this comes with computational overhead. However, the approximation we obtain for the first value are obviously significantly better. We find

Fi+1=Fi+h6c1+2c2+2c3+c4=1+16-0.5-20.375-0.297=0.60677

si104_e

which is very close to the exact solution 0.606531

27.2.8.3 Maple Worksheet

As for the other schemes, we will now implement this scheme as a Maple worksheet. The listing is shown in listing 27.6. As usual, we first restart the Maple server and include Core.txt. The function that implements the fourth-order Runge-Kutta scheme is called RungeKutta4. It expects the ODE as a function of the independent variable t and of the function value Q as parameter df. The range is again given in the form of the upper and lower boundary _from and _to with a total of points from which the stepwidth h is calculated as variable delta. Being a one-step method, the Runge-Kutta function only requires one initial value F0 as parameter.

Listing 27.6

[Maple] Listing for the fourth order Runge-Kutta method for solving ODEs. A digital version of the listing can be found under the name ExampleRungeKuttaMethod.mw in the supplementary material.

 1 restart;read("Core.txt"):
 2
 3 #this function implements the fourth order Runge-Kutta method
 4 RungeKutta4:=proc(df,_from,_to,F0,points) local ret,delta,i,c1,c2,c3,c4:
 5  ret:=Matrix(points,2):
 6  delta:=(_to-_from)/(points-1):
 7  ret [1,1] : = _f rom:ret [1,2] : = F0:
 8  for i from 2 to points do:
 9    ret [i,1]:=evalf(ret[i-1,1]+delta):
10    c1 :=df (ret [i-1,1] ,ret [i-1,2]):
11    c2:=df(ret[i-1,1]+delta/2,ret[i-1,2]+c1/2):
12    c3:=df (ret [i-1, l]+delta/2 ,ret [i-1,2] + c2/2):
13    c4:=df(ret[i-1,1]+delta,ret[i-1,2]+c3):
14    ret[i,2]:=evalf(ret[i-1,2]+delta/6*(c1+2*c2+2*c3+c4)):
15   end do:
16  return ret;
17 end proc:
18
19 #solve the ODE numerically
20 display(quickplot ([t->exp(-t/2)] ,0. .4, "x","f (x)") .pointplot(RungeKutta4((t,Q)->-1/2*Q,0,4,1,5)));

The function then proceeds preparing the return value ret and calculating the stepwidth delta. The initial value is set in line 7. The algorithm then proceeds iteratively in a loop first calculating the parameters c1, c2, c3, and c4 before calculating the next value according to Tab. 27.6.

The function is then called and the result plotted as point plot alongside the analytical solution in line 20. Fig. 27.10a shows this numerical output obtained from RungeKutta4 when applied to Eq. 27.6 alongside the analytical solution. Fig. 27.10b shows a comparison of all the methods we have studied so far applied to the same equation. Tab. 27.4 shows the numerical output in tabular format. As you can see, the results are very close to the analytical solution. Of all the algorithms we have used to far, the Runge-Kutta method gives the closest approximation. However, it comes with the disadvantage of being numerically more expensive. However, in most applications this is not so critical and Runge-Kutta remains one of the best algorithms to try on a numerical problem, being relatively stable and decently quick in converging.

f27-10-9781455731411
Fig. 27.10 a) Numerical output of the Runge-Kutta method applied to the ODEEq. 27.6. Points show the discreet numerical output, the solid line is the analytical solution. b) Comparison of the solution obtained from the forward Euler, second-order Adams-Bashforth, second-order Adams-Moulton, and fourth-order Runge-Kutta methods when applied toEq. 27.6. The Runge-Kutta method yields the best results of all methods tested. Points show the discreet solution of the respective method, the solid line is the analytical solution.

27.2.8.4 Conclusion

The Runge-Kutta methods (foremost the fourth-order method) are among the most important schemes in numerics. The main advantage of the Runge-Kutta methods is the fact that they do not require knowledge of recent points being one-step methods. This is a significant advantage over Adams-Bashforth methods which require more recent points for the higher-order formulae. As we have experienced from working with first-order methods, the predictions using only a single point are usually not very good. However, the Runge-Kutta methods make use of additional intermediary supporting points in between the discreet intervals and thus, exploit the fact that the ODE is not a discreet function and will therefore, yield information about the value of the derivative in supporting points in between the discreet values for which the numerical scheme must derive a solution. This makes these methods significantly more exact.

The fact that they are numerically expensive makes the Runge-Kutta not the method of choice if a numerical problem with a large number of points must be solved. In these scenarios, they are usually combined with a predictor-corrector method. A common combination is the fourth-order Runge-Kutta method which is used only to calculate the first two points of a problem. At this point, the scheme is changed to a third-order Adams-Bashforth scheme predictor method, combined with a fourth-order Adams-Moulton corrector method for completing the calculation. This combined scheme is often used, as it is (in total) a third-order method with a fourth-order error function, which makes for good numerical stability.

27.3 Numerical Solutions to Higher-Order Ordinary Differential Equations and Systems of Coupled Ordinary Differential Equations

27.3.1 Introduction

Until now, we have studied numerical solutions to ODEs of first order, i.e., equations which only contained dfdxsi69_e, but no higher derivatives. However, we have obviously come across higher-order differential equations frequently and therefore, we need to look into methods for solving these equations as well

An Example: Damped Harmonic Oscillation. As we will see in a moment, we can transform all higher- order ODEs into a system of coupled first-order ODEs. As an example, we will consider the problem of the damped harmonic oscillation. An object of mass m is suspended on a spring with the spring constant D and left to oscillate (see Fig. 27.11a). The mass is suspended in a medium which exerts viscous damping on the movement, described by the damping constant k. The forces involved are

f27-11-9781455731411
Fig. 27.11 Example of damped harmonic oscillation. a) A mass m suspended on a spring with spring constant I) surrounded with a viscous media with the damping constant k. The mass is displaced from the equilibrium position at t = 0 to the position s (0) = S. After releasing, it will oscillate. b) Force balance during the downwards movement.

Fspring=DstFdamping=kvt=kdsdttFgravity=mat=md2sdt2t

si106_e

A balance of force (see Fig. 27.11b) gives rise to the second-order ODE

md2sdt2t+kdsdtt+Dst=0

si107_e  (Eq. 27.32)

We assume the initial values s (t = 0) = S and vt=0=dsdtt=0=0si108_e, i.e., the mass is left to oscillate starting from the maximum amplitude S where it was at rest initially.

Analytical Solution. Eq. 27.32 is a linear ODE with constant coefficients which we have studied in section 8.2.3. It gives rise to the characteristic polynomial

mλ2+kλ+D=0

si109_e  (Eq. 27.33)

which has the two roots

λ1=-k+k2-4mD2mλ2=-k-k2-4mD2m

si110_e

and the analytical solution

st=c0eλ1t+c1eλ2t

si111_e  (Eq. 27.34)

Obviously, the term k2 - 4 mD is crucial, as it decides whether or not the two solutions are complex (in which case we expect sine and cosine to show up as solutions according to section 8.2.3.4) or not (in which case we have only exponential terms).

Overdamped Oscillation.Overdamped oscillation occurs if k2 - 4 mD > 0. In this case, there will be no oscillation, but simply a quick decay given by Eq. 27.34 as

st=c0e-k+k2-4mD2m+c1e-k-k2-4mD2mt

si112_e  (Eq. 27.35)

We now have to apply the initial conditions for which we know that v (t = 0) = 0. from which we find

dsdt0=-k+k2-4mD2mc0e-k-k2-4mD2m0-k+k2-4mD2mc1e-k-k2-4mD2m0=-k+k2-4mD2mc0-k+k2-4mD2mc1=0c0=c1k+k2-4mD-k+k2-4mD

si113_e

which simplifies Eq. 27.35 to

st=c1k+k2-4mD-k+k2-4mDe-k+k2-4mD2mt+e-k-k2-4mD2mt

si114_e  (Eq. 27.36)

Using the second initial condition s (t = 0) = S we find

s0=c1k+k2-4mD-k+k2-4mDe-k+k2-4mD2m0+e-k-k2-4mD2m0=c1k+k2-4mD-k+k2-4mD+1=c1k+k2-4mD-k+k2-4mD-k+k2-4mD=c12k2-4mD-k+k2-4mD=Sc1=S-k+k2-4mD2k2-4mD

si115_e

in which case, our overall solution Eq. 27.36 becomes

st=S-k+k2-4md2k2-4mdk+k2-4md-k+k2-4mde-k+k2-4md2mt+e-k-k2-4md2mt=S2k2-4mdk+k2-4mde-k+k2-4md2mt+-k+k2-4mde-k-k2-4md2mt

si116_e  (Eq. 27.37)

We will later use this equation for fixed values of S = 1, m = 1, k = 3, and D = 1. In this case Eq. 27.37 becomes

st=1253+5e-3+52t+-3+5e-3-52t=5103+5e-3+52t+-3+5e-3-52t

si117_e  (Eq. 27.38)

Critically-Damped Oscillation.Critically-damped oscillation occurs if k2 − 4 mD = 0. This is a special case of overdamped oscillation with the recurring root λ1=λ2=-k2msi118_e in which case we find the solution according to Eq. 8.34 to be

st=c0+c1te-k2mtvt=dsdt=-k2mc0+c1te-k2mt+c1e-k2mt=c1-c0+c1tk2me-k2mt

si119_e

See section 8.2.3.3 for details on ODEs with recurring roots. Again we apply the initial condition v (t = 0) = 0 to find

v0=e-k2m0c1-c0+c10k2m=c1-c0k2m=!0c1=c0k2m

si120_e

which simplifies the solution to

st=c01+k2mte-k2mt

si121_e

The second initial condition s (t = 0) = S yields

s0=c01+k2m0e-k2m0=c0=S!

si122_e

in which case we find the overall solution to be

st=S1+k2mte-k2mt

si123_e  (Eq. 27.39)

We will later use the critically-damped case for the values S = 1, m = 1, k = 2, and D = 1. In this case Eq. 27.39 becomes

st=1+te-t

si124_e  (Eq. 27.40)

Underdamped Oscillation.Underdamped oscillation which occurs if k2 - 4 mD < 0. In this case we find complex roots given by

λ1=-k+i4mD-k22mλ2=-k-i4mD-k22m

si125_e

and a general solution according to Eq. 27.34 given by

st=c0e-k+4mD-k22mt+c1e-k-14mD-k22mt=e-k2mtc0e-1k2-4mD2mt+c1e-1-4mD-k22mt=e-k2mtc2sin4mD-k22mt+c3cos4mD-k22mt

si126_e

Again, we apply the initial condition v (t = 0) = 0 to find

v0=-k2me-k2m0c2sin4mD-k22m0+c3cos4mD-k22m0+e-k2m04mD-k22m0c2cos4mD-k22m0-c3sin4mD-k22m0=-k2mc3+4mD-k22mc2c3=4mD-k2kc2

si127_e

which simplifies our solution to

st=e-k2mtc2sin4mD-k22mt+4mD-k22kcos4mD-k22mt

si128_e

Applying the second boundary condition s (t = 0) = S yields

s0=e-k2m0c2sin4mD-k22m0+4mD-k2kcos4mD-k22m0=c24mD-k22k=!Sc2=Sk4mD-k2

si129_e

in which case we find the total solution to be

st=Se-k2mtk4mD-k2sin4mD-k22mt+cos4mD-k22mt

si130_e  (Eq. 27.41)

We will later use the underdamped case for the values S = 1, m = 5, k = 1, and D = 1. In this case Eq. 27.39 becomes

st=e-t101919sin1910t+cos1910t

si131_e  (Eq. 27.42)

Visualization.Fig. 27.12a visualizes Eq. 27.37 and Eq. 27.39 for selected values of k, m. and D. You can also see the special case of the critically-damped oscillation, which is the fastest decaying oscillation. Fig. 27.12b visualizes several cases of underdamped oscillation where we can see the exponentially decaying oscillation function.

f27-12-9781455731411
Fig. 27.12 Over-, under-, and critically-damped oscillation as a function of time according toEq. 27.39, Eq. 27.39, and Eq. 27.41. The values assumed for the damping constant k, the spring constant D, and the mass m are given as relative values without units.

27.3.2 Converting Higher-Order ODEs

Now that we know the analytical solution to Eq. 27.32, we want to find numerical solutions. For this we first need to convert the second-order ODE to a first-order system of ODEs. For this we simply introduce a second dependent variable v which we couple with the actual dependent variable s by

v=dsdt

si132_e

for which the first derivative with respect to time as

dvdt=d2sdt2

si133_e

In other words, we simply replace all derivatives higher than the first order of s by u and its derivative. In all cases, we must make sure that we introduce this new dependent variable v as a function of the independent variable, in this case of t. Therefore, we now have the following coupled system of ODEs

mdvdtt+kvt+Dst=0dvdtt=-1mkvt+Dst

si134_e  (Eq. 27.43a)

dsdtt=vt

si135_e  (Eq. 27.43a)

with the initial values s (0) = S and v (0) = 0. Using this scheme, any ODE of order n can be transferred into a system of n coupled first-order ODEs.

27.3.3 Solving Coupled Systems of First-Order ODEs

Now that we have a method to convert a higher-order ODE to a system of first-order ODEs we obviously need a method to solve such systems. This we can do by extending the methods we have studied for solving first-order ODEs.

Before returning to the actual problem, we will first convert this problem in a more general form. Let us assume that we have a system of n coupled first-order ODEs fi with the interdependent variables xi and the independent variable t. Interdependent means that each xi depends on the independent variable t and on all other dependent values xj with i ≠ j. In a more general notation, we can write this as

dx1dt=f1tx1x2xndx2dt=f2tx1x2xndxndt=fntx1x2xn

si136_e

We have already encountered such systems of equations during the derivation of solutions for the linear systems of equations in section 25.2. There we introduced a vector notation that we will do here as well. In this notation, we can rewrite the system as

dxdt=ftxdxdt-ftx=0

si137_e  (Eq. 27.44)

with fsi138_e being a vector function and xsi139_e being the vector value, which makes Eq. 27.44 equal the zero vector 0si140_e. In other word, xsi139_e is the root of Eq. 27.44.

Interestingly, this does not increase the difficulty of the algorithms we have studied. If you look carefully at Eq. 27.44, you will see that this is simply a set of n equations, each of which can be solved step-by-step by a suitable numerical scheme. We start with the initial values X(1,1), X(2,1),…,X(n,1)and calculate (by a suitable scheme) the values X(1,2), X(2,2),…,X(n,2). These values are then used to calculate the values X(1,3), X(2,3),…,X(n,3) and so forth.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset