-
Notifications
You must be signed in to change notification settings - Fork 0
/
dynamic_triple.py
276 lines (240 loc) · 8.38 KB
/
dynamic_triple.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# 3-ODE Dynamical System Adams-Bashforth-Moulton estimation using
# Runge-Kutta four for initial values.
#
# 3D plotting using matplotlib Axes3D
#
# Input ODE equations as f1, f2, and f3
# Input t, x, y, z initial conditions
# Input a, b, n as lower bound, upper bound, num iterations
#
# This file is a Class structure so you must instantiate it first,
# ex: my_abm_calculation = ABM( enter all params here ), then call the
# other methods ex.: my_abm_calculation.plot3d()
#
# Can run ABM.plot() to see results. Can also run
# ABM.rk() or ABM.adams() by themselves, they output a list of lists
# result = [[xvals], [yvals], [zvals]] for plotting or analysis
#
# Note line in plot() that specifies RK vs. ABM results. You can
# 3D plot RK results too if you want!
#
# ABM needs lots of iterations! Do not run on <1000.
#
# To estimate a dynamical system of 3 ODEs (see examples folder):
#
# Note use of x, y, z as variables
#
# def f1(x, y, z):
# return (-1)*y - z
#
# def f2(x, y, z):
# return x + (0.1)*y
#
# def f3(x, y, z):
# return 0.1 + z*(x - 14)
#
# res = ABM(f1, f2, f3, 0, 15, 15, 36, 0, 100, 10000)
# res.plot()
#
# Output will be 3D matplotlib plot if you do this. Other methods like res.adams() will
# show numerical results.
#
import numpy as np
import matplotlib.pyplot as plt
class ABM:
def __init__(
self,
f1,
f2,
f3,
t0: float,
x0: float,
y0: float,
z0: float,
a: float,
b: float,
n: int,
):
# function 1, func2, func3, initial t0, x0, y0, z0, lower bound, upper, num iterations
self.f1 = f1
self.f2 = f2
self.f3 = f3
self.t0 = t0
self.x0 = x0
self.y0 = y0
self.z0 = z0
self.a = a
self.b = b
self.n = n
def abm_comp(self):
self.adams()
# derivative functions
def x1p(self, x1: float, x2: float, x3: float) -> float:
return self.f1(x1, x2, x3)
def x2p(self, x1: float, x2: float, x3: float) -> float:
return self.f2(x1, x2, x3)
def x3p(self, x1: float, x2: float, x3: float) -> float:
return self.f3(x1, x2, x3)
def adams(self) -> list: # A-B-M computation, using RK4 to start
a = self.a
b = self.b
n = self.n
h = (b - a) / n
y1 = np.zeros(n) # initialize y1, y2, y3 matrices (and y_predictors)
y2 = np.zeros(n)
y3 = np.zeros(n)
y_pred1 = np.zeros(n)
y_pred2 = np.zeros(n)
y_pred3 = np.zeros(n)
for E in (0, 1, 2, 3):
y1[E] = self.rk(E, 1) # initial estimations to start A-B-M
y2[E] = self.rk(E, 2)
y3[E] = self.rk(E, 3)
for i in range(
4, n
): # Equations change slightly, hard to refactor to make clean
# x1p
y_pred1[i] = y1[i - 1] + (
h
/ 24
* (
55 * self.x1p(y1[i - 1], y2[i - 1], y3[i - 1])
- 59 * self.x1p(y1[i - 2], y2[i - 2], y3[i - 2])
+ 37 * self.x1p(y1[i - 3], y2[i - 3], y3[i - 3])
- 9 * 0
)
)
# x1p
y1[i] = y1[i - 1] + (
h
/ 24
* (
9 * self.x1p(y_pred1[i - 1], y_pred2[i - 1], y_pred3[i - 1])
+ 19 * self.x1p(y1[i - 1], y2[i - 1], y3[i - 1])
- 5 * self.x1p(y1[i - 2], y2[i - 2], y3[i - 2])
+ self.x1p(y1[i - 3], y2[i - 3], y3[i - 3])
)
)
# x2p
y_pred2[i] = y2[i - 1] + (
h
/ 24
* (
55 * self.x2p(y1[i - 1], y2[i - 1], y3[i - 1])
- 59 * self.x2p(y1[i - 2], y2[i - 2], y3[i - 2])
+ 37 * self.x2p(y1[i - 3], y2[i - 3], y3[i - 3])
- 9 * (15 * (-8) - 15)
)
)
# x2p
y2[i] = y2[i - 1] + (
h
/ 24
* (
9 * self.x2p(y_pred1[i - 1], y_pred2[i - 1], y_pred3[i - 1])
+ 19 * self.x2p(y1[i - 1], y2[i - 1], y3[i - 1])
- 5 * self.x2p(y1[i - 2], y2[i - 2], y3[i - 2])
+ self.x2p(y1[i - 3], y2[i - 3], y3[i - 3])
)
)
# x3p
y_pred3[i] = y3[i - 1] + (
h
/ 24
* (
55 * self.x3p(y1[i - 1], y2[i - 1], y3[i - 1])
- 59 * self.x3p(y1[i - 2], y2[i - 2], y3[i - 2])
+ 37 * self.x3p(y1[i - 3], y2[i - 3], y3[i - 3])
- 9 * ((15 * 15) - ((8 / 3) * 36))
)
)
# x3p
y3[i] = y3[i - 1] + (
h
/ 24
* (
9 * self.x3p(y_pred1[i - 1], y_pred2[i - 1], y_pred3[i - 1])
+ 19 * self.x3p(y1[i - 1], y2[i - 1], y3[i - 1])
- 5 * self.x3p(y1[i - 2], y2[i - 2], y3[i - 2])
+ self.x3p(y1[i - 3], y2[i - 3], y3[i - 3])
)
)
return [y1, y2, y3]
def rk(
self, which_y: int = 9, which_x: int = 9
) -> list: # Runge-Kutta 4 computation by itself
# which_y indicator for which Y1, Y2, Y3 val is needed to be return by function
# which_x means x1, x2, x3 when calling for values Y1, Y2, Y3, etc...
a = self.a
b = self.b
n = self.n
h = (b - a) / n # step size
p = 0 # flag for while loop
# initialize lists for plot
t = np.zeros(int(n + 1))
x1 = np.zeros(int(n + 1))
x2 = np.zeros(int(n + 1))
x3 = np.zeros(int(n + 1))
# define initial values
t[0] = self.t0
x1[0] = self.x0
x2[0] = self.y0
x3[0] = self.z0
# initialize slots for values of RK4
i = np.zeros(4) # for x1
j = np.zeros(4) # for x2
k = np.zeros(4) # for x3
# iterate n times
while p < n:
i[0] = h * self.x1p(x1[p], x2[p], x3[p])
j[0] = h * self.x2p(x1[p], x2[p], x3[p])
k[0] = h * self.x3p(x1[p], x2[p], x3[p])
i[1] = h * self.x1p(
x1[p] + (1 / 2) * i[0], x2[p] + (1 / 2) * j[0], x3[p] + (1 / 2) * k[0]
)
j[1] = h * self.x2p(
x1[p] + (1 / 2) * i[0], x2[p] + (1 / 2) * j[0], x3[p] + (1 / 2) * k[0]
)
k[1] = h * self.x3p(
x1[p] + (1 / 2) * i[0], x2[p] + (1 / 2) * j[0], x3[p] + (1 / 2) * k[0]
)
i[2] = h * self.x1p(
x1[p] + (1 / 2) * i[1], x2[p] + (1 / 2) * j[1], x3[p] + (1 / 2) * k[1]
)
j[2] = h * self.x2p(
x1[p] + (1 / 2) * i[1], x2[p] + (1 / 2) * j[1], x3[p] + (1 / 2) * k[1]
)
k[2] = h * self.x3p(
x1[p] + (1 / 2) * i[1], x2[p] + (1 / 2) * j[1], x3[p] + (1 / 2) * k[1]
)
i[3] = h * self.x1p(x1[p] + i[2], x2[p] + j[2], x3[p] + k[2])
j[3] = h * self.x2p(x1[p] + i[2], x2[p] + j[2], x3[p] + k[2])
k[3] = h * self.x3p(x1[p] + i[2], x2[p] + j[2], x3[p] + k[2])
x1[p + 1] = x1[p] + (1 / 6) * (i[0] + (2 * i[1]) + (2 * i[2]) + i[3])
x2[p + 1] = x2[p] + (1 / 6) * (j[0] + (2 * j[1]) + (2 * j[2]) + j[3])
x3[p + 1] = x3[p] + (1 / 6) * (k[0] + (2 * k[1]) + (2 * k[2]) + k[3])
t[p + 1] = t[p] + h
p += 1 # advance flag variable
if which_y in (0, 1, 2, 3):
if which_x == 1:
return x1[which_y]
elif which_x == 2:
return x2[which_y]
elif which_x == 3:
return x3[which_y]
else:
return []
return [x1, x2, x3]
def plot3d(self):
# res = self.rk() # to compare to RK solution only
res = self.adams()
# Plotting
fig = plt.figure()
ax = fig.gca(projection="3d")
ax.plot(res[0], res[1], res[2], linewidth=1.0, color="darkblue")
ax.set_xlabel("X")
ax.set_ylabel("Y")
ax.set_zlabel("Z")
plt.title("Adams-Bashforth-Moulton Estimation")
plt.style.use("ggplot")
plt.show()