title: Numerical solving of overdetermined, nonlinear equation system in Python
body: <p>Here's a minimal example of my problem - solved with scipy.optimize.leastsq</p>
  
  <pre><code>from scipy.optimize import leastsq
  from numpy import array, exp, sin, cos
  
  def MatrixFun(x, *par):
      a, b, c, d = par
      m11 = a*sin(x[0])+b*cos(x[1])
      m12 = c*cos(x[0])*sin(x[1])
      m21 = c*sin(x[0])/cos(x[1])
      m22 = d*exp(x[0]*x[1])
      M = array([[m11, m12], [m21, m22]])
      return M
  
  def Residualvector(x, parameters):
      MatrixAim = MatrixFun([-1 , 1], *parameters)
      return (MatrixFun(x, *parameters)-MatrixAim).flatten()
  
  parameters = [1, 2, 3, 4]
  start = [0, 0]
  print(leastsq(Residualvector, start, args=parameters))
  </code></pre>
  
  <blockquote>
    <p>Problems:</p>
    
    <ul>
    <li>Requires good starting point</li>
    <li>Does not converge to desired values with my real systems</li>
    <li>I need constraints for x</li>
    </ul>
  </blockquote>
  
  <p>Here's my bruteforce solution for the example problem  </p>
  
  <pre><code>from numpy import ones, array, arange, exp, sin, cos, sum, abs, argmin
  from itertools import product as iterprod
  
  def MatrixFun(x, *par):
      a, b, c, d = par
      m11 = a*sin(x[0])+b*cos(x[1])
      m12 = c*cos(x[0])*sin(x[1])
      m21 = c*sin(x[0])/cos(x[1])
      m22 = d*exp(x[0]*x[1])
      M = array([[m11, m12], [m21, m22]])
      return M  
  
  def ResidualMatrix(x, parameters):
      MatrixAim = MatrixFun([-1 , 1], *parameters)
      return MatrixFun(x, *parameters)-MatrixAim
  
  def MyBruteMatrixMinimizer(ResidualMatrix, ranges, args=()):
      pathongrid = list(iterprod(*ranges))
      pathlength = len(pathongrid)
      MatSum = ones(pathlength)
      for i in range(pathlength):
          MatSum[i] = sum(abs(ResidualMatrix(pathongrid[i], args)))
      pathgoal = pathongrid[argmin(MatSum)]
      return pathgoal
  
  parameters = [1, 2, 3, 4]
  ranges = [arange(-2,0,1e-2), arange(0,2,1e-2)]
  
  print(MyBruteMatrixMinimizer(ResidualMatrix, ranges, args=parameters))
  </code></pre>
  
  <blockquote>
    <p>Problems:</p>
    
    <ul>
    <li>Slow</li>
    <li>Stability unclear</li>
    </ul>
  </blockquote>
  
  <p>I would rather be using scipy.optimize.brute or scipy.optimize.basinhopping which both lead to the error <code>TypeError: fsolve: there is a mismatch between the input and output shape of the 'func' argument 'F'</code>. This is clear as my matrix has more equations than variables (overdetermined).<br>
  The only idea I have so far is to sum up the absolut values of as many equations as necessary to reduce the output shape size - but I am absolutely not happy with that.<br>
  I would be very gratefull for alternate or improved solutions or any other advice.</p>