【问题标题】:Using adaptive time step for scipy.integrate.ode when solving ODE systems在求解 ODE 系统时对 scipy.integrate.ode 使用自适应时间步长
【发布时间】:2017-02-09 01:36:14
【问题描述】:

我必须阅读Using adaptive step sizes with scipy.integrate.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.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()

它产生的情节:

【问题讨论】:

    标签: python python-3.x scipy ode


    【解决方案1】:

    在一行

       y_solutions.append(y) 
    

    你认为你是附加电流矢量。什么actally情况是,要附加的对象引用y。因为显然积分积分环路中重复使用矢量y,你总是追加相同的对象引用。因此,在结束时,将列表中的每个位置由相同的参考指向y的最后状态的向量填充。 P>

    长话短说:与替换 P>

        y_solutions.append(y.copy()) 
    

    和一切都很好。 P>

    【讨论】:

    • 非常感谢!引用仍然设法有时混淆我...顺便说一句,你是怎么来到这个解决方案? SPAN>
    • 按照调试。该q'n'd解决方案是增加的solution_getter内和小区之前打印语句,看看实际的数据是什么。 SPAN>
    猜你喜欢
    • 1970-01-01
    • 2012-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-18
    • 2017-08-16
    相关资源
    最近更新 更多