Assuming we have the below function to optimize for 4 parameters, we have to write the function as below, but if we want the same function with more number of parameters, we have to rewrite the function definition.
def radius (z,a0,a1,k0,k1,):
k = np.array([k0,k1,])
a = np.array([a0,a1,])
w = 1.0
phi = 0.0
rs = r0 + np.sum(a*np.sin(k*z +w*t +phi), axis=1)
return rs
The question is if this can be done easier in a more automatic way, and more intuitive than this question suggests.
example would be as following which has to be written by hand.
def radius (z,a0,a1,a2,a3,a4,a5,a6,a7,a8,a9,k0,k1,k2,k3,k4,k5,k6,k7,k8,k9,):
k = np.array([k0,k1,k2,k3,k4,k5,k6,k7,k8,k9,])
a = np.array([a0,a1,a2,a3,a4,a5,a6,a7,a8,a9,])
w = 1.0
phi = 0.0
rs = r0 + np.sum(a*np.sin(k*z +w*t +phi), axis=1)
return rs
It might be easier to construct the function as a string within a loop and use 'exec' to execute that string as a python code then use it in Scipy curve_fit:
N = 5
arg_k = ''
arg_a = ''
for i in range(N):
arg_a += 'a'+str(i)+','
arg_k += 'k'+str(i)+','
function_def = 'def radius (z,' + arg_a+arg_k+ '):'
a = 'a = np.array(['+arg_a+'])'
k = 'k = np.array(['+arg_k+'])'
indent = ' '
function_def += ('\n'+indent+ k
+'\n'+indent+ a
+'\n'+indent+'w = 1.0'
+'\n'+indent+'phi = 0.0'
+'\n'+indent+'rs = r0 + np.sum(a*np.sin(k*z +w*t +phi), axis=1)'
+'\n'+indent+'return rs')
exec(function_def)
printing the string gives the following result:
print(function_def)
def radius (z,a0,a1,a2,a3,a4,k0,k1,k2,k3,k4,):
k = np.array([k0,k1,k2,k3,k4,])
a = np.array([a0,a1,a2,a3,a4,])
w = 1.0
phi = 0.0
rs = r0 + np.sum(a*np.sin(k*z +w*t +phi), axis=1)
return rs
Then use Scipy curve_fit on the defined function:
popt, pcov = curve_fit(radius, xdata=z, ydata=r)
来源:https://stackoverflow.com/questions/58463550/using-scipy-curve-fit-with-variable-number-of-parameters-to-optimize