title: function of two variables reduced to 1 variable but issues with minimizing in Python
body: <p>I have a function which is multivariate (code below), depending on <code>x1</code> and <code>x2</code> and I have a target output value in mind for this function, named <code>target</code>. I fix <code>x1</code> and provide my <code>target</code> in the outer function: <code>find_x2</code>. The objective is to minimize the inner function <code>error</code>, which is just the squared distance between my output target and the resulting value <code>est</code>, with respect to <code>x2</code>.</p>
  <p>But having run it:</p>
  <p><code>find_x2(target = .12, x1 = 100)</code> yields <code>x2 = 0.99899</code>, but it should be roughly <code>.80</code>.</p>
  <p>It appears the function is minimizing with respect to <code>est</code> instead of <code>x2</code>, but I can't see why. Any help would be appreciated.</p>
  <p>Code:</p>
  <pre><code>import numpy as np
  from scipy.optimize import minimize_scalar, minimize 
  
  def find_x2(x1, target): 
      def error(x2, x1 = x1, target = target):
          k = np.zeros(x1) 
          V = np.zeros(x1) 
          for i in range(x1): 
              try:
                  k[i-1] = (i/x1)**(-1/x2)
              except: 
                  k[x1-1]=1
          for i in range(x1): 
              V[i] = k[i]/sum(k)
          est = sum(V**2) 
          return (target - est)**2
      return minimize_scalar(error, bounds = (.001,.999), method = 'bounded') 
  </code></pre>