我试图通过使用 lmfit.Model 拟合实验数据来确定系数。
当我运行我的代码时,我收到以下错误:
Traceback (most recent call last):
File "C:\***.py", line 82, in <module>
Dc = fit_Dc(t_days, prod, PV, Swi, l, r)
File "C:\***.py", line 63, in fit_Dc
result = fmodel.fit(Son, params, t)
File "C:\***\Anaconda3\lib\site-packages\lmfit\model.py", line 991, in fit
var_data = kwargs[var]
KeyError: 't_days'
功能是:
我试图修复参数 l&r 因为它们是已知的。t(时间)对应于我的 x 数据,儿子是我想要拟合模型的 Y 数据。
我的代码是:
t_days = np.array([0, 2.3, 4.33, 6.83])
prod = np.array([0, 3, 3.1, 3.3])
PV = 11.2
Swi = 0.1303
def calc_So(t_days, PV, prod, Swi):
t = t_days*24
Sw = Swi + prod/PV
So = 1 - Sw
Soi = 1 - Swi
recovery = prod/Soi/PV
Son = (So - So[-1])/(Soi - So[-1])
return recovery, Soi, Sw, So, Son, t
def calc_So_n(t, l, r, Dc):
c_cyl = []
#calc C_ps
for n in range(0, 1200):
c_psi = ((8/(2*n+1)**2)/np.pi**2)*np.exp(-Dc*((2*n+1)**2*np.pi**2)*t/4/l**2)
c_ps.append(c_psi)
c_ps = np.asarray(c_ps, dtype = np.float64)
return np.sum(c_ps, axis = None)
#calc c_cyl
for n in range(0, 1200):
c_cyli = (4/((jn_zeros(0,n)[n-1])**2))*np.exp(-Dc*t*(jn_zeros(0,n)[n-1]/r)**2)
c_cyl.append(c_cyli)
c_cyl = np.asarray(c_cyl, dtype = np.float64)
return np.sum(c_cyl, axis = None)
return c_cyl*c_ps
# fit DC ti experimental data using lmfit model
def fit_Dc(t_days, prod, PV, Swi, l, r):
recovery, Soi, Sw, So, Son, t = calc_So(t_days, PV, prod, Swi)
# create model
fmodel = Model(calc_So_n)
# giving initial values for parameters
params = fmodel.make_params(r=r, l=l, Dc = 0)
# fix l & r:
params['r'].vary = False
params['l'].vary = False
# fit parameters to data with constant values of r, l:
params['r'].value = r
params['l'].value = l
result = fmodel.fit(Son, params, t= t)
print("Dc, Dc_error, chi-square=%f" % (result.params['Dc'].value,
result.params['Dc'].stderr,
result.chisqr))
return result.params['Dc'].value
recovery, Soi, Sw, So, Son, t = calc_So(t_days, PV, prod, Swi)
Dc = fit_Dc(t, prod, PV, Swi, l, r)
t
是一个 np 数组以及Son