-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy patheuler__runga_kutta__visualization.py
95 lines (67 loc) · 1.8 KB
/
euler__runga_kutta__visualization.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#! /usr/bin/env python
"""
@author Rafiul
@require numpy
@require matplotlib
"""
from numpy import arange
from numpy import array
from matplotlib import pyplot as graph
"""
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)
def euler(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)
# loop through for xi -> yi
for i in range(1, len(x)):
# append yi into y list
y.append(y[i-1]+h*f(y[i-1], x[i-1]))
# 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
e = euler(f, [0,1], 1, 0.1)
r = runga_kutta(f, [0,1], 1, 0.1)
graph.subplot(2,2,1)
graph.plot(e[0], e[1], 'green')
graph.legend('E')
graph.subplot(2,2,2)
graph.plot(r[0],r[1], 'blue')
graph.legend('R')
graph.subplot(2,1,2)
graph.plot(e[0],e[1],'green', r[0],r[1],'blue')
graph.legend('E' 'R')
graph.show()
if __name__ == '__main__':
main()