Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 不连续动力学函数上的Scipy odeint中断,例如xdot=-sgn(x)_Python_Numpy_Scipy_Numerical - Fatal编程技术网

Python 不连续动力学函数上的Scipy odeint中断,例如xdot=-sgn(x)

Python 不连续动力学函数上的Scipy odeint中断,例如xdot=-sgn(x),python,numpy,scipy,numerical,Python,Numpy,Scipy,Numerical,我试图使用Python和scipy.integrate.odeint来模拟以下动态系统: 但是这种集成在Python中以数字形式中断,从而产生以下和类似的图像(通常比这更糟): 在iPython/Jupyter笔记本中使用以下内容生成: import numpy as np from scipy.integrate import odeint import matplotlib.pyplot as plt %matplotlib inline f = lambda x,t: -np.sig

我试图使用
Python
scipy.integrate.odeint
来模拟以下动态系统:

但是这种集成在
Python
中以数字形式中断,从而产生以下和类似的图像(通常比这更糟):

iPython/Jupyter笔记本中使用以下内容生成:

import numpy as np
from scipy.integrate import odeint
import matplotlib.pyplot as plt
%matplotlib inline

f = lambda x,t: -np.sign(x)
x0 = 3
ts = np.linspace(0,10,1000)
xs = odeint(f,x0,ts)
plt.plot(ts,xs)
plt.show()
如何更好地模拟这样一个具有不连续动力学的系统,有什么建议吗

编辑#1:

当以较小的时间步长运行时的示例结果,
ts=np.linspace(0,101000000)
,以响应@Hun的回答。根据我的预期,这也是一个不正确的结果


我完全按照所示运行了您的代码,这就是我得到的


我也完全按照图中所示运行了它,得到了:


一种更有效的方法是实现一个自定义的
sgn()
函数,该函数的ε公差为零,而不是期望与零精确相等

import numpy as np
from scipy.integrate import odeint
import matplotlib.pyplot as plt
%matplotlib inline

sgn = lambda x,eps: (x<-eps)*(-1) + (x>=-eps and x<eps)*0 + (x>=eps)*1
f = lambda x,t: -sgn(x,1e-7)
x0 = 3
ts = np.linspace(0,10,1000)
xs = odeint(f,x0,ts)
plt.plot(ts,xs)
plt.show()
将numpy导入为np
从scipy.integrate导入odeint
将matplotlib.pyplot作为plt导入
%matplotlib内联
sgn=λx,每股收益:(x=-eps和x=eps)*1
f=λx,t:-sgn(x,1e-7)
x0=3
ts=np.linspace(0,101000)
xs=odeint(f,x0,ts)
plt.绘图(ts、xs)
plt.show()

请注意,1e-7的
eps
参数对我来说效果很好,但小于该参数似乎会崩溃。它似乎来自
numpy.finfo
并证明了
numpy.float32
的机器ε大约是1e-7,而
float
大约是1e-16。

只是补充一点,我在重复执行代码时会得到不同的图像……另外,运行Python版本:2.7.11I正在使用Python 3.5.1、numpy 1.10.4、scipy 0.17.0。当我完全按照图中所示运行你的代码时,它工作得很好。这是我有时得到的另一种结果……你有关于那个动力系统的更多信息吗,它叫什么我可以查到的吗?你能不能再运行几次,并报告你是否总是得到这个图像?(这是我期望它的样子)ts=np.linspace(0,101000)#使这个时间步长变小。例如,设为1e4。不会解决问题。我在
https://try.jupyter.org/
python2
以及
ts=np.linspace(0,101000000)
这一行,并得到了一个相关的问题(请参见添加到我的原始帖子中的图像,因为我无法将其包含在注释中)。