# [Numpy-discussion] Forth-Order Runge-Kutta

Thu Dec 13 13:12:25 EST 2001

```> Date: Thu, 13 Dec 2001 12:39:32 +0100
> From: Nils Wagner <nwagner at mecha.uni-stuttgart.de>
> Subject: [Numpy-discussion] Forth-Order Runge-Kutta
>
> I am looking for an implementation of the fourth-order Runge-Kutta
> method in Numpy.

Here is one I wrote (based on a similar I had seen in lorentz.py in the
PyOpenGL demos, I believe), that I used for chaotic dynamics
visualizations.

# A simple, non-stepsize adaptive fourth order Runge-Kutta integration
# estimator method.
def fourth_order_runge_kutta(self, xyz, dt):
derivative = self.differentiator
hdt = 0.5 * dt

xyz = asarray(xyz) # Force tuple or list to an array

k1 = array(derivative(xyz))
k2 = array(derivative(xyz + k1 * hdt))
k3 = array(derivative(xyz + k2 * hdt))
k4 = array(derivative(xyz + k3 * dt))

new_xyz = xyz + (k1 + k4) * (dt / 6.0) + (k2 + k3) * (dt / 3.0)
return new_xyz

where self.differentiator is a function that takes an x,y,z coordinate
tuple, and returns the deriviative as an x,y,z coordinate tuple.  I
wrote this when I was much more naive about Runge-Kutta and Python
Numeric, so don't use it without some looking over.  It is at least a
good starting point.

--