#!/usr/bin/python3 # 4th order Runge-Kutta method import numpy as np def rk4(f, a, b, x0, N): h = (b - a) / N x = x0 tpoints = np.linspace(a, b, N+1) xpoints = [] for t in tpoints: xpoints += [x] k1 = h * f(x, t) k2 = h * f(x + k1/2, t + h/2) k3 = h * f(x + k2/2, t + h/2) k4 = h * f(x + k3, t + h) x = x + (k1 + 2*k2 + 2*k3 + k4)/6 return tpoints, np.array(xpoints)