66
votes

According to the SciPy documentation it is possible to minimize functions with multiple variables, yet it doesn't tell how to optimize on such functions.

from scipy.optimize import minimize
from math import *

def f(c):
  return sqrt((sin(pi/2) + sin(0) + sin(c) - 2)**2 + (cos(pi/2) + cos(0) + cos(c) - 1)**2)

print minimize(f, 3.14/2 + 3.14/7)

The above code does try to minimize the function f, but for my task I need to minimize with respect to three variables.

Simply introducing a second argument and adjusting minimize accordingly yields an error (TypeError: f() takes exactly 2 arguments (1 given)).

How does minimize work when minimizing with multiple variables.

1
your code shows one scalar decision variable c. There is no indication of the other two decision variables, meaning this is not the multivariate function you want to solve. you want to optimize for three decision variables instead, for a function not shown, with each variable being scalar as well, a, b, c? Do you have an example of 2 vectors for decision variables instead? For example, x and y are two decision vectors/arrays, instead of scalars - develarist
@develarist I think it would be better to make a separate question for what you are asking. But, if you need to optimize two vectors, I would consider each element of the vector to be a variable to be optimized. So if you have two 3-element vectors, you have 6 variables to optimize. - Henrik Hansen

1 Answers

87
votes

Pack the multiple variables into a single array:

import scipy.optimize as optimize

def f(params):
    # print(params)  # <-- you'll see that params is a NumPy array
    a, b, c = params # <-- for readability you may wish to assign names to the component variables
    return a**2 + b**2 + c**2

initial_guess = [1, 1, 1]
result = optimize.minimize(f, initial_guess)
if result.success:
    fitted_params = result.x
    print(fitted_params)
else:
    raise ValueError(result.message)

yields

[ -1.66705302e-08  -1.66705302e-08  -1.66705302e-08]