Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/290.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 在求解ode系统时,对scipy.integrate.ode使用自适应时间步长_Python_Python 3.x_Scipy_Ode - Fatal编程技术网

Python 在求解ode系统时,对scipy.integrate.ode使用自适应时间步长

Python 在求解ode系统时,对scipy.integrate.ode使用自适应时间步长,python,python-3.x,scipy,ode,Python,Python 3.x,Scipy,Ode,我只需要阅读和接受这个问题的解决方案,甚至在我的Python解释器中复制和粘贴结果 我的问题是,当我尝试将解决方案代码改编为我自己的代码时,我只得到了平直的线条 我的代码如下: from scipy.integrate import ode from matplotlib.pyplot import plot, show initials = [1,1,1,1,1] integration_range = (0, 100) f = lambda t,y: [1.0*y[0]*y[1], -1

我只需要阅读和接受这个问题的解决方案,甚至在我的Python解释器中复制和粘贴结果

我的问题是,当我尝试将解决方案代码改编为我自己的代码时,我只得到了平直的线条

我的代码如下:

from scipy.integrate import ode
from matplotlib.pyplot import plot, show

initials = [1,1,1,1,1]
integration_range = (0, 100)

f = lambda t,y: [1.0*y[0]*y[1], -1.0*y[0]*y[1], 1.0*y[2]*y[3] - 1.0*y[2], -1.0*y[2]*y[3], 1.0*y[2], ]

y_solutions = []
t_solutions = []
def solution_getter(t,y): 
   t_solutions.append(t)
   y_solutions.append(y) 


backend = "dopri5"
ode_solver = ode(f).set_integrator(backend)
ode_solver.set_solout(solution_getter)
ode_solver.set_initial_value(y=initials, t=0)

ode_solver.integrate(integration_range[1])

plot(t_solutions,y_solutions)
show()
以及它产生的情节: 在队列中

   y_solutions.append(y) 
您认为您正在附加当前向量。实际发生的情况是将对象引用附加到
y
。由于显然积分器在积分循环期间重用向量
y
,因此您总是附加相同的对象引用。因此,在最后,列表的每个位置都由指向最后状态
y
的向量的相同引用填充

长话短说:替换为

    y_solutions.append(y.copy()) 

一切都很好。

非常感谢!参考资料有时还是会让我困惑。。。顺便说一句,你是如何得到这个解决方案的?通过调试。q'n'd解决方案是在solution_getter内部和绘图之前添加打印语句,以查看实际数据是什么。