Trouble With Curve Fitting - Lmfit Won't Produce Proper Fit To Peak Data
I'm quite new to python and the lmfit model and having some trouble. I want to fit a peak function (something like Gaussian or Voigtian profil) to my experimental data, but it neve
Solution 1:
A PseudoVoigt function (or Voigt or Gaussian or Lorentzian) goes to 0 at +/- infinity. Your data looks to go to ~1.0, with a dip around x=50.
You almost certainly want to add either a linear or constant component to the model. For a linear component, try:
mod = PseudoVoigtModel()
pars = mod.guess(y, x=x)
mod = mod + LinearModel()
pars.add('intercept', value=1, vary=True)
pars.add('slope', value=0, vary=True)
out = mod.fit(y, pars, x=x)
print(out.fit_report(min_correl=0.25))
or for a constant, try:
mod = PseudoVoigtModel()
pars = mod.guess(y, x=x)
mod = mod + ConstantModel()
pars.add('c', value=1, vary=True)
out = mod.fit(y, pars, x=x)
print(out.fit_report(min_correl=0.25))
as a better model for this data.
Also, to get better initial values for the parameters, you might try:
mod = PseudoVoigtModel()
pars = mod.guess((1-y), x=x) # Note '1-y'
so that the curve being used for initial values is more like a positive peak. Of course, the sign of the amplitude will be wrong, but its magnitude will be close, and the starting center and width will be close to correct. That should make the fit more robust.
Post a Comment for "Trouble With Curve Fitting - Lmfit Won't Produce Proper Fit To Peak Data"