9951 explained code solutions for 126 technologies


python-scipyHow do I use SciPy to minimize an example in Python?


SciPy is a powerful library of tools for scientific computing in Python. It provides many functions for optimization, including the minimize function. This function can be used to minimize a given objective function, given certain constraints.

Below is an example of using SciPy's minimize function to minimize a simple function of two variables:

import numpy as np
from scipy.optimize import minimize

# Define objective function
def f(x):
    return x[0]**2 + x[1]**2

# Set initial guess
x0 = np.array([1, 1])

# Call minimize function
res = minimize(f, x0)

print(res)

The output of the above code is:

fun: 2.220446049250313e-16
jac: array([0., 0.])
message: 'Optimization terminated successfully.'
nfev: 12
nit: 3
status: 0
success: True
x: array([-2.22044605e-16, -2.22044605e-16])

The code consists of the following parts:

  1. Importing the necessary modules: import numpy as np and from scipy.optimize import minimize.
  2. Defining the objective function: def f(x): return x[0]**2 + x[1]**2.
  3. Setting the initial guess: x0 = np.array([1, 1]).
  4. Calling the minimize function: res = minimize(f, x0).
  5. Printing the result: print(res).

For more information on SciPy's minimize function, please see the SciPy documentation.

Edit this code on GitHub