I am using the following function to fit data set, function works well and responds well, but when I try to fit the data using curve fitting I get an error. I am sure it is related to *arg, because when I do not use *arg in the function and write A, B, C, D directly in the function, both function and fitting work well.
Here is the code:
bin=[4,4.5,5,5.5]
arg=['A','B','C','D']
def interpo1(x,*arg):
result=[]
for i in range(len(x)):
x2, x1=x[i,0:2]
if (x1 >= bin[0]) & (x1 <bin[1]):
if (x2>= bin[0]) & (x2<bin[1]):
f1=arg[0] + ((x1 -bin[0])/(bin[1]-bin[0]))*(arg[1]-arg[0])
f2=arg[0] + ((x2 -bin[0])/(bin[1]-bin[0]))*(arg[1]-arg[0])
kh=f2/f1
if x2>= bin[1] and x2<bin[2]:
f1=arg[0] + ((x1 -bin[0])/(bin[1]-bin[0]))*(arg[1]-arg[0])
f2=arg[1] + ((x2 -bin[1])/(bin[2]-bin[1]))*(arg[2]-arg[1])
kh=f2/f1
if x2>= bin[2] and x2<bin[3]:
f1=arg[0] + ((x1 -bin[0])/(bin[1]-bin[0]))*(arg[1]-arg[0])
f2=arg[2] + ((x2 -bin[2])/(bin[3]-bin[2]))*(arg[3]-arg[2])
kh=f2/f1
if x1>= bin[1] and x1<bin[2]:
if x2>= bin[1] and x2<bin[2]:
f1=arg[1] + ((x1 -bin[1])/(bin[2]-bin[1]))*(arg[2]-arg[1])
f2=arg[1] + ((x2 -bin[1])/(bin[2]-bin[1]))*(arg[2]-arg[1])
kh=f2/f1
if x2>= bin[2] and x2<bin[3]:
f1=arg[1] + ((x1 -bin[1])/(bin[2]-bin[1]))*(arg[2]-arg[1])
f2=arg[2] + ((x2 -bin[2])/(bin[3]-bin[2]))*(arg[3]-arg[2])
kh=f2/f1
if x1>= bin[2] and x1<bin[3]:
if x2>= bin[2] and x2<bin[3]:
f1=arg[2] + ((x1 -bin[2])/(bin[3]-bin[2]))*(arg[3]-arg[2])
f2=arg[2] + ((x2 -bin[2])/(bin[3]-bin[2]))*(arg[3]-arg[2])
kh=f2/f1
result.append(kh)
return result
popt1, pcov1 = curve_fit(interpo1,x, y, method='lm')
A,B,C,D=popt1
And here is the error that I get:
popt1, pcov1 = curve_fit(interpo1,x, y, method='lm')
File /opt/anaconda3/lib/python3.9/site-packages/scipy/optimize/minpack.py:710 in curve_fit
raise ValueError("Unable to determine number of fit parameters.")
ValueError: Unable to determine number of fit parameters.
Many thanks in advance for your help!
Following this question I added the following part, but, again, I get the same error.
def wrapper_fit_func(x, *arg):
A, B, C,D = list(arg[0]), list(arg[1]), list(arg[2]), list(arg[3])
return interpo1(x, A, B, C,D)
popt1, pcov1 = curve_fit(wrapper_fit_func,x, y, method='lm')