Integration algorithms: Euler, Runge-Kutta, implicit vs. explicitIn 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 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 xn+1 = xn + (dt/6)(k1 + 2k2 + 2k3 + k4) where
k1 = f(tn, xn)
To run the simulation, we simply start with 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 AlgorithmThe 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' = f(t,x,y)
Again we let
xn+1 = xn + (h/6)(k1 + 2k2 + 2k3 + k4) where
k1 = f(tn, xn, yn)
Given starting values Multi-variable Runge-Kutta Algorithm
Suppose we have more than two variables which are
x' = f(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. |