Skip to content

Instantly share code, notes, and snippets.

@mitchute
Created January 3, 2019 22:20
Show Gist options
  • Save mitchute/db4a9dbc0f05d12ed2c7663fdfdfd311 to your computer and use it in GitHub Desktop.
Save mitchute/db4a9dbc0f05d12ed2c7663fdfdfd311 to your computer and use it in GitHub Desktop.
def runge_kutta_fourth(rhs, h, x, y):
"""
Solves one step using a fourth-order Runge-Kutta method.
Moin, Parviz. 2010. Fundamentals of Engineering Numerical Analysis. 2nd ed.
Cambridge University Press. New York, New York.
:param rhs: "Right-hand Side" of the equation(s). Everything but the derivative. (e.g dy/dx = f(x))
:param h: step size
:param x: step dimension
:param y: output dimension
:return:
"""
k_1 = rhs(x, y)
k_2 = rhs(x + h / 2.0, y + k_1 / 2.0)
k_3 = rhs(x + h / 2.0, y + k_2 / 2.0)
k_4 = rhs(x + h, y + k_3)
return y + (k_1 + 2 * (k_2 + k_3) + k_4) / 6.0 * h
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment