title: Minimize objective function using limfit.minimize in Python
body: <p>I am having a problem with package <code>lmfit.minimize</code> minimization procedure. Actually, I could not create a correct objective function for my problem.</p>
  
  <p><strong>Problem definition</strong></p>
  
  <ul>
  <li>My function: <code>yn = a_11*x1**2 + a_12*x2**2 + ... + a_m*xn**2</code>,where <code>xn</code>- unknowns, <code>a_m</code> - 
                coefficients. n = 1..N, m = 1..M</li>
  <li>In my case, <code>N=5</code> for  <code>x1,..,x5</code> and <code>M=3</code> for <code>y1, y2, y3</code>.</li>
  </ul>
  
  <p>I need to find the <code>optimum: x1, x2,...,x5</code> so that it can satisfy the <code>y</code></p>
  
  <p><strong>My question:</strong></p>
  
  <ul>
  <li>Error: <code>ValueError: operands could not be broadcast together with shapes (3,) (3,5)</code>.</li>
  <li>Did I create the objective function of my problem properly in Python?</li>
  </ul>
  
  <p><strong>My code:</strong></p>
  
  <pre><code>import numpy as np
  from lmfit import Parameters, minimize
  
  def func(x,a):
      return np.dot(a, x**2)
  
  def residual(pars, a, y):
      vals = pars.valuesdict()
      x = vals['x']
      model = func(x,a)
      return y - model
  
  def main():
      # simple one: a(M,N) = a(3,5)
      a = np.array([ [ 0, 0, 1, 1, 1 ],
                     [ 1, 0, 1, 0, 1 ],
                     [ 0, 1, 0, 1, 0 ] ])
      # true values of x
      x_true = np.array([10, 13, 5, 8, 40])
      # data without noise
      y = func(x_true,a)
  
      #************************************
      # Apriori x0
      x0 = np.array([2, 3, 1, 4, 20])
      fit_params = Parameters()
      fit_params.add('x', value=x0)
  
      out = minimize(residual, fit_params, args=(a, y))
      print out
  if __name__ == '__main__':
      main()
  </code></pre>