Docsity

Ordinary Differential Equations - Computational Physics | PHYS 305, Exams of Physics

Toggle sidebar
bg1
Chapter 7
Ordinary Differential
Equations
In this chapter we will
•explore the difference between explicit and implicit numerical methods
•study the stability of numerical integrators
•learn how to solve algebraic equations using
–Euler’s method
–Runge-Kutta methods
An equation that involves derivatives of the dependent variable is called a differ-
ential equation. Most problems in the physical sciences involve the solution of
differential equations. In fact, the calculus of differential equations was invented by
Newton and Leibnitz in order to describe the evolution of physical systems.
If a differential equation involves only one independent variable and, there-
fore, contains only full derivatives, it is called an ordinary differential equation
(ODE) . If it involves more than one independent variables and, therefore, contains
partial derivatives, it is a called a partial differential equation (PDE). The or-
der of a differential equation is the order of the highest derivative that appears in
the equation.
There are many well known differential equations in Physics. For example,
Newton’s second law d2~r
dt2=1
m~
F(~r),(7.1)
which connects the acceleration d2~r/dt2of a particle of mass mand the force
~
F(~r), is a second-order, ordinary differential equation. On the other hand, the
one-dimensional Schr¨odinger’s equation
−i¯h∂Ψ(x, t)
∂t =−¯h2
2m
∂2Ψ(x, t)
∂χ2+V(x)Ψ(x, t),(7.2)
which describes the time evolution of a wavefunction Ψ(x, t) of a particle in a
potential V(x), is a second-order partial differential equation. In this chapter, we
will discuss methods for solving ordinary differential equations.
7-1
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download Ordinary Differential Equations - Computational Physics | PHYS 305 and more Exams Physics in PDF only on Docsity!

Chapter 7

Ordinary Differential

Equations

In this chapter we will

  • explore the difference between explicit and implicit numerical methods
  • study the stability of numerical integrators
  • learn how to solve algebraic equations using
    • Euler’s method
    • Runge-Kutta methods

An equation that involves derivatives of the dependent variable is called a differ- ential equation. Most problems in the physical sciences involve the solution of differential equations. In fact, the calculus of differential equations was invented by Newton and Leibnitz in order to describe the evolution of physical systems.

If a differential equation involves only one independent variable and, there- fore, contains only full derivatives, it is called an ordinary differential equation (ODE). If it involves more than one independent variables and, therefore, contains partial derivatives, it is a called a partial differential equation (PDE). The or- der of a differential equation is the order of the highest derivative that appears in the equation.

There are many well known differential equations in Physics. For example, Newton’s second law d^2 ~r dt^2

m

F~ (~r) , (7.1)

which connects the acceleration d^2 ~r/dt^2 of a particle of mass m and the force F^ ~ (~r), is a second-order, ordinary differential equation. On the other hand, the one-dimensional Schr¨odinger’s equation

−i¯h

∂Ψ(x, t) ∂t

¯h^2 2 m

∂^2 Ψ(x, t) ∂χ^2

  • V (x)Ψ(x, t) , (7.2)

which describes the time evolution of a wavefunction Ψ(x, t) of a particle in a potential V (x), is a second-order partial differential equation. In this chapter, we will discuss methods for solving ordinary differential equations.

7-

7-2 CHAPTER 7. ORDINARY DIFFERENTIAL EQUATIONS

A differential equation of order N is typically equivalent to a system of N equa- tions of first order. For example, we can rewrite Newton’s second law (equation [7.1]) as the system of two first-order equations

d~r dt

= ~v d~v dt

m

F~ (~r) , (7.3)

by introducing the particle velocity ~v as an additional dependent variable. Because of such decompositions, we will develop here algorithms that solve a system of N , first-order, ordinary differential equations.

The solution to a differential equation depends not only on the equation itself, but on the boundary conditions as well. In fact, it is the boundary conditions that pick one, many, or even none of all the possible solutions to a differential equation. Furthermore, the number, location, and character of the boundary conditions affect considerably the choice of numerical method used. In a typical problem, for each differential equation of order N , there are N boundary conditions required, each of which has to be of order at most N − 1. In a so-called initial value problem, all boundary conditions are given at the same place in the domain of solution, i.e., for the same value of the independent parameter. In a boundary-value problem, different boundary conditions are given at different places in the domain of solution.

As an example, Newton’s second law (equation [7.1]) is a second-order differen- tial equation. It, therefore, requires two boundary conditions. Each of the boundary conditions can be at most of order one, i.e., it can be either a position or a velocity at a given time. In an initial-value problem, for example, the position and velocity of a particle are given at time t 0 and then the differential equation is solved forward in time. In a boundary value problem, the initial and final position of a particle may be given. In physical situations, initial value problems almost always have unique solutions. On the other hand, boundary value problems may have none, one, or even many solutions. In this chapter, we will discuss only methods for solving a system of first order, ordinary differential equations for initial-value problems.

7.1 Explicit and Implicit Methods

Solving ordinary differential equations is conceptually identical to integrating a function. In fact, for an equation of the form

df dt

= g(t) , (7.4)

in which the right-hand side depends only on the independent variable and not on the function itself, the solution is simply

f (t) = f (t 0 ) +

∫ (^) t

t 0

g(t)dt , (7.5)

where f (t 0 ) is the boundary condition. In general, however, the right-hand side of a differential equation will depend on both the dependent and the independent variables, i.e., df dt

= g[t, f (t)]. (7.6)

In this general case, separation of variables is impossible and ordinary integration methods are not useful in solving differential equations.

7-4 CHAPTER 7. ORDINARY DIFFERENTIAL EQUATIONS

very small values of the timestep, the dependent variable does not change signifi- cantly between timesteps and hence round-off errors affect the numerical solution. Indeed, for every case there is an optimal timestep at which the fractional error between the numerical and the analytic solution is minimized. In this section, we study the accuracy of differential equation solvers and its dependence on the various parameters of the problem.

For large values of the timestep, the systematic error of a numerical scheme always dominates. If we use a scheme of n-th order, then the error we introduce at every timestep between the numerical approximation (e.g., eq. [7.8] or eq. [7.10]), is dominated by the term of order n + 1 of the Taylor expansion around the initial value, i.e.,

f − fnum = 1 (n + 1)!

dn+1f dtn+

t

(∆t)n+1^ + ... , (7.11)

where we used fnum and f to denote the numerical and true solutions, respectively. The compound error after evolving the equation for a time t is approximately equal to the product of the error per timestep (eq. [7.11]) times the total number of timesteps taken N = t/∆t. As a result, after many timesteps, the compound error is

f − fnum ≃

(n + 1)!

dn+1f dtn+

t

(∆t)nt , (7.12)

Clearly, the compound error depends on the problem under study (through the derivative term), on the order of the numerical scheme employed, on the size of the timestep taken, and on the total time over which the differential equation is evolved.

For small values of the timestep, it is the round-off error that dominates the overall accuracy of the numerical solution. If we denote by ǫ the accuracy with which a real number is stored in the computer, then the round-off error after each timestep is about ǫ/2. The compound error after taking N = t/∆t timesteps is approximately proportional to

N. Therefore, for small values of the timestep, the compound error is

f − fnum ≃

ǫ 2

t ∆t

At this limit, the error depends on the machine accuracy, ǫ, the timestep, and the total time over which the differential equation is evolved.

The systematic error introduced by the numerical approximation of the algo- rithm decreases with decreasing size of the timestep (equation 7.24). On the other hand, the effect of the round-off error increases with decreasing size of the timestep (equation 7.25). As a result, there is an optimal size of the timestep at which the combined error is minimal. Equating relations (7.24) and (7.25) we obtain

(∆t)opt ≃

[

ǫ(n + 1)! 2 t^1 /^2

dn+1f dtn+

t

)− 1 ]^1 /(n+1/2) (7.14)

for the optimal size of the timestep. If we have expressed the differential equation in a dimensionless form in terms of the natural units of the problem, then both the function f and its derivatives will be of order unity. Therefore, an approximate value for the optimal timestep after one characteristic timescale in the problem is

(∆t)opt ≃

[

ǫ(n + 1)! 2

] 1 /(n+1/2) , (7.15)

7.2. ACCURACY OF ODE SOLVERS 7-

Figure 7.1: The optimal timestep (left) and the lowest possible fractional error (right) of a numerical solution of a differential equation at t = 1, as a function of the order of the scheme used and the machine accuracy. In this plot we have assumed that the differential equation has been expressed in terms of the natural units of the problem.

which depends strongly on the machine accuracy, ǫ, and the order, n, of the numer- ical scheme used (see Fig. 7.1).

This result might appear counter intuitive in two ways. First, the optimal timestep in order for round-off errors to be avoided is significantly larger than the machine accuracy. For a first-order scheme it is ∼ ǫ^2 /^3 and it asymptotes towards unity for higher orders. This is, of course, a consequence of the fact that the round- off error competes with the systematic error of the numerical scheme and the latter scales as (∆t)n+1. Second, and more important, is the fact that the optimal timestep increases rapidly with the order of the numerical scheme. Indeed, we can achieve a more accurate solution to a problem by increasing the order of the numerical scheme, if and only if we also increase the size of the timestep. If we were to always use the optimal timestep for a first-order scheme to solve numerically a differential equation, the accuracy of the solution would always be the same, independent of the order of the numerical scheme we employed.

We can, in fact, estimate the lowest possible error in our numerical solution, by evaluating the sum of equations (7.24) and (7.25) at the optimal timestep (7.15). The result for the fractional error is (see also the right panel of Fig. 7.1)

f − fnum f

( (^) ǫ 2

)n/(n+1) (^1 (n + 1)!f

dn+1f dtn+

t

) 1 /(n+1) t

( (^) ǫ 2

)n/(n+1) [^1 (n + 1)!

] 1 /(n+1) t (7.16)

It is clear from this last expression that the error we introduce to a numerical solution of an ordinary differential equation depends on a number of factors. First, it depends on the particular equation being solved; the accuracy is always better for solutions that depend very weakly on the independent variable. Second, it depends on the order of the method used. In general, the higher the order of the scheme, the more accurate the result. This is not always true, however, and especially if we do not use the optimal timestep for every scheme. Third, the accuracy of a numerical solution depends on the precision of the floating point arithmetic used, since the latter sets the roundoff error introduced at every timestep. Finally, the accuracy of the final result depends on the total number of timesteps taken.

We will illustrate the effect of all these factors on the accuracy of a numerical

7.2. ACCURACY OF ODE SOLVERS 7-

Figure 7.2: (Left) The analytic solution (red line) and the numerical solution (blue circles) eval- uated every tenth timestep, for the carbon dating problem discussed in §7.2. For this calculation, the timestep is set to ∆t = 0.01. (Right) The fractional error between the analytic and numerical solution for the same problem as a function of the timestep ∆t, evaluated at two different times during the integration.

earlier. We will not discuss in detail the implementation of either method here, because they are both surpassed in quality by the superior Runge-Kutta methods that we will study later in this chapter.

Accuracy of the Numerical Solutions.—The left panel of figure 7.2 compares the analytic (solid line) to the numerical (open circles) solution of the differential equation obtained using Euler’s method with a timestep ∆τ = 0.01. It appears, at least visually, that the numerical solution agrees very well with the analytic solution. This is demonstrated quantitatively in the right panel of figure 7.2 that shows the fractional error at time τ = 1 (blue line) and at time τ = 3 (red line) for the same calculation, as a function of the timestep ∆t.

For large values of the timestep, the systematic error decreases linearly with decreasing timestep but increases linearly with the total time over which the differ- ential equation was evolved. This is in agreement with our previous estimate (7.24), which applied to the current problem gives

f − fnum f

(∆τ )τ , for large ∆t. (7.24)

On the other hand, for small values of the timestep, the error increases again with decreasing timestep as well as with increasing total evolution time. Indeed, accord- ing to equation (7.25) the fractional round-off error is approximately equal to

f − fnum f

ǫ 2

( (^) τ ∆τ

eτ^ , for small ∆t. (7.25)

The optimal timestep is (∆τ )opt ≃ 10 −^4 and the lowest possible fractional error in the solution is ≃ 10 −^4 t.

We can improve the accuracy of the solution either by increasing the order of the numerical scheme or by increasing the accuracy of the floating point arithmetic used. In either case, however, we achieve an improved accuracy if and only if we also change the size of the timestep appropriately. This is shown in Figure 7.3. In the left panel, the accuracy of the first-order Eulerian scheme is compared to that of the second-order implicit scheme. In the right panel, the accuracy of the numerical solution is shown, when single- and double-precision floating point arithmetic is used. Increasing the order of the numerical scheme requires an increase in the size

7-8 CHAPTER 7. ORDINARY DIFFERENTIAL EQUATIONS

Figure 7.3: The dependence on the size of the timestep of the fractional error after one e-folding time (i.e., at τ = 1) for the problem discussed in §7.2. The left panel compares a first-order Euler scheme with a second-order implicit scheme. The right panel compares two first-order Euler schemes in which floating point arithmetic of single and double precission was used. In both cases, an improvement in the accuracy of the numerical solution is achieved if and only if the size of the timestep is also changed accordingly.

of the timestep, while increasing the accuracy of floating point arithmetic requires a decrease in the size of the timestep.

7.3 Stability of ODE solvers

In the previous section we showed how considerations of the accuracy of a numerical scheme lead to a minimum value of the timestep, below which round-off errors dominate the systematic uncertainty of the method. In this section, we will discuss how the stability of an explicit numerical scheme sets a maximum value of the timestep, above which the method diverges and leads to unphysical results.

We will use again, as an example, the numerical solution to the differential equation for the carbon dating problem (eq. 7.21)

df dτ

= −f. (7.26)

Using Euler’s scheme (eq. [7.8]) to evolve this equation by ∆τ away from some initial condition f 0 , we obtain

f (∆τ ) = f 0 +

df dτ

τ =

∆τ (7.27)

= (1 − ∆τ )f 0. (7.28)

After having taken N timesteps, the numerical solution becomes

f (N · ∆τ ) = (1 − ∆τ )N^ f 0. (7.29)

Clearly, the numerical solution has the correct asymptotic behavior

lim N →∞

f (N · ∆τ ) = 0 , (7.30)

if and only if 1 − δτ > −1, i.e., if and only if

∆τ < 2. (7.31)