-
Notifications
You must be signed in to change notification settings - Fork 73
/
runga_kutta_4rth_order.py
63 lines (45 loc) · 1.16 KB
/
runga_kutta_4rth_order.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#! /usr/bin/env python
"""
@author Rafiul
@require numpy
"""
from numpy import arange
from numpy import array
"""
Function of runga kutta method
@param f = function
@param xr = x range or x interval
@param y0 = x0 corresponding y0
@param h = constant difference between 2 corresponding x
@return x array and y array
"""
def runga_kutta(f, xr, y0, h):
# create an empty list of y values
y = []
# append y0 into y as fitst value
y.append(y0)
# x array using constant difference in interval
x = arange(xr[0], xr[1]+h, h)
# runga kutta method:
# y1 = y0 + 1/6(k1+k2+k3+k4)
# loop through for xi -> yi
for i in range(1, len(x)):
k1 = h*f (x[i-1],y[i-1])
k2 = h*f (x[i-1]+(h/2), y[i-1]+(h/2*k1))
k3 = h*f (x[i-1]+(h/2), y[i-1]+(h/2*k2))
k4 = h*f (x[i-1]+h, y[i-1]+(h*k3))
# append yi into y array
y.append(y[i-1] +(1/6)*(k1+(2*k2)+(2*k3)+k4))
# return x and y values in numpy array format
return x, array(y)
# driver
def main():
# define the function
f = lambda x, y : x+y
# call runga kutta with params
ans = runga_kutta(f, [0,1], 1, 0.1)
# print x and y array
print(ans[0])
print(ans[1])
if __name__ == '__main__':
main()