Python SciPy solve_ivp的解包含一阶常微分方程系统的振荡
我试图解一个耦合一阶常微分方程组: 其中,本例中的Tf被视为常数,并给出Q(t)。Q(t)的曲线图如下所示。用于创建时间与Q图的数据文件位于 针对给定的Q(t)(指定为Python SciPy solve_ivp的解包含一阶常微分方程系统的振荡,python,scipy,ode,discrete-mathematics,Python,Scipy,Ode,Discrete Mathematics,我试图解一个耦合一阶常微分方程组: 其中,本例中的Tf被视为常数,并给出Q(t)。Q(t)的曲线图如下所示。用于创建时间与Q图的数据文件位于 针对给定的Q(t)(指定为qheat)求解该系统的Python代码是: 这产生了下图,但不幸的是,结果中出现了几次振荡。有没有更好的方法来解决这个耦合的常微分方程组 如评论中所述,建议插入Q。振荡通常发生在尝试使用RK45(solve_ivp标准)等显式方法求解刚性ODE系统时。由于您的ODE系统似乎是一个僵硬的系统,因此进一步建议使用隐式Runge-
qheat
)求解该系统的Python代码是:
这产生了下图,但不幸的是,结果中出现了几次振荡。有没有更好的方法来解决这个耦合的常微分方程组
如评论中所述,建议插入Q。振荡通常发生在尝试使用RK45(solve_ivp标准)等显式方法求解刚性ODE系统时。由于您的ODE系统似乎是一个僵硬的系统,因此进一步建议使用隐式Runge-Kutta方法,如“Radau”:
import matplotlib.pyplot as plt
import numpy as np
from scipy.integrate import solve_ivp
from scipy.interpolate import interp1d
# Data
time, qheat = np.loadtxt('timeq.txt', unpack=True)
# Interpolate Q
Q = interp1d(time, qheat) # linear spline
# Calculate Temperatures
def tc_dt(t, tc, ts, q):
rc = 1.94
cc = 62.7
return ((ts - tc) / (rc * cc)) + q / cc
def ts_dt(t, tc, ts):
rc = 1.94
ru = 3.08
cs = 4.5
tf = 298.15
return ((tf - ts) / (ru * cs)) - ((ts - tc) / (rc * cs))
def func(t, y):
idx = np.abs(time - t).argmin()
tcdt = tc_dt(t, y[0], y[1], Q(t))
tsdt = ts_dt(t, y[0], y[1])
return tcdt, tsdt
t0 = time[0]
tf = time[-1]
# Note the passed values for rtol and atol.
sol = solve_ivp(func, (t0, tf), (298.15, 298.15), method="Radau", t_eval=time, atol=1e-8, rtol=1e-8)
# Plot
fig, ax = plt.subplots()
ax.plot(sol.t, sol.y[0], label='tc')
ax.plot(sol.t, sol.y[1], label='ts')
ax.set_xlabel('Time [s]')
ax.set_ylabel('Temperature [K]')
ax.legend(loc='best')
plt.show()
给我:
通过向解算器提供雅可比矩阵,我最终得到了ODE系统的合理解决方案。下面是我的工作解决方案
import matplotlib.pyplot as plt
import numpy as np
from scipy.integrate import solve_ivp
from scipy.interpolate import interp1d
# Data
time, qheat = np.loadtxt('timeq.txt', unpack=True)
# Calculate Temperatures
interp_qheat = interp1d(time, qheat)
def tc_dt(t, tc, ts, q):
"""
dTc/dt = (Ts-Tc)/(Rc*Cc) + Q/Cc
"""
rc = 1.94
cc = 62.7
return ((ts - tc) / (rc * cc)) + q / cc
def ts_dt(t, tc, ts):
"""
dTs/dt = (Tf-Ts)/(Ru*Cs) - (Ts-Tc)/(Rc*Cs)
"""
rc = 1.94
ru = 3.08
cs = 4.5
tf = 298.15
return ((tf - ts) / (ru * cs)) - ((ts - tc) / (rc * cs))
def jacobian(t, y):
"""
Given the following system of ODEs
dTc/dt = (Ts-Tc)/(Rc*Cc) + Q/Cc
dTs/dt = (Tf-Ts)/(Ru*Cs) - (Ts-Tc)/(Rc*Cs)
determine the Jacobian matrix of the right-hand side as
Jacobian matrix = [df1/dTc, df2/dTc]
[df1/dTs, df2/dTs]
where
f1 = (Ts-Tc)/(Rc*Cc) + Q/Cc
f2 = (Tf-Ts)/(Ru*Cs) - (Ts-Tc)/(Rc*Cs)
"""
cc = 62.7
cs = 4.5
rc = 1.94
ru = 3.08
jc = np.array([
[-1 / (cc * rc), 1 / (cs * rc)],
[1 / (cc * rc), -1 / (cs * ru) - 1 / (cs * rc)]
])
return jc
def func(t, y):
"""
Right-hand side of the system of ODEs.
"""
q = interp_qheat(t)
tcdt = tc_dt(t, y[0], y[1], q)
tsdt = ts_dt(t, y[0], y[1])
return tcdt, tsdt
t0 = time[0]
tf = time[-1]
sol = solve_ivp(func, (t0, tf), (298.15, 298.15), method='BDF', t_eval=time, jac=jacobian)
# Plot
fig, ax = plt.subplots(tight_layout=True)
ax.plot(sol.t, sol.y[0], label='tc')
ax.plot(sol.t, sol.y[1], label='ts')
ax.set_xlabel('Time [s]')
ax.set_ylabel('Temperature [K]')
ax.legend(loc='center left', bbox_to_anchor=(1, 0.5), frameon=False)
plt.show()
生成的图如下所示。
插值Q的唯一优点是通过删除main函数中的
argmin()
来加快代码的执行。否则,插值Q不会改善结果。您对Q
的近似值可能就是问题所在。我建议为此创建一个。我在示例中没有显示它,但我尝试对q使用interp1d
,但它对结果中的振荡没有帮助。解决方案可能主要通过切换到BDF(隐式解算器)而不是默认解算器RK45来改进,一个明确的解算器。你也可以考虑限制最大步长,即使你在这里很好,在某些情况下你也可以很容易地越过尖峰。你的答案错过了一个应该出现的峰值。Q曲线上有10个峰function@MatthewFlamm我编辑了我的答案。我认为没有必要提供雅可比矩阵,相反,降低误差估计的绝对容差(atol)和相对容差(rtol)会得到同样合理的解决方案。对于那些使用更复杂的ode rhs面临相同问题且不想手动提供雅可比矩阵的用户来说,了解这一点可能很有用。我同意不需要提供雅可比矩阵(尽管建议这样做)。除了隐式方法外,降低公差或修改最大步长都可以捕获所有峰值。您可能想指出为什么不使用默认公差,答案中包含这两种情况实际上是非常有用的。
import matplotlib.pyplot as plt
import numpy as np
from scipy.integrate import solve_ivp
from scipy.interpolate import interp1d
# Data
time, qheat = np.loadtxt('timeq.txt', unpack=True)
# Calculate Temperatures
interp_qheat = interp1d(time, qheat)
def tc_dt(t, tc, ts, q):
"""
dTc/dt = (Ts-Tc)/(Rc*Cc) + Q/Cc
"""
rc = 1.94
cc = 62.7
return ((ts - tc) / (rc * cc)) + q / cc
def ts_dt(t, tc, ts):
"""
dTs/dt = (Tf-Ts)/(Ru*Cs) - (Ts-Tc)/(Rc*Cs)
"""
rc = 1.94
ru = 3.08
cs = 4.5
tf = 298.15
return ((tf - ts) / (ru * cs)) - ((ts - tc) / (rc * cs))
def jacobian(t, y):
"""
Given the following system of ODEs
dTc/dt = (Ts-Tc)/(Rc*Cc) + Q/Cc
dTs/dt = (Tf-Ts)/(Ru*Cs) - (Ts-Tc)/(Rc*Cs)
determine the Jacobian matrix of the right-hand side as
Jacobian matrix = [df1/dTc, df2/dTc]
[df1/dTs, df2/dTs]
where
f1 = (Ts-Tc)/(Rc*Cc) + Q/Cc
f2 = (Tf-Ts)/(Ru*Cs) - (Ts-Tc)/(Rc*Cs)
"""
cc = 62.7
cs = 4.5
rc = 1.94
ru = 3.08
jc = np.array([
[-1 / (cc * rc), 1 / (cs * rc)],
[1 / (cc * rc), -1 / (cs * ru) - 1 / (cs * rc)]
])
return jc
def func(t, y):
"""
Right-hand side of the system of ODEs.
"""
q = interp_qheat(t)
tcdt = tc_dt(t, y[0], y[1], q)
tsdt = ts_dt(t, y[0], y[1])
return tcdt, tsdt
t0 = time[0]
tf = time[-1]
sol = solve_ivp(func, (t0, tf), (298.15, 298.15), method='BDF', t_eval=time, jac=jacobian)
# Plot
fig, ax = plt.subplots(tight_layout=True)
ax.plot(sol.t, sol.y[0], label='tc')
ax.plot(sol.t, sol.y[1], label='ts')
ax.set_xlabel('Time [s]')
ax.set_ylabel('Temperature [K]')
ax.legend(loc='center left', bbox_to_anchor=(1, 0.5), frameon=False)
plt.show()