Optimization Problems with SciPy
Optimization problems involve finding the minimum or maximum of a function, subject to constraints (if any).
$SciPy$ provides a variety of optimization algorithms through the scipy.optimize module.
The most common problem is to find the minimum of a scalar function, often referred to as unconstrained optimization.
Example Problem: Minimizing a Nonlinear Function
Problem Statement:
Suppose we want to minimize the following nonlinear function:
$$
f(x, y) = (x - 2)^2 + (y - 3)^2 + \sin(x) \cdot \cos(y)
$$
This is a two-variable function that includes both quadratic terms and trigonometric components.
The goal is to find the values of $( x )$ and $( y )$ that minimize $( f(x, y) )$.
Steps to Solve:
- Define the Objective Function: We need to define the function $( f(x, y) $) in $Python$.
- Use SciPy’s
minimizeFunction: $SciPy$’sminimizefunction can be used to find the minimum of a multivariable function. - Provide an Initial Guess: The optimization algorithm needs an initial guess for $( x )$ and $( y )$ from where it will start searching for the minimum.
- Analyze the Results: We’ll check the values of $( x )$ and $( y )$ that minimize the function.
Implementation in Python:
1 | import numpy as np |
Explanation of the Code:
Defining the Objective Function:
- The function $( f(x, y) = (x - 2)^2 + (y - 3)^2 + \sin(x) \cdot \cos(y) )$ is defined using $Python$’s $NumPy$ library for mathematical operations.
- This function represents the objective we want to minimize.
The quadratic terms $((x - 2)^2)$ and $((y - 3)^2)$ will pull $(x)$ towards $2$ and $(y)$ towards $3$, while the sinusoidal part adds complexity to the surface of the function.
Initial Guess:
- The optimization algorithm needs a starting point.
In this case, we choose an initial guess of $(x = 0)$ and $(y = 0)$. - The algorithm will use this point to begin searching for the minimum of the function.
- The optimization algorithm needs a starting point.
Minimizing the Function:
- We use the
minimizefunction fromscipy.optimize, which applies a general-purpose optimization algorithm (by default, it uses the BFGS algorithm, which is gradient-based). - The result of
minimizecontains several important details, such as the optimized values of $( x )$ and $( y )$, and the minimum value of the function.
- We use the
Output:
- The optimized values of $( x )$ and $( y )$ are printed, along with the corresponding minimum value of the function.
Output:
1 | Optimal values: x = 1.8587, y = 3.0458 |
Analysis of the Results:
- The function achieves its minimum at approximately $( x = 1.8587 )$ and $( y = 3.0458 )$.
- The minimum value of the function at this point is $( f(x, y) = -0.9324 )$.
Visualization (Optional):
We can visualize the surface of the function $( f(x, y) )$ to better understand where the minimum occurs.
Here’s how you can plot the function and highlight the minimum point:
1 | import matplotlib.pyplot as plt |
Output:

Key Concepts:
Objective Function: This is the function we are trying to minimize.
In this example, the objective function is nonlinear and consists of both quadratic and sinusoidal terms.minimizeFunction: This function in $SciPy$ is a general-purpose optimizer for minimizing scalar functions of one or more variables.
It supports various methods, such as:- BFGS: A quasi-Newton method (used by default) that is efficient for smooth, continuous functions.
- Nelder-Mead: A derivative-free simplex algorithm, useful for nonsmooth functions.
Initial Guess: The optimization process starts from an initial guess, and different guesses may lead to different solutions, especially for nonlinear functions with multiple local minima.
Global vs. Local Minima: For complex functions, the
minimizefunction may find a local minimum instead of the global minimum, depending on the starting point and the shape of the function.
Conclusion:
This example demonstrates how to use $SciPy$’s minimize function to solve an optimization problem involving a nonlinear function.
The function is minimized by adjusting the variables $( x )$ and $( y )$, and the algorithm efficiently finds the point where the function reaches its lowest value.
This process is fundamental in many fields, including machine learning, economics, and engineering, where optimization problems arise frequently.




