1

I am new to Python and I am trying to fit a complex function. What I have is:

def decay(x,A,d,f,p,c):
    return A*numpy.exp(1j*cmath.pi*f*x-d*x+1j*p)+c
    guess=[0.01,0.01,0.01,0,0]
    onFit, var_matrix=scipy.optimize.curve_fit(decay,x,y,guess)

x is real and y is complex. The syntax should work for real function but it seems that it does not work for the complex case.

Is there a simpler way to achieve this task?

Srivatsan
  • 9,225
  • 13
  • 58
  • 83
Glostas
  • 1,090
  • 2
  • 11
  • 21
  • 1
    There are a number of posts about this, have a look [here](http://stackoverflow.com/questions/23424394/fitting-complex-model-using-python-and-lmfit) and [here](http://stackoverflow.com/questions/14296790/python-scipy-leastsq-fit-with-complex-numbers). `scipy.optimize.curve_fit()` is a least squares fitter under the hood so it will have problems with complex numbers. – DrBwts Jul 30 '15 at 11:39
  • Thanks, link #2 helped me to work it out. So there is no complex fit function in scipy? Would be a nicer code without an additional helper function... – Glostas Jul 30 '15 at 11:58
  • 1
    Have a look at [LMFIT](http://lmfit.github.io/lmfit-py/) and [LMFIT for complex data](http://lmfit.github.io/lmfit-py/faq.html#how-can-i-fit-complex-data), it maybe what you are looking for. – DrBwts Jul 30 '15 at 12:51
  • 2
    I added an example for [lmfit here](http://stackoverflow.com/questions/31408782/scipy-curve-fitting-negative-value/31417030#31417030). – Cleb Jul 31 '15 at 15:37

0 Answers0