【发布时间】:2016-02-12 02:45:34
【问题描述】:
我尝试使用 python 3.5 和 scipy.integrate.odeint 函数集成方波,但结果没有任何意义,并且随着所选时间点数组的变化而变化很大。
方波的周期为 10 秒,模拟运行 100 秒。由于时间点数组的大小为 500,方波的每个周期将有 50 个时间点,但这似乎没有发生。
使用可选参数hmax=0.02修复它,但不应该自动推断吗?
代码如下:
import numpy as np
import matplotlib.pyplot as plt
import scipy.integrate as integrate
# dx/dt = f(t), where f(t) is a square wave
def f(x, t):
return float(t % 10.0 < 5.0) * 0.3
T = 100
tt = np.linspace(0, T, 500)
xx = integrate.odeint(f, 0, tt, hmax=0.2)
plt.figure()
plt.subplot(2,1,1)
plt.plot(tt, xx)
plt.axis([0,T,0,16])
plt.subplot(2,1,2)
plt.plot(tt, [f(None,t) for t in tt])
plt.axis([0, T, 0, 1])
plt.show()
我希望有人能对这里发生的事情有所了解。
尝试在 80 到 100(模拟时间)之间更改 T。
【问题讨论】: