Last active
December 2, 2018 23:53
-
-
Save mirianfsilva/375119592e67dc03cf7d12ed20055bcf to your computer and use it in GitHub Desktop.
Method to find solution of ordinary differential equations
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
#Runge Kutta 4th | |
import math, sys | |
import numpy as np | |
def RungeKutta4(f, y0, T, n): | |
t = np.zeros(n+1) | |
y = np.zeros(n+1) | |
y[0] = y0 | |
t[0] = 0 | |
dt = T/float(n) | |
for k in range(n): | |
t[k+1] = t[k] + dt | |
K1 = dt * f(y[k], t[k]) | |
K2 = dt * f(y[k] + 0.5*K1, t[k] + 0.5*dt) | |
K3 = dt * f(y[k] + 0.5*K2, t[k] + 0.5*dt) | |
K4 = dt * f(y[k] + K3, t[k] + dt) | |
y[k+1] = y[k] + (1/6.0)*(K1 + 2*K2 + 2*K3 + K4) | |
return y, t | |
# Problem: y'=y | |
def f(t, y): | |
return y #define f |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment