【问题标题】:Controlling output limits in Scipy's ODEINT控制 Scipy 的 ODEINT 中的输出限制
【发布时间】:2016-02-03 22:38:44
【问题描述】:

我有以下涉及 ODE 系统的简化伪代码设置,我尝试使用 scipy 的 odeint 解决。

from scipy.integrate import odeint

def diff_func(y, time, parms):

    # Do stuff with parms that depends upon y and t.
    new_parms = other_funcs(y,time, parms)

    # Now calculate derivatives
    dy1_dt = dy1_func(y, new_parms)
    dy2_dt = dy2_func(y, new_parms)

# Setup up initial conditions
y_0 = [y_1_0, y_2_0]
time = np.linspace(0, 1000, 1000)
parms = list_o_constants

# Solve diffeq
yout, info = odeint(diff_func, y_0, time, args=(parms,), full_output=True)

根据我给它的输入,我可能会导致无意义的结果 - 或崩溃。我已将问题缩小到y 输入偶尔会变为负数,因为其中之一是温度 - 它会导致各种问题。首先它变为负数的原因尚不清楚,可能是稳定性问题。

我已尝试更改 diff 函数以包含如下检查:

def diff_func(y, time, parms):
    if y[1] <= 0.1:
        y[1] = 0.1

这似乎工作正常,我仍在测试它。但是,我想问一下:这是否会导致问题,以及是否有更好的方法强制 odeint 只允许正输出(即,如果发现负输出则更改步长)。

谢谢!

【问题讨论】:

    标签: python python-3.x scipy differential-equations odeint


    【解决方案1】:

    请先看看我最近发的other answer

    您想要做的是向您的 ODE 添加饱和约束。实现这种行为有几种可能性,但我推荐这个:

    # calculate dy[1]
    y1 = y[1]
    if dy[1] < 0 and y1 <= 0.1 # or 0.0, depends on accuracy you wish to maintain
        y1 = 0
        dy1 = 0 # this prevents your variable from going further down
    else
        # normal routine
    

    如果适合你,请告诉我。

    最好的问候。

    【讨论】:

      猜你喜欢
      • 2016-03-29
      • 1970-01-01
      • 2011-10-07
      • 1970-01-01
      • 2016-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-22
      相关资源
      最近更新 更多