title: Chi-squared minimization in python using lmfit
body: <p>I'm trying to carry out a multi-parameter fit using python and the <code>lmfit</code> module. I've been following the example shown <a href="http://cars9.uchicago.edu/software/python/lmfit/" rel="nofollow noreferrer">here</a> as the basis for my code. As far as I understand the code, I should be able to carry out a least-squares fit, provided I properly define my objective function (gives the residuals) and supply it with the right arguments.</p>
  <p>This is my current objective function:</p>
  <pre><code># Define objective function: each data point has a different
  # objective function which is defined by the model method
  # the objective function returns the array to be minimized
  def objfunc(params,trans,sum_in,sum_out,data):
      &quot;&quot;&quot; model fit using branching ratios and resonance strength
          then subtract data &quot;&quot;&quot;
      model = fit_model(params,trans,sum_in,sum_out)
  
      return model - data
  </code></pre>
  <p>where the <code>fit_model(args*)</code> method is defined by</p>
  <pre><code>def fit_model(params,trans,sum_in,sum_out):
      &quot;&quot;&quot; model the transition based upon the input string trans
          using parameter convention for the branching &quot;&quot;&quot;
      model = []
  
      # The amplitude: technically the resonance strength term
      # here it gives the number of resonant decays
      amp = params['amp'].value
  
      # For each transition we want to retrieve the parameter values
      # for the branching ratios and evaluate the new value for
      # the fit (of that transition). The easiest way to do this is
      # to store the braching ratios with the same notation used
      # previously, and to explicity call those values using the
      # 'params.['']value' method
      for i in range(len(trans)):
  
          # Employs the termvalue() method to evalueate the branching
          # and efficiency values
          model.append( str(amp * termValue(trans[i]) + amp * termValue(sum_in[i]) - amp * termValue(sum_out[i])))
  
      return np.array(model,dtype='float64')
  </code></pre>
  <p>This gives me what I expect to get: a <code>numpy.ndarray</code> the length of my data. The problem I'm having is that when I try to minimize the chi-squared fit with</p>
  <pre><code>result = minimize(objfunc,params,args=(trans,sum_in,sum_out,data)) 
  </code></pre>
  <p>I get the error message:</p>
  <pre><code>File &quot;path/chisquare.py&quot;, line 94, in &lt;module&gt;
      result = minimize(objfunc,params,args=(trans,sum_in,sum_out,data))
    File &quot;/usr/local/lib/python2.7/dist-packages/lmfit-0.7-py2.7.egg/lmfit/minimizer.py&quot;, line 498, in minimize
      fitter.leastsq()
    File &quot;/usr/local/lib/python2.7/dist-packages/lmfit-0.7-py2.7.egg/lmfit/minimizer.py&quot;, line 369, in leastsq
      lsout = scipy_leastsq(self.__residual, self.vars, **lskws)
    File &quot;/usr/lib/python2.7/dist-packages/scipy/optimize/minpack.py&quot;, line 278, in leastsq
      raise TypeError('Improper input: N=%s must not exceed M=%s' % (n,m))
  TypeError: Improper input: N=26 must not exceed M=25
  </code></pre>
  <p>I've tried to figure out what this means from the <code>lmfit</code> source code, but it's a bit beyond my understanding. Does anyone know how I can resolve this error?</p>
  <p>Thanks</p>