Skip to content

Instantly share code, notes, and snippets.

@sshine
Created June 12, 2014 06:20
Show Gist options
  • Save sshine/7271208e54177daa3e0e to your computer and use it in GitHub Desktop.
Save sshine/7271208e54177daa3e0e to your computer and use it in GitHub Desktop.
from __future__ import division
## Equations:
# x(t+h) = x(t) + 1/2 (F1 + F2)
# F1 = h*f(t,x)
# F2 = h*f(t+h, x+h*f(t,x))
def rungekutta(f, a, b, x0, iterations):
h = (b - a) / iterations
x = x0
result = [x]
for i in range(iterations):
s = a + h*i
F1 = h*f(s, x)
F2 = h*f(s+h, x+F1)
x = x + .5*(F1 + F2)
result.append(x)
return result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment