【问题标题】:Scipy ode solverScipy ode 求解器
【发布时间】:2016-10-06 17:05:06
【问题描述】:

使用 scipy 版本 0.18.0 并真正抓挠 此刻我的头。 我将代码简化为以下最小示例:

尽量求解最简单的微分方程

def phase(t, y):
    c1 = y
    dydt = - c1 
    return dydt

c1 = 1.0
y0 = c1
t = np.linspace(0,1,100)
ode_obj = sp.integrate.ode(phase)
ode_obj.set_initial_value(y0)
sol = ode_obj.integrate(t)

对象告诉我它成功了,但是返回值 是一个长度为 1 的 numpy 数组。

文档非常稀少,我不确定 如果我用错了。

感谢大家的帮助。

您好。

【问题讨论】:

    标签: python scipy


    【解决方案1】:

    在github上发现这个问题https://github.com/scipy/scipy/issues/1976 这基本上解释了它是如何工作的,以及当你认为这些初始 价值求解器工作(逐步进入终点的方向)它 更清楚为什么要这样做。我上面的代码看起来像 这个:

    import scipy as sp
    import pylab as pb
    
    def phase(t, y):
        c1 = y
        dydt = - c1 
        return dydt
    
    c1 = 1.0
    t0 = 0.0 # not necessary here
    y0 = c1
    t1 = 5.0
    
    t = []
    sol = []
    ode_obj = sp.integrate.ode(phase)
    ode_obj.set_initial_value(y0, t=t0)
    ode_obj.set_integrator('vode',method='bdf',rtol=1.e-12)
    sol = ode_obj.integrate(t)
    while ode_obj.successful() and ode_obj.t < t1:
        ode_obj.integrate(t1,step=100)
        t.append(ode_obj.t)
        sol.append(ode_obj.y)
    
    pb.plot(t,sol)
    pb.show()
    

    这将产生以下输出:

    【讨论】:

      猜你喜欢
      • 2014-02-11
      • 2016-01-24
      • 2014-09-07
      • 2018-06-14
      • 2017-09-11
      • 1970-01-01
      • 2018-09-20
      • 2020-08-01
      • 2019-04-13
      相关资源
      最近更新 更多