-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4th Order Runge Kutta.py
78 lines (57 loc) · 1.63 KB
/
4th Order Runge Kutta.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
import numpy as np
import pylab
import math
class RK4(object):
def __init__(self, *functions):
"""
Initialize a RK4 solver.
:param functions: The functions to solve.
"""
self.f = functions
self.t = 0
def solve(self, y, h, n,t0):
"""
Solve the system ODEs.
:param y: A list of starting values.
:param h: Step size.
:param n: Endpoint.
"""
self.t = t0
t = []
res = []
for i in y:
res.append([])
while self.t <= n and h != 0:
t.append(self.t)
y = self._solve(y, self.t, h)
for c, i in enumerate(y):
res[c].append(i)
self.t += h
# if self.t + h > n:
# h = n - self.t
return t, res[0]
def _solve(self, y, t, h):
functions = self.f
k1 = []
for f in functions:
k1.append(h * f(t, *y))
k2 = []
for f in functions:
k2.append(h * f(t + .5*h, *[y[i] + .5*h*k1[i] for i in xrange(0, len(y))]))
k3 = []
for f in functions:
k3.append(h * f(t + .5*h, *[y[i] + .5*h*k2[i] for i in xrange(0, len(y))]))
k4 = []
for f in functions:
k4.append(h * f(t + h, *[y[i] + h*k3[i] for i in xrange(0, len(y))]))
return [y[i] + (k1[i] + 2*k2[i] + 2*k3[i] + k4[i]) / 6.0 for i in xrange(0, len(y))]
ydot = lambda t, y: -(y**2)*t
lv = RK4(ydot)
a, b = lv.solve([y0], h, tf,t0)
length = len(a)
for i in range(0,length):
print a[i],
print " ",
print b[i]
pylab.plot(a,b)
pylab.show()