Skip to content

Instantly share code, notes, and snippets.

@CryogenicPlanet
Created February 27, 2020 16:11
Show Gist options
  • Save CryogenicPlanet/04f4f580581488516662795373e5d859 to your computer and use it in GitHub Desktop.
Save CryogenicPlanet/04f4f580581488516662795373e5d859 to your computer and use it in GitHub Desktop.
Single Time Step for Runge Kutta 4th Order
#Runge-Kutta Method Single Time Step
def rk4(func,t,a,b,c,dt):
"""
Peforms a single time step of the Runge-Kutta 4th Order Method.
The below function finds the ki value for [dx,dy,dz] and return the value to move Yn+1
func is an input of functions, for the Lorenz system this is [dx,dy,dz]
Recall Rk4 Equations :
k1 = h*f(xn,yn)
k2 = h*f(xn+h/2,yn+k1/2)
k3 = h*f(xn+h/2,yn+k2/2)
k4 = h*f(xn,yn+k3)
Where f is a function [dx,dy,dz]
Yn+1 = Yn + 1/6*(k1+k2+k3+k4)
"""
k1,k2,k3,k4 = [],[],[],[]
for f in func:
k1.append(dt*f(t,a,b,c))
for f in func:
k2.append(dt*f(t+dt/2,a+k1[0]/2,b+k1[1]/2,c+k1[2]/2))
for f in func:
k3.append( dt*f(t+dt/2,a+k2[0]/2,b+k2[1]/2,c+k2[2]/2))
for f in func:
k4.append( dt*f(t+dt/2,a+k3[0],b+k3[1],c+k3[1]))
k1,k2,k3,k4 = np.array(k1),np.array(k2),np.array(k3),np.array(k4)
return (1/6)*(k1+k2+k3+k4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment