title: Lorentzian scipy.optimize.leastsq fit to data fails
body: <p>Since I took a lecture on Python I wanted to use it to fit my data. Although I have been trying for a while now, I still have no idea why this is not working.</p>
  
  <h2>What I would like to do</h2>
  
  <p>Take one data-file after another from a subfolder (here called: 'Test'), transform the data a little bit and fit it with a Lorentzian function.</p>
  
  <h2>Problem description</h2>
  
  <p>When I run the code posted below, it does not fit anything and just returns my initial parameters after 4 function calls. I tried scaling the data, playing around with <code>ftol</code> and <code>maxfev</code> after checking the python documentation over and over again, but nothing improved. I also tried changing the lists to <code>numpy.arrays</code> explicitely, as well as the solution given to the question <a href="https://stackoverflow.com/q/12473406/2062965">scipy.optimize.leastsq returns best guess parameters not new best fit</a>, <code>x = x.astype(np.float64)</code>. No improvement. Strangely enough, for few selected data-files this same code worked at some point, but for the majority it never did. It can definitely be fitted, since a Levenberg-Marquard fitting routine gives reasonably good results in Origin. </p>
  
  <p>Can someone tell me what is going wrong or point out alternatives...? </p>
  
  <pre><code>import numpy,math,scipy,pylab
  from scipy.optimize import leastsq
  import glob,os
  for files in glob.glob("*.txt"):
      x=[]
      y=[]
      z=[]
      f = open(files, 'r')
      raw=f.readlines()
      f.close()
      del raw[0:8]       #delete Header
      for columns in ( raw2.strip().split() for raw2 in raw ):  #data columns
          x.append(float(columns[0]))
          y.append(float(columns[1]))
          z.append(10**(float(columns[1])*0.1)) #transform data for the fit
      def lorentz(p,x):
          return (1/(1+(x/p[0] - 1)**4*p[1]**2))*p[2]
      def errorfunc(p,x,z):
          return lorentz(p,x)-z
  
      p0=[3.,10000.,0.001]
  
      Params,cov_x,infodict,mesg,ier = leastsq(errorfunc,p0,args=(x,z),full_output=True)
      print Params
      print ier
  </code></pre>