我试图用一个以上的吸收峰(莫斯鲍尔光谱)拟合一个洛伦兹函数,但是curve_fit函数不能正常工作,只能拟合几个峰。我怎么适应呢?
Figure: Trying to adjusting multi-Lorentzian
下面我显示我的代码。请帮帮我。
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
def mymodel_hema(x,a1,b1,c1,a2,b2,c2,a3,b3,c3,a4,b4,c4,a5,b5,c5,a6,b6,c6):
f = 160000 - (c1*a1)/(c1+(x-b1)**2) - (c2*a2)/(c2+(x-b2)**2) - (c3*a3)/(c3+(x-b3)**2) - (c4*a4)/(c4+(x-b4)**2) - (c5*a5)/(c5+(x-b5)**2) - (c6*a6)/(c6+(x-b6)**2)
return f
def main():
abre = np.loadtxt('HEMAT_1.dat')
x = np.zeros(len(abre))
y = np.zeros(len(abre))
for i in range(len(abre)):
x[i] = abre[i,0]
y[i] = abre[i,1]
popt,pcov = curve_fit(mymodel_hema, x, y,maxfev=1000000000)
我的数据-> https://drive.google.com/file/d/1LvCKNdv0oBza_TDwuyNwd29PgQv22VPA/view?usp=sharing
答案 0 :(得分:0)
此代码使用leastsq
代替curve_fit
,因为后者需要固定数量的参数。在这里,我不想这样做,因为我让代码“决定”有多少个峰。请注意,我缩放了数据以简化拟合。真正的拟合参数很容易通过缩小(和标准误差传播)来计算
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import leastsq
def lorentzian( x, x0, a, gam ):
return a * gam**2 / ( gam**2 + ( x - x0 )**2)
def multi_lorentz( x, params ):
off = params[0]
paramsRest = params[1:]
assert not ( len( paramsRest ) % 3 )
return off + sum( [ lorentzian( x, *paramsRest[ i : i+3 ] ) for i in range( 0, len( paramsRest ), 3 ) ] )
def res_multi_lorentz( params, xData, yData ):
diff = [ multi_lorentz( x, params ) - y for x, y in zip( xData, yData ) ]
return diff
xData, yData = np.loadtxt('HEMAT_1.dat', unpack=True )
yData = yData / max(yData)
generalWidth = 1
yDataLoc = yData
startValues = [ max( yData ) ]
counter = 0
while max( yDataLoc ) - min( yDataLoc ) > .1:
counter += 1
if counter > 20: ### max 20 peak...emergency break to avoid infinite loop
break
minP = np.argmin( yDataLoc )
minY = yData[ minP ]
x0 = xData[ minP ]
startValues += [ x0, minY - max( yDataLoc ), generalWidth ]
popt, ier = leastsq( res_multi_lorentz, startValues, args=( xData, yData ) )
yDataLoc = [ y - multi_lorentz( x, popt ) for x,y in zip( xData, yData ) ]
print popt
testData = [ multi_lorentz(x, popt ) for x in xData ]
fig = plt.figure()
ax = fig.add_subplot( 1, 1, 1 )
ax.plot( xData, yData )
ax.plot( xData, testData )
plt.show()
提供
[ 9.96855817e-01 4.94106598e+02 -2.82103813e-01 4.66272773e+00
2.80688160e+01 -2.72449246e-01 4.71728295e+00 1.31577189e+02
-2.29698620e-01 4.20685229e+00 4.01421993e+02 -1.85917255e-01
5.57859380e+00 2.29704607e+02 -1.47193792e-01 3.91112196e+00
3.03387957e+02 -1.37127711e-01 4.39571905e+00]
和