Class 6.1

Integration algorithms: Euler, Runge-Kutta, implicit vs. explicit

In most cases the differential equations that we create for a model are too complex to solve and analyze analytically. There are numerical methods to use to find the solutions. However we should remember that these solutions will be approximate, and the larger the time step we use, the more error we generate.

Consider the single variable problem

x' = f(t,x)

with initial condition x(0)=x0. Suppose that xn is the value of the variable at time tn.

The Euler algorithm is the most straightforward way to solve a difference equation that is used to approximate the differential equation.

xn+1 = xn + f(tn,xn) dt

The major source of error is that we assume the right-hand-side function f to be constant over the time step dt. Obviously, the smaller the dt, the less the difference between the continuos function f(t,p) and its discrete approximation.

The Runge-Kutta algorithm interpolates the function between tn and tn+1 in a tricky way. The Runge-Kutta formula uses a weighted average of approximated values of f(t,x) at several times within the interval (tn,tn+dt). The formula is given by

xn+1 = xn + (dt/6)(k1 + 2k2 + 2k3 + k4)

where

k1 = f(tn, xn)
k2 = f(tn + dt/2, xn + (dt/2)k1)
k3 = f(tn + dt/2, xn + (dt/2)k2)
k4 = f(tn + dt, xn + dt k3)

To run the simulation, we simply start with x0 and find x1 using the formula above. Then we plug in x1 to find x2 and so on.

Note that even though we use 6 intermediate values when calculating xn+1, we calculate the function f only 4 times. Calculating the right-hand-side function is what usually takes most of the CPU time.

Two-variable Runge-Kutta Algorithm

The Runge-Kutta algorithm can be easily extended to a set of first order differential equations.  You wind up with essentially the same formulas shown above, but all the variables (except for time) are vectors.

To give an idea of how it works, here is an example where we expand the vector notation.  That is, instead of using the highly compact vector notation, we write out all the components of the vector.

Suppose there are only two variables, x, y and two differential equations

x' = f(t,x,y)
y' = g(t,x,y)

Again we let xn be the value of x at time tn, and similarly for yn.  Then the formulas for the Runge-Kutta algorithm are

xn+1 = xn + (h/6)(k1 + 2k2 + 2k3 + k4)
yn+1 = yn + (h/6)(j1 + 2j2 + 2j3 + j4)

where

k1 = f(tn, xn, yn)
j1 = g(tn, xn, yn)
k2 = f(tn + h/2, xn + (h/2)k1, yn + (h/2)j1)
j2 = g(tn + h/2, xn + (h/2)k1, yn + (h/2)j1)
k3 = f(tn + h/2, xn + (h/2)k2, yn + (h/2)j2)
j3 = g(tn + h/2, xn + (h/2)k2, yn + (h/2)j2)
k4 = f(tn + h, xn + h k3, yn + h j3)
j4 = g(tn + h, xn + h k3, yn + h j3)

Given starting values x0,y0 we can plug them into the formula to find x1,y1. Then we can plug in x1,y1 to find x2,y2 and so on.

Multi-variable Runge-Kutta Algorithm

Suppose we have more than two variables which are x, y, z,.... We then need to convert the differential equations into a system of first order equations, as follows:

x' = f(t, x, y, z, ...)
y' = g(t, x, y, z, ...)
z' = h(t, x, y, z, ...)
...

You will have as many equations as there are variables. The algorithm is similar to that shown for the one-variable version. The details can be confusing and easy to get wrong, so I recommend looking at source code. A simplified version of the source code is available here.