【发布时间】:2019-12-23 06:13:01
【问题描述】:
我正在尝试求解耦合的一阶 ODE 系统:
这里的 Tf 被认为是常数,Q(t) 是给定的。 Q(t) 的图如下所示。用于创建时间与 Q 图的数据文件可在here 获得。
对于给定的 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 耦合系统?
【问题讨论】:
-
您对
q的近似值可能是问题所在。我建议为此创建一个interpolation function。 -
我没有在我的示例中展示它,但我尝试使用
interp1d表示 q 但它对结果中的波动没有帮助。
标签: python scipy ode discrete-mathematics