ode
解算器来自
scipy.integrate
dt
,这就是我得到的:
我希望结果是正确的
集成步长很小,但我看到的几乎相反。。。有人知道发生了什么事吗?
--
代码:
from scipy.integrate import ode
import matplotlib.pyplot as plt
import numpy as np
y0, t0 = 0., 0
def f2(t, y, arg1):
t0 = 5
shape = np.piecewise(t, [t<t0, t>=t0], [arg1*t, arg1*(2*t0-t)])
return shape
t1 = 10.
dtlist = np.array([t1/j for j in range(1, 50)])
temp = []
for i in dtlist:
dt = i
r = ode(f2).set_integrator('zvode', method='bdf')
r.set_initial_value(y0, t0).set_f_params(2.0)
while r.successful() and r.t < t1:
r.integrate(r.t+dt)
temp.append(r.y)
fig, axs = plt.subplots(1, 1, figsize=(15, 6), facecolor='w', edgecolor='k')
xaxis = dtlist
yaxis = temp
axs.plot(xaxis, yaxis, 'bo', label = 'obtained')
axs.plot(xaxis, xaxis - xaxis + 50, 'r--', label = 'expected')
axs.set_xlabel('time increment $\delta t$')
axs.set_ylabel('Final $y$ value')
axs.legend(loc = 4)