【问题标题】:Update initial condition in ODE solver each time step每个时间步更新 ODE 求解器中的初始条件
【发布时间】:2013-05-07 08:55:23
【问题描述】:

我想解决一个 ODE 系统,其中在前 30,000 秒内,我希望我的一个状态变量从相同的初始值开始。在这 30,000 秒之后,我想将该状态变量的初始值更改为不同的值,并在其余时间模拟系统。这是我的代码:

def ode_rhs(y, t):
    ydot[0] = -p[7]*y[0]*y[1] + p[8]*y[8] + p[9]*y[8]
    ydot[1] = -p[7]*y[0]*y[1] + p[8]*y[8]
    ydot[2] = -p[10]*y[2]*y[3] + p[11]*y[9] + p[12]*y[9]
    ydot[3] = -p[13]*y[3]*y[6] + p[14]*y[10] + p[15]*y[10] - p[10]*y[2]*y[3] + p[11]*y[9] + p[9]*y[8] - p[21]*y[3]
    ydot[4] = -p[19]*y[4]*y[5] - p[16]*y[4]*y[5] + p[17]*y[11] - p[23]*y[4] + y[7]*p[20]
    ydot[5] = -p[19]*y[4]*y[5] + p[15]*y[10] - p[16]*y[4]*y[5] + p[17]*y[11] + p[18]*y[11] + p[12]*y[9] - p[22]*y[5]
    ydot[6] = -p[13]*y[3]*y[6] + p[14]*y[10] - p[22]*y[6] - p[25]*y[6] - p[23]*y[6]
    ydot[7] = 0
    ydot[8] = p[7]*y[0]*y[1] - p[8]*y[8] - p[9]*y[8]
    ydot[9] = p[10]*y[2]*y[3] - p[11]*y[9] - p[12]*y[9] - p[21]*y[9]
    ydot[10] = p[13]*y[3]*y[6] - p[14]*y[10] - p[15]*y[10] - p[22]*y[10] - p[21]*y[10] - p[23]*y[10]
    ydot[11] = p[19]*y[4]*y[5] + p[16]*y[4]*y[5] - p[17]*y[11] - p[18]*y[11] - p[22]*y[11] - p[23]*y[11]
    ydot[12] = p[22]*y[10] + p[22]*y[11] + p[22]*y[5] + p[22]*y[6] + p[21]*y[10] + p[21]*y[3] + p[21]*y[9] + p[24]*y[13] + p[25]*y[6] + p[23]*y[10] + p[23]*y[11] + p[23]*y[4] + p[23]*y[6]
    ydot[13] = p[15]*y[10] + p[18]*y[11] - p[24]*y[13]
    return ydot

pysb.bng.generate_equations(model)
alias_model_components()
p = np.array([k.value for k in model.parameters])
ydot = np.zeros(len(model.odes))
y0 = np.zeros(len(model.odes))
y0[0:7] = p[0:7]
t = np.linspace(0.0,1000000.0,100000)
r = odeint(ode_rhs,y0,t)

因此,换句话说,我想在前 30,000 秒内每次调用 odeint 时将 y0[1] 设置为相同的值 (100)。在将信号输入系统之前,我正在有效地尝试让系统平衡一段时间。我考虑过将if t < 30000: y0[1] = 100 之类的东西作为ode_rhs() 函数的第一行,但我不太确定它是否有效。

【问题讨论】:

  • 我不确定我是否关注你,但初始条件是初始条件,例如 t=0。在初始时间之后更改初始条件是没有意义的。此外,您只调用一次odeint。 “在前 30,000 秒内每次调用 odeint”是什么意思?
  • 为什么这个问题有matlab标签?为什么不scipy
  • 你是对的 - 它应该被标记为 scipy 而不是 matlab。谢谢。
  • 好的,我更新了标签。

标签: python numpy scipy integrate ode


【解决方案1】:

听起来您希望 y1(t) 保持不变(值为 100) 平衡阶段。您可以通过确保在此期间 dy1(t)/dt = 0 来做到这一点 阶段。有(至少)两种方法可以做到这一点。第一个是 修改ode_rhsydot[1]的计算如下:

if t < 30000:
    ydot[1] = 0.0
else:
    ydot[1] = -p[7]*y[0]*y[1] + p[8]*y[8]

并为y[1] 使用初始条件100。

请注意,这会在系统右侧引入不连续性, 但odeint(Fortran 代码 LSODA)使用的自适应求解器通常足够强大,可以处理它。

这是一个独立的示例。我向ode_rhs 提出了pt1 参数。 t1 是平衡阶段的持续时间。

import numpy as np
from scipy.integrate import odeint
import matplotlib.pyplot as plt


def ode_rhs(y, t, p, t1):
    ydot[0] = -p[0]*y[0]*y[1] + p[1]*y[2] + p[2]*y[2]
    if t < t1:
        ydot[1] = 0.0
    else:
        ydot[1] = -p[0]*y[0]*y[1] + p[1]*y[2]
    ydot[2] = p[0]*y[0]*y[1] - p[1]*y[2] - p[2]*y[2]
    return ydot


ydot = np.zeros(3)
p = np.array([0.01, 0.25, 0.1])
y0 = [20.0, 100.0, 0.0]
t = np.linspace(0, 200, 2001)
t1 = 20.0

sol = odeint(ode_rhs, y0, t, args=(p, t1))


plt.figure(1)
plt.clf()

plt.subplot(3, 1, 1)
plt.plot(t, sol[:, 0])
plt.axvline(t1, color='r')
plt.grid(True)
plt.ylabel('y[0]')


plt.subplot(3, 1, 2)
plt.plot(t, sol[:, 1])
plt.axvline(t1, color='r')
plt.grid(True)
plt.ylabel('y[1]')
plt.ylim(0, 110)

plt.subplot(3, 1, 3)
plt.plot(t, sol[:, 2])
plt.axvline(t1, color='r')
plt.grid(True)
plt.ylabel('y[2]')
plt.xlabel('t')

plt.show()

上述方法的一个细微变化是通过添加一个 参数为 0 或 1。当参数为 0 时,求解平衡系统,当参数为 1 时,求解整个系统。 ydot[1] 的代码(在我的小例子中)是

ydot[1] = full * (-p[0]*y[0]*y[1] + p[1]*y[2])

full 是参数。

为了处理平衡阶段,系统在 0 full=0。然后将平衡解的最终值用作 第二个解决方案的初始条件,使用full=1 运行。这种方法的优点是您不会强迫求解器处理不连续性。

这是它在代码中的样子。

import numpy as np
from scipy.integrate import odeint
import matplotlib.pyplot as plt


def ode_rhs(y, t, p, full):
    ydot[0] = -p[0]*y[0]*y[1] + p[1]*y[2] + p[2]*y[2]
    ydot[1] = full * (-p[0]*y[0]*y[1] + p[1]*y[2])
    ydot[2] = p[0]*y[0]*y[1] - p[1]*y[2] - p[2]*y[2]
    return ydot


ydot = np.zeros(3)
p = np.array([0.01, 0.25, 0.1])
y0 = [20.0, 100.0, 0.0]
t1 = 20.0  # Equilibration time
tf = 200.0  # Final time

# Solve the equilibration phase.
teq = np.linspace(0, t1, 100)
full = 0
soleq = odeint(ode_rhs, y0, teq, args=(p, full))

# Solve the full system, using the final point of the
# equilibration phase as the initial condition.
y0 = soleq[-1]
# Note: the system is autonomous, so we could just as well start
# at t0=0.  But starting at t1 makes the plots (below) align without
# any additional shifting of the time arrays.
t = np.linspace(t1, tf, 2000)
full = 1
sol = odeint(ode_rhs, y0, t, args=(p, full))

plt.figure(2)
plt.clf()
plt.subplot(3, 1, 1)
plt.plot(teq, soleq[:, 0], t, sol[:, 0])
plt.axvline(t1, color='r')
plt.grid(True)
plt.ylabel('y[0]')

plt.subplot(3, 1, 2)
plt.plot(teq, soleq[:, 1], t, sol[:, 1])
plt.axvline(t1, color='r')
plt.grid(True)
plt.ylabel('y[1]')
plt.ylim(0, 110)

plt.subplot(3, 1, 3)
plt.plot(teq, soleq[:, 2], t, sol[:, 2])
plt.axvline(t1, color='r')
plt.grid(True)
plt.ylabel('y[2]')
plt.xlabel('t')

plt.show()

这是它生成的图(第一个示例中的图是 几乎完全相同):

【讨论】:

  • 非常感谢 - 非常雄辩和完美的解释。完全有道理。
【解决方案2】:

这个答案对我不起作用,因为我需要定期更改我的初始条件。因此,我想提出一个替代解决方案,即在函数本身内部对微分方程进行替代条件:

我们查看t的值并调整它:

if int(t) > 20:
    full = 1
else:
    full = 0

这是在函数内部:

import numpy as np
from scipy.integrate import odeint
import matplotlib.pyplot as plt

def ode_rhs(y, t, p, full):

    if int(t) > 20:
        full = 1
    else:
        full = 0

    ydot[0] = -p[0]*y[0]*y[1] + p[1]*y[2] + p[2]*y[2]
    ydot[1] = full * (-p[0]*y[0]*y[1] + p[1]*y[2])
    ydot[2] = p[0]*y[0]*y[1] - p[1]*y[2] - p[2]*y[2]
    return ydot

ydot = np.zeros(3)

# intial conditions
p = np.array([0.01, 0.25, 0.1])
y0 = [20.0, 100.0, 0.0]
t = np.linspace(0, 200, 100)
full = 0

# solve equation
solution = odeint(ode_rhs, y0, t, args=(p, full))

plt.figure()
plt.clf()
plt.subplot(3, 1, 1)
plt.plot(t, solution[:, 0])
plt.axvline(20, color='r')  # vertical line
plt.grid(True)
plt.ylabel('y[0]')

plt.subplot(3, 1, 2)
plt.plot(t, solution[:, 1])
plt.axvline(20, color='r')  # vertical line
plt.grid(True)
plt.ylabel('y[1]')
plt.ylim(0, 110)

plt.subplot(3, 1, 3)
plt.plot(t, solution[:, 2])
plt.axvline(20, color='r')  # x=20 vertical line
plt.grid(True)
plt.ylabel('y[2]')
plt.xlabel('t')

plt.show()

这允许调用函数来求解方程一次。

  • 您不必弄乱上一步中的初始条件
  • 更容易绘制
  • 代码更简洁,更易于管理

更重要的是,您现在可以在方程内定期调整参数。例如,假设您有 t = [0:200] 并且您想每 20 步更改 full 的值,您可以这样做:

if int(t/20) % 2 == 0:
    full = 1
else:
    full = 0

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-02-17
    • 1970-01-01
    • 1970-01-01
    • 2020-01-02
    • 1970-01-01
    • 1970-01-01
    • 2013-01-03
    • 1970-01-01
    相关资源
    最近更新 更多