title: ValueError: The user-provided objective function must return a scalar value" on scalar-valued function
body: <p>I'm having a problem understanding why my code continues to return a value error when the function is sure to always provide a scalar value. The function values after each run seem to only contain scalar values or Sympy constants.</p>
  <p>This is my code:</p>
  <pre><code>from sympy import Symbol
  from sympy.stats import Normal, density, sample_iter
  from scipy.optimize import minimize
  from sympy import Sum, binomial, exp, simplify
  def my_func_v(args):
      print(args)
      N = Normal(&quot;N&quot;, args[0], args[1])
      y = Symbol(&quot;y&quot;, real = True)
      samples = list(sample_iter(N, numsamples=200))
      sampleSum = sum(samples)
      print(&quot;Gotten samples&quot;)
      func = simplify((Sum(sum([simplify(binomial(20,y)*(1/(1+exp(-sample)))**y*(exp(-sample)/(1+exp(-sample)))**(20-y)*density(N)(sample))/sampleSum for sample in samples]), (y, 0, 10)).doit() - 0.8)**2)
      print(f&quot;Done with func with end value: {func}&quot;)
      return func
  print(&quot;Now optimizing&quot;)
  results = minimize(my_func_v, [1,1], method='BFGS', options={&quot;disp&quot;: True, &quot;return_all&quot;: True})
  </code></pre>
  <p>This is the return output
  <a href="https://i.stack.imgur.com/EF34K.png" rel="nofollow noreferrer">Output from running code</a></p>