The Runge-Kutta algorithm is *the* magic formula behind
most of the physics simulations
shown on this web site. The Runge-Kutta algorithm lets us solve a differential equation
numerically (that is, approximately); it is known to be very accurate and well-behaved
for a wide range of problems.

Consider the single variable problem

*x*' = *f* (*t*, *x*)

with initial condition
*x*(0) = *x*_{0}
. Suppose that
*x _{n}*
is the value of
the variable at time

*x*_{n+1} = *x _{n}* +

where

*a* = *f* (*t _{n}*,

To run the simulation, we start with
*x*_{0}
and find
*x*_{1}
using the
formula above. Then we plug in
*x*_{1}
to find
*x*_{2}
and so on.

With multiple variables, the Runge-Kutta algorithm looks similar to the previous equations, except that the variables become vectors.

The Runge-Kutta Algorithm is fairly simple, but to describe it precisely we need to
develop some notation. Suppose there are
*m*
variables
*x _{1}*,

*x _{1}*' =

...

Notice there are no derivatives on the right hand side of any of those equations, and there are only first derivatives on the left hand side. These equations can be summarized in vector form as

x' = f (x)

where
x = (*x _{1}*,

x_{n} = (*x*_{1,n}, *x*_{2,n}, ..., *x*_{m,n})

x_{n+1} = (*x*_{1,n+1}, *x*_{2,n+1}, ..., *x*_{m,n+1})

Suppose we have the state of the simulation at time
*t*_{n}
as
x_{n}
. To compute the state a short time
*h*
later and put the
results into
x_{n+1}
, the Runge-Kutta algorithm does the following:

a_{n} = f(x_{n})

b_{n} = f(x_{n} + ^{h}⁄_{2} a_{n})

c_{n} = f(x_{n} + ^{h}⁄_{2} b_{n})

d_{n} = f(x_{n} + *h* c_{n})

x_{n+1} = x_{n} + ^{h}⁄_{6} (a_{n}
+ 2 b_{n} + 2 c_{n} + d_{n})

The new vector
x_{n+1}
gives you the state of the simulation after
the small time
h
has elapsed. To spell out the above in more detail, we can
drop the vector notation and write the Runge-Kutta algorithm like this:

*x*_{j, n} = *f _{j}*(

The above equations are applied for each variable
*j*=(1, ..., *m*)
to get the
full set of variables in the vector
x_{n+1}
.

Most of the simulations shown on this website *do not* have differential
equations that depend explicitly on time. That is, you won't see the variable
*t*
on the right-hand side of the differential equations. One simulation that
*does* depend on time is the
Chaotic Driven Pendulum
because the driving force (which applies the twist to the pendulum)
varies over time according to
cos(*k* *t*)
.

When time appears explicitly in the differential equations we can add a time
variable
*t*
to the state vector
x
. Suppose we assign this role to
the variable
*x _{2}*
. This new variable has the extremely simple differential
equation

*x _{2}*' = 1

That says that the rate of change of the variable
*x _{2}*
is a constant. Since
we are taking derivatives with respect to time we can also write the above equation as

$$x_2' = \frac{d}{d t} x_2 = 1$$

This integrates very easily to give
*x _{2}* =

You may ask: *Why have time as a variable? We already know the value of
t
at each time step!* The Runge-Kutta algorithm works by averaging the
predicted rates at various points in the time interval from

If you want to, you can avoid keeping time as an additional variable. The following
is an equivalent formulation of the Runge-Kutta algorithm where time
*t*
is
passed in as a variable to each function in
f
.

a_{n} = f(*t*, x_{n})

b_{n} = f(*t* + ^{h}⁄_{2}, x_{n} + ^{h}⁄_{2} a_{n})

c_{n} = f(*t* + ^{h}⁄_{2}, x_{n} + ^{h}⁄_{2} b_{n})

d_{n} = f(*t* + *h*, x_{n} + *h* c_{n})

x_{n+1} = x_{n} + ^{h}⁄_{6} (a_{n}
+ 2 b_{n} + 2 c_{n} + d_{n})

This is equivalent to the formulation where time is kept as one of the variables in x . Whether you use this formulation or the earlier (cleaner) one is entirely up to you.

The source code for the Runge-Kutta algorithm is available.

This web page was first published April 2001.