Created
December 27, 2016 16:58
-
-
Save NaxAlpha/7b5394b79437c4d91e2cf2ba3a1b4189 to your computer and use it in GitHub Desktop.
Runge–Kutta method
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 4th order implementation | |
def iterate(tn, yn, h, f): | |
k1 = h * f(tn, yn) | |
k2 = h * f(tn + h/2, yn + k1/2) | |
k3 = h * f(tn + h/2, yn + k2/2) | |
k4 = h * f(tn + h, yn + k3) | |
yx = yn + (1/6)*(k1 + 2*k2 + 2*k3 + k4) | |
return yx | |
def evaluate(t0, y0, h, f, n): | |
tn = t0 | |
yn = y0 | |
for i in range(0, n+1): | |
print(str(tn),'=>',str(yn)) | |
yn = iterate(tn, yn, h, f) | |
tn = tn + h | |
def fx(t, y): | |
return (t+y)/(y-t) | |
evaluate(0, 1, 0.1, fx, 4) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment