我如何使用scipy.optimize.curve_fit在python上拟合一个好的Lorentzian? [英] How can I fit a good Lorentzian on python using scipy.optimize.curve_fit?

查看:79
本文介绍了我如何使用scipy.optimize.curve_fit在python上拟合一个好的Lorentzian?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用一个以上吸收峰(莫斯鲍尔光谱)拟合一个洛伦兹函数,但是curve_fit函数不能正常工作,只拟合了几个峰.我怎么适应呢?

I'm trying to fit a Lorentzian function with more than one absorption peak (Mössbauer spectra), but the curve_fit function it not working properly, fitting just few peaks. How can I fit it?

Figure: Trying to adjusting multi-Lorentzian

Below I show my code. Please, help me.

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)

My data --> https://drive.google.com/file/d/1LvCKNdv0oBza_TDwuyNwd29PgQv22VPA/view?usp=sharing

解决方案

This code uses leastsq instead of curve_fit as the latter one requires a fixed number of parameters. Here I do not want this as I let the code "decide" how many peaks are there. Note that I scaled the data to simplify the fit. The true fitting parameters are calculated easily be scaling back ( and standard error propagation )

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()

Providing

[ 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]

and

这篇关于我如何使用scipy.optimize.curve_fit在python上拟合一个好的Lorentzian?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆