From the integral to the algorithm

Most science problems are described by differential equations, hence the numerical resolution of ordinary and partial differential equations is central to science and to most simulation methods. When analytic computation is not feasible, numerical resolution can help.

The integral formulation

Differential equations come in a variety of forms in science: in Physics and Engineering, and in general in the simulation techniques that will be discussed in these notes, most of them are of the kind

\begin{equation} \frac{dx^2(t)}{dt^2} + a(t)\frac{dx(t)}{dt} + b(t)x(t) + c(t) = 0 \end{equation}

where $t$ is called the independent variable, and $x(t)$ is the dependent variable. A typical example is the equation of the harmonic oscillator, which is usually written as

\begin{equation*} \frac{dx^2(t)}{dt^2} + \omega^2 x(t) = 0 \end{equation*}

The equation described by eq. (1) is of second order, however it is always possible to reduce a differential equation of order $N$ (however big) to a set of $N$ differential equations of first order of the kind:

\begin{equation} \frac{dx^i(t)}{dt} = f_i(t,x^1,\ldots,x^N) \end{equation}

Thus, even in the case where we are facing a differential equation of higher order $N$ , the problem of finding its solution can be easily transformed into the problem of finding the solution of a set of $N$ differential equations of first order. For instance we can transform eq. (1) in the following set of first order differential equations:

\begin{equation*} \left\{ \begin{array}{l c l} \displaystyle\frac{dx(t)}{dt} & = & \displaystyle y(t)\\ & & \\ \displaystyle\frac{dy(t)}{dt} & = & \displaystyle - a(t)y(t) - b(t)x(t) - c(t) \end{array} \right. \end{equation*}

Our problem has been modified into that of finding the solution of a set of first order differential equations.

In order to understand how we can numerically integrate a set of first order differential equations, we will focus for the moment on the simplest case $N=1$ , when we only have to solve one first order differential equation in one variable:

\begin{equation} \frac{dx(t)}{dt} = f(t,x(t)) \end{equation}

The solution to this equation is formally given by:

\begin{equation} x(t) = x(t_0) + \int_{t_0}^t f(\tau, x(\tau)) d\tau \end{equation}

We note immediately that in order to get the solution we need the initial condition $x(t_0)$ , that is the value of the dependent variable at the initial value of the integration interval. Once we know this, we only need to perform the integration shown to get the solution $x(t)$ . The problem however is that we do not know how to perform the integration as the integrand depends on the unknown $x(t)$ .

Before dwelling into the various algorithms, a remark on rigour is in order: all the methods we will illustrate make use of the knowledge of the derivative of $f(t;x)$ at different times through Taylor expansions, hence a minimal requirement is derivability up to some order $p$ which depends on the method. On the other hand, convergence of the numerical solution to the true solution requires a condition on $f$ : an intermediate approach is followed by Iserles [1] by requiring the following Lipschitz condition, which among other things ensures that eq. (3) has a unique solution:

\begin{equation} ||\vec{f}(t,\vec{x})-\vec{f}(t,\vec{y})|| \leq \lambda ||\vec{x}-\vec{y}||\qquad\forall\,\vec{x},\vec{y} \in \mathbb{R}^d,\quad t\ge t_0 \end{equation}

Here $\lambda$ is a positive real constant which is independent of the choice of $\vec{x}$ and $\vec{y}$ . This condition ensures that $\vec{f}(t,\vec{x})$ is not too sensitive to small changes in $\vec{x}$ , and ensures the convergence of many of the methods we will encounter. We will not dwell on these analytical aspects here: a complete treatment can be found in Iserles [1].

The forward Euler's method

The easiest way to approximate the integral is to take the value of the integrand in one of the extremes, for instance in $t=t_0$ , and extend it to the whole integration interval, which leads to an approximation with an error that depends on the size of the integration interval. We can express this formally by performing a Taylor expansion in $t=t_0$ ; using eq. (3) we get:

\begin{equation*} x(t) = x(t_0) + (t-t_0)\,f(t_0; x(t_0)) + O\left( (t-t_0)^2 \right) \end{equation*}

Unless we are integrating for very short time intervals, the error is extremely high and our approximation is in practice meaningless. However, when the integration interval is sufficiently small, the error also becomes comparatively small and the method can be successfully used. Following this idea, we can easily integrate equation (4) if we use this approximation on small time intervals. Hence, we proceed as follows: the integration interval $[t_0,t]$ is divided into sub-intervals of duration $h \ll t-t_0$ and an approximated integration of the differential equation in each sub-interval is provided. The integration on a single sub-interval becomes thus:

\begin{equation} \begin{array}{lcl} x(t+h) & = & \displaystyle x(t) + h\,\frac{dx(t)}{dt} + O(h^2)\\[0.3ex] & = & \displaystyle x(t) + h\,f(t,x(t)) + O(h^2) \end{array} \end{equation}

Starting from the lower end of the integration interval, we build a sequence of values $\{x_n\}$ corresponding to approximations of the true solution $x(t)$ at times $t_n = t_0 + nh$ . The initial condition hence corresponds to the value $x_0 = x(t_0)$ . For the first step, we get an approximated value $x_1$ at time $t_1=t_0+h$ using the initial condition $x_0$ . Once obtained, we use this new value as the initial condition to get the value $x_2$ at time $t_2=t_1+h$ . By carrying out this exercise we recover the sequence of values $x_0, \ldots, x_M$ . This sequence represents our approximation to the true solution, and its temporal resolution is controlled by the timestep $h$ . The generic approximated value $x_{n+1}$ can be thus written as:

\begin{equation*} x_{n+1} = x_n + h\,f(t_n,x_n) \end{equation*}

The error made on $x_{n+1}$ is of order $O(h^2)$ : if $h$ is small enough this error will also be small. Indeed the Euler's method is convergent [1] and the error tends to zero in the limit $h \rightarrow 0$ . This tells us that we can always get a solution with some desired accuracy provided we decrease the timestep sufficiently.

A different way to look at this method is to obtain it from the integral solution eq. (4) with the integration taken as $[t,t+h]$ :

\begin{equation} x(t+h) = x(t) + \int_{t}^{t+h} f(\tau, x(\tau)) d\tau \end{equation}

as shown in figure [1] we can think to approximate the integral by taking the value of the integrand at $t$ and extending it to the whole interval:

\begin{equation*} \int_{t}^{t+h} f(\tau, x(\tau)) d\tau \simeq h\, f(t, x(t)) \end{equation*}

This approximation is of course quite rough, and the method is the simplest we can use to get the solution of the differential equation. This however does not mean that we cannot use it! Indeed when the dynamics of the system we need to simulate is slow, it can be used with success.

Different approximation for the integral: (a) forward Euler, (b) backward Euler,
Different approximation for the integral: (a) forward Euler, (b) backward Euler, (c) Trapezoidal.

The backward Euler's method

Figure [1] shows that we are not forced to approximate the integral solution (4) in the way discussed above. We could, for instance, decide to approximate the integral by taking the value at the other extreme, at time $t+h$ and extending it to the whole integral. This leads us to the backward Euler's method:

\begin{equation*} x(t+h) = x(t) + \int_{t}^{t+h} f(\tau, x(\tau)) d\tau \simeq x(t) + h\, f(t+h, x(t+h)) \end{equation*}

We can obtain the same result with a Taylor expansion as follows:

\begin{equation*} x(t'-h) = x(t') - h\,\frac{dx(t')}{dt'} + O(h^2) \end{equation*}

we now use the change of variable $t'\rightarrow t+h$ and we get:

\begin{equation*} x(t) = x(t+h) - h\,\frac{dx(t+h)}{dt} + O(h^2) \end{equation*}

by using the fact that $dt'=dt$ . Reordering we finally get:

\begin{equation*} x(t+h) = x(t) + h\,f(t+h,x(t+h)) + O(h^2) \end{equation*}

from which we deduce the backward Euler's method:

\begin{equation*} x_{n+1} = x_n + h\,f(t_{n+1},x_{n+1}) \end{equation*}

This is very similar to the forward Euler's method, the difference being that now $f$ is evaluated at $x_{n+1}$ . Methods for which the right hand side depends on $x_{n+1}$ are called implicit as the value of $x_{n+1}$ is not explicitly stated in the equation. We have of course a problem because the value $x_{n+1}$ is unknown. However, if $f$ is linear in $x(t)$ we can easily solve the equation for $x_{n+1}$: we show an example for the case of the harmonic oscillator in the following articles. The main advantage of using an implicit method over an explicit method is the fact that they have a greater stability. In general $f$ will not be a linear function of $x(t)$, therefore solving an implicit method can be problematic. There are techniques which can be used in this case, for instance predictor-corrector algorithms: these will be discussed in a future article.

The trapezoidal method

Instead of the rough approximation used in the Euler's methods, the integral in eq. (4) can be better approximated by approximating the area of the function to a trapezoid:

\begin{equation*} x(t+h) = x(t) + \int_{t}^{t+h} f(\tau, x(\tau)) d\tau \simeq x(t) + \frac{h}{2}\left[ f(t,x(t)) + f(t+h, x(t+h)) \right] \end{equation*}

If this looks like a better approximation, what is the error we make by using it? We can understand it if we obtain the approximation using Taylor expansions:

\begin{equation} x(t+h) = x(t) + h\,\frac{dx(t)}{dt} + \frac{h^2}{2}\,\frac{dx^2(t)}{dt^2} + O(h^3) \end{equation}

and

\begin{equation*} x(t'-h) = x(t') - h\,\frac{dx(t')}{dt'} + \frac{h^2}{2}\,\frac{dx^2(t')}{dt'^2} + O(h^3) \end{equation*}

Using the change of variables $t'=t+h$ , the last equation becomes:

\begin{equation*} x(t) = x(t+h) - h\,\frac{dx(t+h)}{dt} + \frac{h^2}{2}\,\frac{dx^2(t+h)}{dt^2} + O(h^3) \end{equation*}

We can approximate the second derivative in $t+h$ with the second derivative in $t$ . In fact, if we expand the second derivative around $t$ :

\begin{equation*} \frac{dx^2(t+h)}{dt^2} = \frac{dx^2(t)}{dt^2} + h\,\frac{dx^3(t)}{dt^3} + O(h^2) \end{equation*}

so that when multiplied by $h^2$ , the second term becomes of order $h^3$ and hence is incorporated into the term $O(h^3)$ . Hence we get:

\begin{equation} x(t) = x(t+h) - h\,\frac{dx(t+h)}{dt} + \frac{h^2}{2}\,\frac{dx^2(t)}{dt^2} + O(h^3) \end{equation}

Now we can add up eq. (8) and the rearranged eq. (9) and we get

\begin{equation*} x(t+h) = x(t) + \frac{h}{2}\,\left[ f(t,x(t)) + f(t+h,x(t+h)) \right] + O(h^3) \end{equation*}

The result is another implicit method, the difference being that now the error is of order $O(h^3)$ , much smaller than the simple Euler method.

\begin{equation} x_{n+1} = x_n + \frac{h}{2}\,\left[ f(t_n,x_n) + f(t_{n+1},x_{n+1}) \right] \end{equation}

Once again the right hand side of eq. (10) is implicitly defined, so if it is linear in $x_{n}$ it can be easily solved algebraically, otherwise we need to use predictor corrector algorithms.

The order of a method

The numerous algorithms that exist to integrate differential equations are all different ways in which we can perform this approximated integration in the time interval $h$ . The overall error we get in the solution at time $t$ is the sum of the error of the individual integration steps. The error on the single integration step will depend on the specific integration method chosen. In general a specific integration method is of order $p$ if the error of the integration step is of order $p+1$ . This is because the overall error of the solution at time $t_M$ , corresponding to $M$ integration steps, has order $M\,O(h^{p+1})=O(h^{p+1})(t-t_0)/h = O(h^{p})$ .

Of the methods presented in this article, the forward and backward Euler's methods have an integration error of order $O(h^2)$ and are therefore of order 1, while the trapezoidal method, whose integration error is of order $O(h^3)$ , is of order 2.

References

[1] Arieh Iserles. A First Course in the Numerical Analysis of Differential Equations. Cambridge University Press (1996).