title: How do I write a multivariate objective function for use with scipy.optimize.minimize() so that it can also be plotted with matplotlib?
body: <p>I have a function for which I would like to find a global maximum. Written as a python function, it looks like this:</p>
  
  <pre><code>def fh(x,sign=1.0):
      x1 = x[0,:]
      x2 = x[1,:]
      out = (np.sin(x1 - x2/8)**2 + np.sin(x2 + x1/8)**2)/(np.sqrt((x1 - 8.6998)**2 + (x2 - 6.7665)**2) + 1)
      return out
  </code></pre>
  
  <p>It was written (with the help of <a href="https://stackoverflow.com/a/27043124/2531042">this answer</a>) such that it is easy to plot using the following code:</p>
  
  <pre><code>import numpy as np
  import matplotlib.pyplot as mplot
  import matplotlib.cm as cm
  from mpl_toolkits.mplot3d import Axes3D as m3d
  
  x = np.linspace(-20,30,num=250)
  y = x
  dim = np.size(x)
  
  xx = np.zeros((2,dim,dim))
  
  xx[0],xx[1] = np.meshgrid(x,y)
  
  zz = fh(xx)
  
  fig = mplot.figure()
  ax = fig.add_subplot(111, projection='3d')
  
  ax.plot_surface(xx[0,:],xx[1,:],zz,cmap=cm.coolwarm)
  </code></pre>
  
  <p>The trouble is that <code>scipy.optimize.minimize()</code> can't work with this function, because it passes a vector with the shape <code>(n,)</code> to it. The result is an error:</p>
  
  <pre><code>&lt;ipython-input-25-2e1bca04325c&gt; in fh(x, sign)
        2 
        3 def fh(x,sign=1.0):
  ----&gt; 4     x1 = x[0,:]
        5     x2 = x[1,:]
        6     out = (np.sin(x1 - x2/8)**2 + np.sin(x2 + x1/8)**2)/(np.sqrt((x1 - 8.6998)**2 + (x2 - 6.7665)**2) + 1)
  
  IndexError: too many indices
  </code></pre>
  
  <p>One way to approach this problem is to use a conditional in the objective function to check the size of the input before referencing it, but this strikes me as ugly and not very efficient.</p>
  
  <p>Is there another way to do it?</p>