Skip to content

Instantly share code, notes, and snippets.

@msiahaan
Created December 1, 2020 06:51
Show Gist options
  • Save msiahaan/9ebd8a4f8ab3ebd88868db8bcf6dd322 to your computer and use it in GitHub Desktop.
Save msiahaan/9ebd8a4f8ab3ebd88868db8bcf6dd322 to your computer and use it in GitHub Desktop.
Runge Kutta Implementation Example
def runge_kutta(func, x0, y0, x, h=0.5):
# count number of interation using step size or
# step height h
n = int((x-x0)/h)
# iterate for number of iterations
y = y0
for i in range(1, n+1):
# Apply runge-kutta formula to find the value of y
k1 = h * func(x0, y)
k2 = h * func(x0 + 0.5*h, y + 0.5*k1)
k3 = h * func(x0 + 0.5*h, y + 0.5*k2)
k4 = func(x0 + h, y + h*k3)
y = y + h*(k1 + 2*k2 + 2*k3 + k4)/6
x0 = x0 + h
# return y, the result
return y
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment