我定义了一个可以正常模拟某些数据的函数。 对于物理学家来说:该函数应该在 NMR 数据中生成低于超导转变的 Hebel-Slichter 相干峰。
我现在想使用 lmfit 将函数拟合到我的数据。 我收到一条错误消息,这似乎是因为我在函数的Integrate.quad 部分中使用了附加参数(参数)。我对 python 确实不太熟练。可以让它工作吗?
代码有点混乱,因为我有一个部分(已注释掉)仅用于绘制数据和模拟。代码和错误消息如下所示:
import numpy as np
import matplotlib.pyplot as plt
import scipy.integrate as integrate
#import scipy.special as special
#from scipy.optimize import curve_fit
from lmfit import Model
#The function that describes the change in the DOS around the Fermi-level as a function of temperature below Tc
def T1Tfunc(En, Temperature , Gamma0 , Nfactor, Gap2 , Tc):
kB = 8.617E-5
Delta0 = kB * Tc * Gap2 / 2
Delta1 = Delta0 * np.tanh(((Tc / Temperature)-1) ** 0.5)
Gamma1 = Gamma0 * (Temperature / Tc) ** Nfactor
Enp = En + 8.974E-6
EnB = En + Gamma1 * 1j
EnBp = Enp + Gamma1 * 1j
Ns = (EnB / np.sqrt(EnB * EnB - Delta1 * Delta1))
Nsp = (EnBp / np.sqrt(EnBp * EnBp - Delta1 * Delta1))
Ms = (Delta1 / np.sqrt(EnB * EnB - Delta1 * Delta1))
Msp = (Delta1 / np.sqrt(EnBp * EnBp - Delta1 * Delta1))
FE = 1/(1 + np.exp(En/(kB*Temperature)))
FEp = 1/(1 + np.exp(Enp/(kB*Temperature)))
func = (np.real(Ns)*np.real(Nsp)+np.real(Ms)*np.real(Msp))*FE*(1-FEp)
return func
#The integration of the DOS, and resulting spin-lattice relaxation rate
def T1T(Temperature , Gamma0 , Nfactor , Gap2 , Tc , Koringaa , Koringab):
kB = 8.61728E-5
# Integration parameters
aint = 0
bint = 0.1
I = integrate.quad(T1Tfunc, aint, bint, args=(Temperature , Gamma0 , Nfactor , Gap2 , Tc))[0]
#For Korringa
return I*(2/(kB*Temperature)) * (Koringaa + Koringab * Temperature)
#For No-Korringa
# return I*(2/(kB*Temperature)) * (Koringaa + Koringab * Tc)
T1T = np.vectorize(T1T)
#Some if functions for when the calculation values end up too small, and for above Tc
def T1Textended(Temperature , Gamma0 , Nfactor, Gap2 , Tc , Koringaa , Koringab):
if Temperature < (0.1 * Tc):
T1Te = 0
elif Temperature < Tc:
T1Te = T1T(Temperature , Gamma0 , Nfactor , Gap2 , Tc , Koringaa , Koringab)
else:
T1Te = Koringaa + Koringab * Temperature
return T1Te
T1Textended = np.vectorize(T1Textended)
#Importing some example data to be fitted
filename = 'Rb2CsC60.txt'
data = np.loadtxt(filename, delimiter=',')
datax = data[:, 0]
datay = data[:, 1]
dataerr = data[:, 2]
#print(data)
# #For plotting a simulation resulting from the function, on top of data
# #Parameters when simulating
# Tc = 32.2
# Gamma0 = 9.56496E-4
# #Gap2 = 4.18126
# Gap2 =
# Nfactor = 1
# Koringaa = 0.02542
# Koringab = 4.24127E-4
# Temperature = np.arange((0.1 * Tc), (1.8 * Tc), (Tc / 100))
# DOST0p0 = T1Textended(Temperature , Gamma0 , Nfactor , Gap2 , Tc , Koringaa , Koringab)
# fig = plt.figure()
# ax = plt.axes()
# line1 = ax.plot(Temperature,DOST0p0)
# ax.scatter(datax,datay)
# #plt.ylim([0, 2])
# #plt.xlim([0, (2 * Tc)])
# plt.title(u"1/T_1T vs. Temperature \n \u0393\u2080 = {} eV, n = {}, T_c = {}".format(Gamma0, Nfactor, Tc))
# plt.xlabel("Temperature (K)")
# plt.ylabel("$1/(T_{1}T)$");
#Now attempting to fit the function to some data
#Define T1Textended as a function to be wrapped by the 'Model' fitting package of lmfit
HSmodel = Model(T1Textended)
#Define and set the parameters for the model, to be fitted
params = Model.Parameters()
params.add('Tc', value=32.2, vary=False)
params.add('Gamma0', value=1E-3, vary=True)
params.add('Nfactor', value=1, vary=False)
params.add('Gap2', value=4.25, vary=True)
params.add('Koringaa', value=1, vary=True)
params.add('Koringab', value=0, vary=True)
#Check that it is read properly and gives all the right input parameters and variables
print(HSmodel.param_names, HSmodel.independent_vars)
错误信息:
runfile('C:/Users/Ross Colman/Qsync/Hebel-Slichter/DOS_simulation/HS_fit.py', wdir='C:/Users/Ross Colman/Qsync/Hebel-Slichter/DOS_simulation') 回溯(最近一次调用最后一次):
文件“C:\Users\Ross Colman\Qsync\Hebel-Slichter\DOS_simulation\HS_fit.py”,第 111 行,位于 HSmodel = 模型(T1Textded)
文件“C:\ProgramData\Anaconda3\lib\site-packages\lmfit\model.py”,第 277 行,位于 init self._parse_params()
文件“C:\ProgramData\Anaconda3\lib\site-packages\lmfit\model.py”,第 489 行,在 _parse_params 中 引发 ValueError(f"不支持 varargs '*{fnam}'")
ValueError:不支持可变参数'*args'
如有任何帮助,我们将不胜感激
lmfit.Model()
无法包装由 numpy.vectorize()
“矢量化”的函数。