-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patheuler.py
More file actions
141 lines (110 loc) · 3.4 KB
/
Copy patheuler.py
File metadata and controls
141 lines (110 loc) · 3.4 KB
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
import numpy as np
import matplotlib.pyplot as plt
class TimeSeries:
def __init__(self, time_grid, values, omega):
"""
Constructs a time series
:param time_grid: array starting with 0 and containing the times at which the values in param values are defined
:param values: values assumed by the time series
:param omega: callable in the form of f(t) which returns the values for t < 0
"""
self.time_grid = time_grid
self.values = values
self.omega = omega
def __call__(self, t):
"""
Gets the value at time t
:param t: time at which to get values
:return: the value at time t
"""
if t < 0:
x = self.omega(t)
else:
idx = np.searchsorted(self.time_grid, t)
x = self.values[idx]
return x
class EulerMethod:
def __init__(self, f, x0, t_vals):
"""
:param f: right hand side of Differential Equation dot{x} = f(t, x)
:param x0: initial values
:param t_vals: values on which to evaluate f (from t1 to T)
"""
self.f = f
self.x0 = x0
self.t_vals = t_vals
self.x_vals = np.zeros((len(self.t_vals), len(x0)))
self.x_vals[0] = x0
# t_vals = [0:0,1:1,2:2, ...]
# x_vals = [0:a,1:b,2:c, ...]
def run(self):
for i in range(0, len(self.t_vals) - 1):
t = self.t_vals[i]
x_t = self.x_vals[i]
epsilon = self.t_vals[i + 1] - t
f_t = np.asarray(self.f(t, x_t))
self.x_vals[i + 1] = x_t + epsilon * f_t
return list(self.x_vals)
class DelayedEulerMethod():
def __init__(self, f, omega, t_vals):
self.f = f
x0 = omega(0)
x_vals = np.zeros((len(t_vals), len(x0)))
x_vals[0] = x0
self.t_series = TimeSeries(t_vals, x_vals, omega)
def run(self):
for i in range(0, len(self.t_series.time_grid) - 1):
t = self.t_series.time_grid[i]
x_t = self.t_series.values[i]
epsilon = self.t_series.time_grid[i + 1] - t
f_t = np.asarray(self.f(t, self.t_series, epsilon))
self.t_series.values[i + 1] = x_t + epsilon * f_t
return list(self.t_series.values)
gamma = 0.1
beta = 0.170
epsilon_s = 1e-6
S0 = 1 - epsilon_s
I0 = epsilon_s
ND = 500
TS = 0.01
tau = 6
def f(t, x):
return [
- beta * x[0] * x[1],
beta * x[0] * x[1] - gamma * x[1],
gamma * x[1]
]
def f_past_2(t, x, x_past, dt):
return [
- beta * x[0] * x[1],
beta * x[0] * x[1] - gamma * x_past[1],
gamma * x_past[1]
]
def f_past(t, X, dt):
out = [
- beta * X(t)[0] * X(t)[1],
beta * X(t)[0] * X(t)[1] - gamma * X(t-tau)[1],
gamma * X(t-tau)[1]
]
if out[1] * dt + X(t)[1] < 0:
out[1] = -X(t)[1] / dt
out[2] = out[0] + X(t)[1] / dt
if out[1] * dt + X(t)[1] > 1:
out[1] = (1 - X(t)[1]) / dt
out[2] = out[0] + (1 - X(t)[1]) / dt
return out
def omega(t):
return [
0 if t < 0 else S0,
0 if t < 0 else I0,
0
]
if __name__ == '__main__':
t_range = np.arange(0, ND, TS)
euler = DelayedEulerMethod(f_past, omega, t_range)
sol = euler.run()
a = plt.figure(1)
plt.plot(t_range, sol)
plt.grid()
a.show()
print(f"x: {sol[len(sol) - 1][0]}, y: {sol[len(sol) - 1][2]}")