6

There is a statement in the manual of curve_fit that

The model function, f(x, ...). It must take the independent variable as the first argument and the parameters to fit as separate remaining arguments.

However, I would like to use as a model function a method of the class which is defined as:

def model_fun(self,x,par):

So, the first argument is not an independent variable, as you can see. Is there any way how I can use the method of a class as a model function for curve_fit

freude
  • 3,632
  • 3
  • 32
  • 51

1 Answers1

5

Sure, create an instance and pass its bound method:

class MyClass(object):
   ...
   def model_fun(self,x,par): ...

obj = MyClass(...)
curve_fit(obj.model_fun, ...)

You can find a good explanation about bound/unbound/etc. in this question.

Community
  • 1
  • 1
shx2
  • 61,779
  • 13
  • 130
  • 153