我是Python的新手,但我有C++和Matlab的经验。 我目前正在写一个程序,在相空间中绘制包含dy/dt和dx/dt的非线性系统的轨迹。但是,对于更复杂的函数形式,我收到了溢出错误。我有什么办法可以避开这个问题吗?提前谢谢!在
以下是我的密码:
fig = plt.figure(figsize=(18,6))
dt = 0.01
def trajectories():
#initial conditions
x = y = 0.1
xresult = [x]
yresult = [y]
for t in xrange(10000):
# functional form: dx/dt = y, dy/dt = -r(x**2-1)*y-x
nextx = x + (r*x-y+x*y**2) * dt
nexty = y + (x + r*y + y**3) * dt
x, y = nextx, nexty
xresult.append(x)
yresult.append(y)
plt.plot(xresult, yresult)
plt.axis('image')
plt.axis([-3, 3, -3, 3])
plt.title('r = ' + str(r))
rs = [-1, -0.1, 0, .1, 1]
for i in range(len(rs)):
fig.add_subplot(1, len(rs), i + 1)
r = rs[i]
trajectories()
plt.show()
EDIT: this is the full traceback
Traceback (most recent call last):
File "/Users/Griffin/Atom/NumInt.py", line 33, in <module>
trajectories()
File "/Users/Griffin/Atom/NumInt.py", line 18, in trajectories
nextx = x + (r*x-y+x*y**2) * dt
OverflowError: (34, 'Result too large')
您的直接错误与以下事实有关:您用于积分的Euler算法在您使用的步长下变得不稳定。最终的问题实际上是使用欧拉算法。下面的代码使用
scipy.integrate.odeint
来处理集成,由于能够执行可变的步长,因此可以做得更好。有些整合还不完善,但至少我们得到了一些结果。在结果:
相关问题 更多 >
编程相关推荐