So I've been attempting to create a function during runtime, which should dynamically adds pairs of parameters. To give an idea of what I'm looking for, here is what I've made so far :
def smart_func(terms):
params = []
for n in range(terms):
params.append((2*n*np.pi, 2*n*np.pi))
def func(t, freq, offset, *params):
result = 0
for (a,b) in zip(params):
result += np.sin(a*freq*t) + np.cos(b*freq*t)
return result
return func
I know this does not work but should give some idea of what I'm attempting to do. I've looked at this question but still have been unable to come up with a solution.
To give a little bit more explanation, i then need to pass this newly created function into this
from scipy.optimize import curve_fit
f_vars, f_cov = curve_fit(smart_func(terms=3), time_in_hours, full_fit_flux, p0=p0)
Which will allow me to easily determine the fewest amount of parameters needed to adequetly fit my data.
This is a hard coded function that I've used successfully. If smart_func had a 3 passed to it, it would return this function.
def func(t, freq, offset, a0, b0, a1, b1, a2, b2):
return b0 + a0 \
+ a1*np.sin(2.*np.pi*freq*t) \
+ b1*np.cos(2.*np.pi*freq*t) \
+ a2*np.sin(4*np.pi*freq*t) \
+ b2*np.cos(4*np.pi*freq*t) \
+ offset
This is what it would be if smart_func had a 2 passed to it
def func(t, freq, offset, a0, b0, a1, b1):
return b0 + a0 \
+ a1*np.sin(2.*np.pi*freq*t) \
+ b1*np.cos(2.*np.pi*freq*t) \
+ offset
What I want is something where an additional a and b term are added depending on how many terms are specified.