代码之家  ›  专栏  ›  技术社区  ›  SuperCiocia

在ode solver-python中使用的步长

  •  1
  • SuperCiocia  · 技术社区  · 6 年前

    ode 解算器来自 scipy.integrate dt ,这就是我得到的:

    enter image description here

    我希望结果是正确的 集成步长很小,但我看到的几乎相反。。。有人知道发生了什么事吗?

    --

    代码:

    from scipy.integrate import ode
    import matplotlib.pyplot as plt
    import numpy as np
    
    y0, t0 = 0., 0
    
    def f2(t, y, arg1):
        t0 = 5
        shape = np.piecewise(t, [t<t0, t>=t0], [arg1*t, arg1*(2*t0-t)])
        return shape
    
    t1 = 10.
    dtlist = np.array([t1/j for j in range(1, 50)])
    temp = []
    
    for i in dtlist:
        dt = i
        r = ode(f2).set_integrator('zvode', method='bdf')
        r.set_initial_value(y0, t0).set_f_params(2.0)   
    
        while r.successful() and r.t < t1:
            r.integrate(r.t+dt)
        temp.append(r.y)
    
    fig, axs = plt.subplots(1, 1, figsize=(15, 6), facecolor='w', edgecolor='k')
    xaxis = dtlist
    yaxis = temp
    axs.plot(xaxis, yaxis, 'bo', label = 'obtained')
    axs.plot(xaxis, xaxis - xaxis + 50, 'r--', label = 'expected')
    axs.set_xlabel('time increment $\delta t$')
    axs.set_ylabel('Final $y$ value')
    axs.legend(loc = 4)
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   Lutz Lehmann    6 年前

    您需要确保最后一个集成步骤始终在 t1 r.t 略小于 t1级 t1+dt 执行。你可以用

    r.integrate(min(r.t+dt, t1))
    

    r.t+dt 已经接近 t1级 .