scipy.optimize.minimize is SciPy’s shared interface for finding a local minimum of a scalar objective over one or more variables. Choose a method that supports the bounds or constraints your problem needs, provide derivatives when the method can use them, and check the returned result and feasibility rather than treating a successful call as proof of a correct or global optimum.
Define the objective and starting point
Pass minimize a callable that accepts a one-dimensional parameter vector x and returns a scalar. Supply x0, an initial vector of values. Optional arguments include fixed inputs through args, a solver name through method, and derivative functions or solver options where supported. The precise accepted arguments and their meanings vary by method; see the SciPy v1.18.0 minimize API reference.
| # | Preview | Product | Price | |
|---|---|---|---|---|
| 1 |
|
Numerical Analysis | $221.47 | Buy on Amazon |
| 2 |
|
Numerical Analysis | $91.01 | Buy on Amazon |
| 3 |
|
Numerical Analysis: Interpolation, Differentiation, and Integration (De Gruyter Textbook) | $74.55 | Buy on Amazon |
| 4 |
|
An Introduction to Numerical Analysis | $80.41 | Buy on Amazon |
| 5 |
|
Classical Numerical Analysis: A Comprehensive Course (Cambridge Mathematical Textbooks) | $127.16 | Buy on Amazon |
from scipy.optimize import minimize
def objective(x):
return (x[0] - 2.0)**2 + (x[1] + 1.0)**2
result = minimize(objective, x0=[0.0, 0.0], method="BFGS")
print(result.x) # candidate minimizer
print(result.fun) # objective value at the candidate
print(result.success) # whether the solver reports success
print(result.message) # termination information
This unconstrained example illustrates the interface, not a guarantee that BFGS is appropriate for every objective. minimize performs local optimization: its result depends on the problem and starting point, and does not by itself establish a global minimum.
Choose a method that matches the problem
Methods differ in which restrictions and derivative information they support. The v1.18.0 reference lists the following methods; check the manual for the SciPy version installed in your environment because support can be version-specific.
#1 Best Overall
| Problem or method family | Documented choices and useful distinction |
|---|---|
| Unconstrained optimization | Nelder-Mead and Powell are derivative-free options; CG, BFGS, Newton-CG, dogleg, trust-ncg, trust-krylov, and trust-exact use derivative information in different ways. Check each method’s documentation for its requirements. |
| Variable bounds | The v1.18.0 API lists L-BFGS-B, TNC, SLSQP, Powell, trust-constr, COBYLA, COBYQA, and Nelder-Mead as methods that accept bounds. Their algorithms and derivative needs differ. |
| General constraints | COBYLA, COBYQA, SLSQP, and trust-constr are the documented choices. COBYLA uses linear approximations; COBYQA uses quadratic approximations in a derivative-free trust-region SQP method; SLSQP uses sequential least squares programming; trust-constr supports constraint objects and bounds. |
For bounds, the documentation states: “Bounds on variables for Nelder-Mead, L-BFGS-B, TNC, SLSQP, Powell, trust-constr, COBYLA, and COBYQA methods.” That is a statement about documented support, not a ranking of performance. The SciPy optimization tutorial also compares method capabilities. No single method is best for every problem.
When derivatives are available
If you can provide a reliable gradient or Hessian, consider a method that uses it. minimize exposes jac, hess, and hessp for derivative information, but not every solver accepts them or interprets them the same way. Consult the selected method’s reference before passing these arguments. Derivative-free does not mean constraint-free: COBYQA, for example, is documented for general constraints.
Rank #2
How do I use scipy.optimize.minimize with bounds?
Bounds restrict each variable directly. The Bounds class represents componentwise limits lb <= x <= ub; endpoints can be broadcastable arrays. Equal lower and upper endpoints fix a variable, while infinite endpoints leave that side unbounded.
import numpy as np
from scipy.optimize import Bounds, minimize
def objective(x):
return (x[0] - 2.0)**2 + (x[1] + 1.0)**2
bounds = Bounds(lb=[0.0, -np.inf], ub=[3.0, 4.0])
result = minimize(objective, x0=[1.0, 0.0], method="L-BFGS-B", bounds=bounds)
Choose a solver documented to accept bounds; passing bounds does not make every method bound-aware. Bounds.keep_feasible is used only by trust-constr. It should not be taken to mean every solver keeps all intermediate evaluations within bounds. See the Bounds API reference for endpoint and feasibility details.
Rank #3
Bounds versus general constraints
Bounds apply directly to individual components of x. General constraints instead restrict the value of a function of the variables, such as a linear combination or nonlinear expression. For example, requiring x[0] + x[1] <= 1 is a general constraint, not a pair of component bounds.
COBYLA, COBYQA, and trust-constr accept LinearConstraint and NonlinearConstraint objects. SLSQP accepts a sequence of constraint dictionaries instead. In those dictionaries, an equality constraint has type: "eq" and must equal zero; an inequality has type: "ineq" and must be nonnegative.
Rank #4
Constraint-object example
import numpy as np
from scipy.optimize import LinearConstraint, minimize
def objective(x):
return (x[0] - 2.0)**2 + (x[1] + 1.0)**2
# Require x[0] + x[1] to be between 0 and 1.
constraint = LinearConstraint([1.0, 1.0], lb=0.0, ub=1.0)
result = minimize(objective, x0=[0.0, 0.0], method="trust-constr",
constraints=[constraint])
SLSQP dictionary example
The SciPy v1.18.0 API reference demonstrates SLSQP with nonnegative variable bounds and dictionary inequality constraints. In the pattern below, an inequality function must return a nonnegative value for feasible points:
from scipy.optimize import minimize
def objective(x):
return x[0]**2 + x[1]**2
def constraint_fun(x):
return x[0] + x[1] - 1.0
constraints = [{"type": "ineq", "fun": constraint_fun}]
result = minimize(objective, x0=[0.5, 0.5], method="SLSQP",
bounds=[(0.0, None), (0.0, None)],
constraints=constraints)
print(constraint_fun(result.x))
Evaluate the original constraint functions at the returned candidate, as in the documentation’s example. A solver’s status and a plausible-looking objective value are not substitutes for checking whether the solution meets application requirements.
Quick wins for a faster PC:
Repair Windows errors before they cause bigger problemsFix Now →Scan for outdated or missing drivers - takes under a minuteDriver Scan →Clear out junk files and repair common Windows errorsFree Scan →Inspect the result and termination status
The returned result includes the candidate point and objective value, along with termination information such as success and message. Some methods may return additional fields; for example, the SLSQP example in the API reference shows multipliers. Do not assume every result has the same method-specific fields.
- Check
result.successand readresult.messageto understand how the solver stopped. - Evaluate the objective and the original bounds or constraint functions at
result.x. - Decide whether the achieved objective value and feasibility are adequate for the application; solver termination alone cannot make that judgment.
When a different SciPy optimizer fits better
minimize is not the right interface for every optimization task. SciPy lists separate routines for distinct formulations:
Quick Recap
- Use
least_squareswhen the problem is formulated as minimizing residuals. - Use
minimize_scalarfor one-dimensional scalar minimization. - Use
linprogfor linear programming. - For a global search rather than a local minimization, explore the global optimization routines in SciPy’s optimization reference.
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




