我正在尝试解决耦合一阶ODE的系统:
在此示例中,Tf被认为是常数,并且给出了Q(t)。 Q(t)的曲线如下所示。 here中提供了用于创建时间vs Q图的数据文件。
我针对给定Q(t)(指定为qheat
)求解该系统的Python代码为:
import matplotlib.pyplot as plt
import numpy as np
from scipy.integrate import solve_ivp
# Data
time, qheat = np.loadtxt('timeq.txt', unpack=True)
# 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()
q = qheat[idx]
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), t_eval=time)
# 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耦合系统?
答案 0 :(得分:1)
就像在评论中已经说过的那样,建议对Q进行插值。当尝试使用诸如RK45(solve_ivp的标准)之类的显式方法求解刚性ODE系统时,通常会发生振荡。由于您的ODE系统似乎很僵化,因此进一步建议使用“ Radau”之类的隐式Runge-Kutta方法:
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()
给我:
答案 1 :(得分:0)
通过将雅可比矩阵提供给求解器,我终于得到了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进行插值并不能改善结果。