You can wrap func
in a lambda, as follows:
def func(x,a,b):
return a*x*x + b
for b in xrange(10):
popt,pcov = curve_fit(lambda x, a: func(x, a, b), x1, x2)
A lambda is an anonymous function, which in Python can only be used for simple one line functions. Basically, it's normally used to reduce the amount of code when don't need to assign a name to the function. A more detailed description is given in the official documentation: http://docs.python.org/tutorial/controlflow.html#lambda-forms
In this case, a lambda is used to fix one of the arguments of func
. The newly created function accepts only two arguments: x
and a
, whereas b
is fixed to the value taken from the local b
variable. This new function is then passed into curve_fit
as an argument.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…