## module run_kut4 ''' X,Y = integrate(F,x,y,xStop,h). 4th-order Runge-Kutta method for solving the initial value problem {y}' = {F(x,{y})}, where {y} = {y[0],y[1],...y[n-1]}. x,y = initial conditions xStop = terminal value of x h = increment of x used in integration F = user-supplied function that returns the array F(x,y) = {y'[0],y'[1],...,y'[n-1]}. ''' from numarray import array def integrate(F,x,y,xStop,h): def run_kut4(F,x,y,h): K0 = h*F(x,y) K1 = h*F(x + h/2.0, y + K0/2.0) K2 = h*F(x + h/2.0, y + K1/2.0) K3 = h*F(x + h, y + K2) return (K0 + 2.0*K1 + 2.0*K2 + K3)/6.0 X = [] Y = [] X.append(x) Y.append(y) while x < xStop: h = min(h,xStop - x) y = y + run_kut4(F,x,y,h) x = x + h X.append(x) Y.append(y) return array(X),array(Y)