【发布时间】:2016-03-22 10:23:25
【问题描述】:
我想用代码实现下面给出的系统,但是当我将它增加到 1500 次迭代时,我得到以下错误:
Warning (from warnings module): File "D:\python test files\sys1.py", line 16 dy = c*x- x*z + w RuntimeWarning: overflow encountered in double_scalars Warning (from warnings module): File "D:\python test files\sys1.py", line 17 dz = -b*z + x*y RuntimeWarning: overflow encountered in double_scalars Warning (from warnings module): File "D:\python test files\sys1.py", line 18 du = -h*u - x*z RuntimeWarning: overflow encountered in double_scalars Warning (from warnings module): File "D:\python test files\sys1.py", line 42 zs[i+1] = zs[i] + (dz * t) RuntimeWarning: invalid value encountered in double_scalars Warning (from warnings module): File "D:\python test files\sys1.py", line 15 dx = a*(y-x) + u RuntimeWarning: invalid value encountered in double_scalars Warning (from warnings module): File "D:\python test files\sys1.py", line 19 dw = k1*x - k2*y RuntimeWarning: invalid value encountered in double_scalars Warning (from warnings module): File "C:\Python27\lib\site-packages\mpl_toolkits\mplot3d\proj3d.py", line 156 txs, tys, tzs = vecw[0]/w, vecw[1]/w, vecw[2]/w RuntimeWarning: invalid value encountered in divide
我的代码:
from __future__ import division
import numpy as np
import math
import random
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
# import pdb
# pdb.set_trace()
def sys1(x, y, z, u, w , a=10, b=8.0/3.0, c=28, k1=0.4, k2=8, h=-2):
dx = a*(y-x) + u
dy = c*x- x*z + w
dz = -b*z + x*y
du = -h*u - x*z
dw = k1*x - k2*y
return dx, dy, dz, du, dw
t = 0.01
itera = 2500
# Need one more for the initial values
xs = np.empty((itera+1,))
ys = np.empty((itera+1,))
zs = np.empty((itera+1,))
us = np.empty((itera+1,))
ws = np.empty((itera+1,))
# Setting initial values
xs[0], ys[0], zs[0], us[0], ws[0] = (0.1, 0.1, 0.1, 0.1, 0.1)
# Stepping through "time".
for i in range(itera):
# Derivatives of the X, Y, Z state
dx, dy, dz, du, dw = sys1(xs[i], ys[i], zs[i], us[i], ws[i])
xs[i+1] = xs[i] + (dx * t)
ys[i+1] = ys[i] + (dy * t)
zs[i+1] = zs[i] + (dz * t)
us[i+1] = us[i] + (du * t)
ws[i+1] = ws[i] + (dw * t)
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.plot(xs, ys, zs)
ax.set_xlabel("X Axis")
ax.set_ylabel("Y Axis")
ax.set_zlabel("Z Axis")
ax.set_title("Lorenz Attractor")
plt.show()
【问题讨论】:
-
某处被零除,发出警告。
-
虽然我运行代码时没有收到任何警告。你使用的是什么版本的 python/matplotlib?
-
我正在使用 python 2.7.6 和 matplotlib 1.3.1。
-
我查看了您的代码,当
itera达到值 1590 时,dy和du等于无穷大,dz等于负无穷大。在此之上,你所有的值,dx,dy,dz,du,dw都是无穷大的。这就是您的问题所在。 -
感谢大卫,但我需要系统迭代更多的值,在 MATLAB 中我做到了,但在 python 中它显示了问题,你有什么建议可以用任何其他方式实现吗?
标签: python python-2.7 numpy matplotlib ode