2

I want to minimize 2-dimenshional function and have such Python code:

def f(x,y): 
    return (x-1.0)**2 + (y-2.0)**2

res = minimize(f, x0 = [0.0,0.0], bounds = ((-5,5),(-5,5)), method = 'L-BFGS-B')

And it doesn't work, because of such error (this is the last line of the error):

 TypeError: f() missing 1 required positional argument: 'y'

What does it mean and how can I fix it?

Nick ODell
  • 15,465
  • 3
  • 32
  • 66
K. Kovalev
  • 925
  • 1
  • 6
  • 6

1 Answers1

6

If you want to optimize a multidimensional function with scipy.optimize.minimize, you need to express it as a function that takes an array:

res = minimize(lambda x: f(*x), x0=...)
user2357112
  • 260,549
  • 28
  • 431
  • 505