# Python function for the Runge-Kutta 4th Order Method

These days, I tried to make a numerical anlysis function for the 4th-order Runge-Kutta method. I could use ODE function in the Scipy or other public libraries, but I wanted to make it and custumize it.

Here is the python code. I refered to someone else's code and textbook.

import numpy as np

def RK4thOrder(func, yinit, x_range, h):
n = int((x_range[-1] - x_range)/h)

x, y = x_range, yinit

del x_range

# Containers for solutions
xsol, ysol = [x,], [y,]

i = 0
while i < n:
k1 = func(x, y)

yp2 = y + k1 * h / 2

k2 = func(x+h/2, yp2)

yp3 = y + k2 * h / 2

k3 = func(x+h/2, yp3)

yp4 = y + k3 * h

k4 = func(x+h, yp4)

y = y + (((k1 + k4) + (2 * (k2 + k3))) / 6 * h)
del k1, k2, k3, k4, yp2, yp3, yp4
x = x + h
xsol.append(x)
ysol.append([])
i = i + 1
ysol[i] = y
ysol = np.array(ysol)
return xsol, ysol

input value:

• func: function of differential equation.
• yinit: inital y value at x. yinit can be 1-dimensional numpy array.
• x_range: [x_min, x_max]
• h: interval for an iteration step.

output value:

• xsol: list of x
• ysol: numpy array of y
Comment 1개
• 니모
2019.12.13

유용한 정보 감사합니다

### board

• 14
Others 2023.03.30 조회 55
• 13
Pharmacokinetics 2023.03.24 조회 76
• 12
Pharmacokinetics 2023.03.23 조회 60
• 11
Pharmacokinetics 2023.03.23 조회 46
• 10
2023.03.22 조회 58
• 9
2022.11.22 조회 77
• 8
Others 2020.09.18 조회 879
• 7
Others 2020.05.26 조회 806
• 6
Science and Technology 2018.10.24 조회 2508
• 5
Yummy 2018.08.15 조회 1025
• 4
Others 2018.05.13 조회 1064
• 3
Others 2017.06.27 조회 552
• 2
Others 2017.05.13 조회 6572
• 1
Yummy 2016.09.21 조회 1367