title: least square fitting to complex numbers in python with errors
body: <p>Apologies here is the code.
  I am trying to fit a Bessel function to some 2D data, the Bessel function is complex. However when I fit the data I always end up with an array error, which I cannot sort ( I am new to python). In the end the code would ideally give me the error to account for the scatter of the imported data.</p>
  
  <pre><code>from numpy import *
  from pylab import *
  from scitools.std import *
  import csv
  import matplotlib.pyplot as plt
  import scipy.special
  from scipy.optimize import curve_fit
  
  
  
  xdata=np.array([7.6,22.6,47.6,97.6,147.6,200,300,400,500,600,700,800,900,1000,1100,1200,1300])
  ydata=np.array([1.19E-05,1.05E-05,1.06E-05,9.92E-06,9.85E-06,9.88E-06,9.45E-06,9.77E-06,9.59E-06,1.09E-05,1.22E-05,1.39E-05,1.78E-05,2.04E-05,2.61E-05,3.17E-05,3.86E-05])
  
  
  
  
  
  b = 1.63854196884e+39
  def func(x,p1,p2):
      return p2*(sqrt((scipy.special.jn(1,(1j*sqrt((8*b*p1**3*(x/10000))))))/(1j*sqrt((2*b*p1**3*(x/10000))))))
  
  
  popt, pcov = curve_fit(func, xdata, ydata,p0=(1.0,0.2)) 
  </code></pre>
  
  <p>This code is meant to show the rough problem, but in the end I want the value of p1 and p2 plus the error.</p>
  
  <p>the error I get back is this, ( I know its basic but I keep messing up )</p>
  
  <hr>
  
  <p>TypeError                                 Traceback (most recent call last)</p>
  
  <h2>TypeError: array cannot be safely cast to required type</h2>
  
  <p>error                                     Traceback (most recent call last)
  /Library/Frameworks/EPD64.framework/Versions/7.1/lib/python2.7/site-packages/IPython/utils/py3compat.pyc in execfile(fname, *where)
      173             else:
      174                 filename = fname
  --> 175             <strong>builtin</strong>.execfile(filename, *where)</p>
  
  <p>/Users/sean/Cardiff/DTO/paulsen/carley report/crap.py in ()
       21 
       22 
  ---> 23 popt, pcov = curve_fit(func, xdata, ydata,p0=(1.0,0.2))
       24 
       25 </p>
  
  <p>/Library/Frameworks/EPD64.framework/Versions/7.1/lib/python2.7/site-packages/scipy/optimize/minpack.pyc in curve_fit(f, xdata, ydata, p0, sigma, absolute_sigma, **kw)
      553     # Remove full_output from kw, otherwise we're passing it in twice.</p>
  
  <pre><code>554     return_full = kw.pop('full_output', False)
  </code></pre>
  
  <p>--> 555     res = leastsq(func, p0, args=args, full_output=1, **kw)
      556     (popt, pcov, infodict, errmsg, ier) = res
      557 </p>
  
  <p>/Library/Frameworks/EPD64.framework/Versions/7.1/lib/python2.7/site-packages/scipy/optimize/minpack.pyc in leastsq(func, x0, args, Dfun, full_output, col_deriv, ftol, xtol, gtol, maxfev, epsfcn, factor, diag)
      377             maxfev = 200*(n + 1)
      378         retval = _minpack._lmdif(func, x0, args, full_output, ftol, xtol,
  --> 379                                  gtol, maxfev, epsfcn, factor, diag)
      380     else:
      381         if col_deriv:</p>
  
  <p>Any help is greatly appreciated.
  Cheers</p>