-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolver_test.py
60 lines (51 loc) · 1.47 KB
/
solver_test.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
import numpy as np
import matplotlib.pyplot as plt
import math
from scipy.integrate import odeint
def diff(y, t, tau, f):
# 定义微分方程
# dydt = -(1/tau + f(t))*y + f(t)
dydt = -(1/tau)*y + f(t)
return dydt
def euler_solver(y0, step_size, step_num, tau, f):
yt = y0
step = 0
ans = [y0]
while(step < step_num):
ytt = yt + step_size * diff(yt, step*step_size, tau, f)
yt = ytt
ans.append(yt)
step += 1
return ans[0:-1]
def fused_solver(y0, step_size, step_num, tau, f):
yt = y0
step = 0
ans = [y0]
while(step < step_num):
# ytt = (yt + step_size * f(step*step_size)) / (1 + step_size * (1/tau + f(step*step_size)))
ytt = (yt + step_size * f(step*step_size)) / (1 + step_size * (1/tau))
yt = ytt
ans.append(yt)
step += 1
return ans[0:-1]
# 定义时间常数的取值范围和步长
#tau_values = [0.1, 1, 3.0]
TAU = 2
function = math.sin
t = np.linspace(0, 20, 200) # 时间范围和步长
euler = euler_solver(1, 0.1, 200, TAU, function)
fused = fused_solver(1, 0.1, 200, TAU, function)
# 绘制不同时间常数下的微分方程解
y0 = 1.0 # 初始条件
y = odeint(diff, y0, t, args=(TAU, function))
plt.plot(t, y, label=f'baseline')
plt.plot(t, euler, label=f'Euler')
plt.plot(t, fused, label=f'Fused')
# 设置图形标题和标签
plt.title('Different ODE solvers')
plt.xlabel('Time')
plt.ylabel('y')
# 添加图例
plt.legend()
# 显示图形
plt.show()