我一直在尝试创建一个函数来解决一组常微分方程,然后使用scipy.optimize.curve_fit函数将其拟合到实验数据,但是我收到一条包含以下内容的错误消息:
"ValueError: object too deep for desired array
odepack.error: Result from function call is not a proper array of floats."
非常感谢任何帮助。
import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import odeint
from pylab import *
from scipy.optimize import curve_fit
kB = 8.6173324e-5 #eV/K
P_H2O = 0.05 # bar
H_H2O = -0.8 #eV
S_H2O = -0.0016
def K_H2O(T):
return np.exp(-H_H2O/(kB*T))*np.exp(S_H2O/(kB))
def FracA(T):
return K_H2O(T)*P_H2O / (1+K_H2O(T)*P_H2O)
def FracB(T):
return 1-FracA(T)
EA1_A = 0.725 # eV (from experimental data)
def k1_A(A1_A,T):
return A1_A*np.exp(-EA1_A/(kB*T))
A1_B = 5.e3 #
EA1_B = 0.8 #eV
def k1_B(T):
return A1_B*np.exp(-EA1_B/(kB*T))
A2_B = 8.e3 #
EA2_B = 1.0 #eV
def k2_B(T):
return A2_B*np.exp(-EA2_B/(kB*T))
# initial conditions
P_NO0 = 500.e-6 # initial NO
P_NH30 = 530.e-6 # initial NH3
y0 = [P_NO0, P_NH30] # initial condition vector
t = np.linspace(0, 30., 1000) # time grid
def conv(T,A1_A):
def f(y, t):
P_NOi = y[0]
P_NH3i = y[1]
# the differential equations
if y[0] and y[1] > 0:
f0 = -k1_A(A1_A,T)*FracA(T)*P_NOi -k1_B(T)*FracB(T)*P_NOi*P_NH3i**(-0.25)
else:
f0 = 0
if y[0] and y[1] > 0:
f1 = -k1_A(A1_A,T)*FracA(T)*P_NOi -k1_B(T)*FracB(T)*P_NOi*P_NH3i**(-0.25)-k2_B(T)*P_NH3i
else:
f1 = 0
return [f0, f1]
# solve the DEs
soln = odeint(f, y0, t)
P_NO = soln[:, 0]
P_NH3 = soln[:, 1]
NO_conv = 1-P_NO[-1]/P_NO0
return NO_conv
x_real = np.array([433.1,443.1,453.2,463.1,473.1,483.7,494.2,503.5,523.9,553.7,573.6,623.4,673.4,723.3,773.4,823.2])
y_real =np.array([0.064305859, 0.098333053, 0.151494329, 0.217225336, 0.296164608, 0.397472394, 0.508515308, 0.612339428, 0.793549257, 0.892454094, 0.895511489, 0.861625527, 0.949118344, 0.940025727, 0.852439418, 0.727332885])
popt, pcov = curve_fit(conv, x_real, y_real)