I have a function as shown below
def PPM(x,a,b,c,d): # d is the fixed parameter
if x>d:
return 100/(1+((x-d)/c)**(-a))**b
else: return 0
the parameter d is fixed(calculated with lab data), and I need to curve fit to get all other parameters.
I am now using lmfit because I can easily fix parameter d.
from lmfit import minimize, Parameters
def residual(params, x, data):
d = params['d']
a = params['a']
b = params['b']
c = params['c']
if x>d:
PP = 100/(1+((x-d)/c)**(-a))**b
else: PP = 0
return data-PP
but it shows ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all(), I know it is related to vectorize the function with array but no idea how to do that in lmfit
I also modified the code above a little bit by doing
def residual(params, x, data):
d = params['d'] #the value of d is 0.078528, the fixed parameter
a = params['a']
b = params['b']
c = params['c']
PP = 100/(1+((x-d)/c)**(-a))**b
PP[np.where(d>x)] = 0
return data-PP
params = Parameters()
params.add('d', value=0.078528, vary=False)
params.add('a', value=0.25, min=0, max=1)
params.add('b', value=5, min=0.5, max=500)
params.add('c', value=0.001, min=0.0001, max=1)
out = minimize(residual, params, args=(x, data))
After modifying the code, there is no warning (error) after running but the fitted data looks terrible. It seems whatever the initial values I assigned, none have been changed. (I guess because if x is less than d, the function has no value) By the way I use minimize from lmfit to leastsquare the residual Is there a way to do it in python? Data and Fitting in excel the picture is what I expected And the data
x = [6.2e-5, 1.2e-4, 2.5e-4, 5.0e-4, 1.0e-3, 2.0e-3, 4.2e-3, 8.8e-3, 2.0e-2,
4.6e-2, 1.1e-1, 2.6e-1, 6.0e-1]
Data = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0.056, 0.478, 3.725, 7.800]
Thanks!
Regards,
Steve
