🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEscipy

scipy Documentation

LOADING ENGINE...

optimize.minimize()

AI & DATA SCIENCE // optimize-minimize

scipy.optimize.minimize() finds the input value that minimizes a given scalar function, using one of several numerical optimization algorithms.

Syntax

scipy.optimize.minimize(fun, x0, method=None)

Deep Dive Course

minimize() takes a function fun that returns a single scalar value, and an initial guess x0, then searches for the input that makes fun's output as small as possible, returning a result object with .x, the optimal input found, and .fun, the minimum value achieved there. Like root(), it can get stuck in a local minimum rather than finding the true global minimum for functions with multiple valleys, and different method choices, like Nelder-Mead, BFGS, or L-BFGS-B, trade off speed, robustness, and whether they require the function's derivative.

1Understanding optimize.minimize()

minimize() takes a function fun that returns a single scalar value, and an initial guess x0, then searches for the input that makes fun's output as small as possible, returning a result object with .x, the optimal input found, and .fun, the minimum value achieved there. Like root(), it can get stuck in a local minimum rather than finding the true global minimum for functions with multiple valleys, and different method choices, like Nelder-Mead, BFGS, or L-BFGS-B, trade off speed, robustness, and whether they require the function's derivative.

💡

minimize() only guarantees finding a local minimum near the starting guess, not necessarily the global minimum across the entire function — for functions with multiple local minima, try several different starting points, or use a global-optimization-specific method, if finding the true overall minimum matters.

editor.html
from scipy import optimize

def f(x):
    return (x - 3) ** 2 + 5

result = optimize.minimize(f, x0=0)
print(result.x, result.fun)
localhost:3000

2Practical Example

Here is a real-world application of optimize.minimize() showing how it is used in production SciPy code.

editor.html
from scipy import optimize

def f(x):
    return x[0]**2 + x[1]**2

result = optimize.minimize(f, x0=[3, 4])
print(result.x)
localhost:3000

3Best Practices

Follow these guidelines when working with optimize.minimize():

1. Try multiple different starting points for functions that might have several local minima, since minimize() can get stuck in whichever one is nearest the initial guess

2. Provide the function's gradient, via the jac parameter, when available, since many methods converge faster and more reliably with it than by estimating it numerically

3. Check result.success and inspect result.message when a minimization doesn't produce the expected result, rather than assuming it always converges correctly

⚠️

Tip: minimize() only guarantees finding a local minimum near the starting guess, not necessarily the global minimum across the entire function — for functions with multiple local minima, try several different starting points, or use a global-optimization-specific method, if finding the true overall minimum matters.

editor.html
from scipy import optimize

def f(x):
    return (x - 3) ** 2 + 5

result = optimize.minimize(f, x0=0)
print(result.x, result.fun)
localhost:3000

Examples

Example 01Basic Usage
from scipy import optimize

def f(x):
    return (x - 3) ** 2 + 5

result = optimize.minimize(f, x0=0)
print(result.x, result.fun)
Example 02Advanced Example
from scipy import optimize

def f(x):
    return x[0]**2 + x[1]**2

result = optimize.minimize(f, x0=[3, 4])
print(result.x)

Best Practices

  • Try multiple different starting points for functions that might have several local minima, since minimize() can get stuck in whichever one is nearest the initial guess
  • Provide the function's gradient, via the jac parameter, when available, since many methods converge faster and more reliably with it than by estimating it numerically
  • Check result.success and inspect result.message when a minimization doesn't produce the expected result, rather than assuming it always converges correctly

Interview Question

Why doesn't scipy.optimize.minimize() guarantee finding the global minimum of a function with multiple local minima?

Hint: Think about how the underlying algorithms actually search for a minimum.

Most of minimize()'s algorithms work by making incremental, locally-informed adjustments to the current guess, generally moving in whatever direction appears to decrease the function's value based on its local shape near that guess, similar to walking downhill from wherever you start. Once such an algorithm settles into a local minimum, a point where the function stops decreasing in every immediately explorable direction, it has no built-in mechanism to recognize that a deeper, better minimum might exist somewhere else entirely on the function's landscape, since it never explores those distant regions at all. Finding a genuine global minimum reliably requires either trying many different starting points or using a specifically global-search-oriented algorithm.

Exercises

MediumPractice using optimize.minimize() in a real scenario.
View Solution
from scipy import optimize

def f(x):
    return (x - 3) ** 2 + 5

result = optimize.minimize(f, x0=0)
print(result.x, result.fun)

Frequently Asked Questions

Why doesn't scipy.optimize.minimize() guarantee finding the global minimum of a function with multiple local minima?

Most of minimize()'s algorithms work by making incremental, locally-informed adjustments to the current guess, generally moving in whatever direction appears to decrease the function's value based on its local shape near that guess, similar to walking downhill from wherever you start. Once such an algorithm settles into a local minimum, a point where the function stops decreasing in every immediately explorable direction, it has no built-in mechanism to recognize that a deeper, better minimum might exist somewhere else entirely on the function's landscape, since it never explores those distant regions at all. Finding a genuine global minimum reliably requires either trying many different starting points or using a specifically global-search-oriented algorithm.

Related Functions

optimize-rootoptimize-curve-fitoptimize-linprog