Hardware FixRecommendedDevice not working? Your driver may be the problemCheck updates for common hardware issues.Fix DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan Now×
Skip to content
World desk4 min

SciPy optimize.minimize: Methods, Bounds, and Constraints

A practical guide to SciPy’s local minimization interface: define an objective, choose a compatible solver, add bounds or constraints, and validate the result.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
#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.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

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.success and read result.message to 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:

  • Use least_squares when the problem is formulated as minimizing residuals.
  • Use minimize_scalar for one-dimensional scalar minimization.
  • Use linprog for 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.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.