scipy odeint 用于 ODE 系统时出错
Error in scipy odeint use for system of ODE's
我正在尝试重现下面显示的两个图表,它们是作为时间函数的温度和浓度曲线。我已经检查了我的方法和代码一百万次,但似乎找不到其中的错误,但我无法重现这些图表。除 CA 和 T 外,所有值都是常数。这可能是 scipy 的 odeint 精度的问题吗?任何帮助将非常感激!
两式如下:
dCA/dt = q*(CAi - CA)/V - k*CA
dT/dt = w*(Ti - T)/(Vp) + d_HRkCA/(pC) + UA*(Tc - T)/(VpC)
密码是:
import numpy as np
from scipy.integrate import odeint
import matplotlib.pyplot as plt
def ODESolve(y, t, q, CAi, V, k0, w, Ti, p, dH_R, C, UA, Tc):
CA, T = y
k = k0*np.exp(8750*1/T)
dydt = [q*(CAi - CA)/V - k*CA, w*(Ti - T)/(V*p) + \
dH_R*k*CA/(p*C) + UA*(Tc - T)/(V*p*C)]
return dydt
q = 100
CAi = 1.0
V = 100
p = 1000
C = .239
dH_R = 5*(10**4)
k0 = 7.2*(10**10)
UA = 5*10**4
CA0 = .5
T0 = 350
Ti = T0
w = p*q
y0 = [CA0, T0]
t = np.linspace(0, 20, 100)
Tc = 305
sol1 = odeint(ODESolve, y0, t, args = (q, CAi, V, k0, w, Ti, p, dH_R, C, UA, Tc))
Tc = 300
sol2 = odeint(ODESolve, y0, t, args = (q, CAi, V, k0, w, Ti, p, dH_R, C, UA, Tc))
Tc = 290
sol3 = odeint(ODESolve, y0, t, args = (q, CAi, V, k0, w, Ti, p, dH_R, C, UA, Tc))
plt.figure(1)
plt.plot(t, sol1[:,0], label = 'Tc = 305')
plt.plot(t, sol2[:,0], label = 'Tc = 300')
plt.plot(t, sol3[:,0], label = 'Tc = 290')
plt.ylim(ymax = 1, ymin = 0)
plt.title ('CA(t)')
plt.legend(loc = 'best')
plt.figure(2)
plt.plot(t, sol1[:,1], label = 'Tc = 305')
plt.plot(t, sol2[:,1], label = 'Tc = 300')
plt.plot(t, sol3[:,1], label = 'Tc = 290')
plt.ylim(ymax = 450, ymin = 300)
plt.legend(loc = 'best')
plt.title ('T(t)')
plt.show()
这是图表应该产生的结果:
下面是我上面的代码的输出:
显然公式k = k0*np.exp(8750*1/T)
中存在符号错误。如果将其更改为 k = k0*np.exp(-8750*1/T)
,您将获得与预期相同的图。
我正在尝试重现下面显示的两个图表,它们是作为时间函数的温度和浓度曲线。我已经检查了我的方法和代码一百万次,但似乎找不到其中的错误,但我无法重现这些图表。除 CA 和 T 外,所有值都是常数。这可能是 scipy 的 odeint 精度的问题吗?任何帮助将非常感激!
两式如下:
dCA/dt = q*(CAi - CA)/V - k*CA
dT/dt = w*(Ti - T)/(Vp) + d_HRkCA/(pC) + UA*(Tc - T)/(VpC)
密码是:
import numpy as np
from scipy.integrate import odeint
import matplotlib.pyplot as plt
def ODESolve(y, t, q, CAi, V, k0, w, Ti, p, dH_R, C, UA, Tc):
CA, T = y
k = k0*np.exp(8750*1/T)
dydt = [q*(CAi - CA)/V - k*CA, w*(Ti - T)/(V*p) + \
dH_R*k*CA/(p*C) + UA*(Tc - T)/(V*p*C)]
return dydt
q = 100
CAi = 1.0
V = 100
p = 1000
C = .239
dH_R = 5*(10**4)
k0 = 7.2*(10**10)
UA = 5*10**4
CA0 = .5
T0 = 350
Ti = T0
w = p*q
y0 = [CA0, T0]
t = np.linspace(0, 20, 100)
Tc = 305
sol1 = odeint(ODESolve, y0, t, args = (q, CAi, V, k0, w, Ti, p, dH_R, C, UA, Tc))
Tc = 300
sol2 = odeint(ODESolve, y0, t, args = (q, CAi, V, k0, w, Ti, p, dH_R, C, UA, Tc))
Tc = 290
sol3 = odeint(ODESolve, y0, t, args = (q, CAi, V, k0, w, Ti, p, dH_R, C, UA, Tc))
plt.figure(1)
plt.plot(t, sol1[:,0], label = 'Tc = 305')
plt.plot(t, sol2[:,0], label = 'Tc = 300')
plt.plot(t, sol3[:,0], label = 'Tc = 290')
plt.ylim(ymax = 1, ymin = 0)
plt.title ('CA(t)')
plt.legend(loc = 'best')
plt.figure(2)
plt.plot(t, sol1[:,1], label = 'Tc = 305')
plt.plot(t, sol2[:,1], label = 'Tc = 300')
plt.plot(t, sol3[:,1], label = 'Tc = 290')
plt.ylim(ymax = 450, ymin = 300)
plt.legend(loc = 'best')
plt.title ('T(t)')
plt.show()
这是图表应该产生的结果:
下面是我上面的代码的输出:
显然公式k = k0*np.exp(8750*1/T)
中存在符号错误。如果将其更改为 k = k0*np.exp(-8750*1/T)
,您将获得与预期相同的图。