Pin 8
Pin 8

Reputation: 90

Python's Lmfit package not converging to a meaningful result

I'm running the code below:

import numpy as np
from lmfit import Model

def exp_model(x, ampl1=1.0, tau1=0.1):             
    exponential = ampl1*np.exp(-x/tau1)
    return exponential

x = np.array([2.496,2.528,2.56,2.592,2.624])
y = np.array([8774.52,8361.68,7923.42,7502.43,7144.11])

dec_model = Model(exp_model, nan_policy='propagate')
results = dec_model.fit(y, x=x, ampl1=y[0])

results.plot()

The result I get is

enter image description here

which means that the fit is just failing for some reason. I can't figure out why. It had worked for similar data before. Any help would be greatly appreciated.

Upvotes: 1

Views: 271

Answers (1)

Pin 8
Pin 8

Reputation: 90

It wasn't converging because the initial value for the tau1 parameter was too far away from the real value. The code below works well.

import numpy as np
from lmfit import Model

def exp_model(x, ampl1=1.0, tau1=1.0): # The initial value of tau1 was changed from 0.1 to 1.0            
    exponential = ampl1*np.exp(-x/tau1)
    return exponential

x = np.array([2.496,2.528,2.56,2.592,2.624])
y = np.array([8774.52,8361.68,7923.42,7502.43,7144.11])

dec_model = Model(exp_model, nan_policy='propagate')
results = dec_model.fit(y, x=x, ampl1=y[0])

results.plot()

enter image description here

Upvotes: 2

Related Questions