Main Content

Internally, the `solve`

function
solves optimization problems by calling a solver:

`linprog`

for linear objective and linear constraints`intlinprog`

for linear objective and linear constraints and integer constraints`quadprog`

for quadratic objective and linear constraints`lsqlin`

or`lsqnonneg`

for linear least-squares with linear constraints`lsqcurvefit`

or`lsqnonlin`

for nonlinear least-squares with bound constraints`fminunc`

for problems without any constraints (not even variable bounds) and with a general nonlinear objective function`fmincon`

for problems with a nonlinear constraint, or with a general nonlinear objective and at least one constraint`fzero`

for a scalar nonlinear equation`lsqlin`

for systems of linear equations, with or without bounds`fsolve`

for systems of nonlinear equations without constraints`lsqnonlin`

for systems of nonlinear equations with bounds

Before `solve`

can call these
functions, the problems must be converted to solver form, either by `solve`

or some other associated functions or objects. This conversion entails, for example, linear
constraints having a matrix representation rather than an optimization variable
expression.

The first step in the algorithm occurs as you place
optimization expressions into the problem. An `OptimizationProblem`

object has an internal list of the variables used in its
expressions. Each variable has a linear index in the expression, and a size. Therefore, the
problem variables have an implied matrix form. The `prob2struct`

function performs the conversion from problem form to solver form. For an example, see Convert Problem to Structure.

For nonlinear optimization problems that use the `fmincon`

or
`fminunc`

solvers, `solve`

uses
*automatic differentiation* to compute the gradients of the
objective function and nonlinear constraint functions. These derivatives apply when the
objective and constraint functions are composed of Supported Operations on Optimization Variables and Expressions and do not use the
`fcn2optimexpr`

function. When automatic differentiation does not apply, solvers estimate derivatives using
finite differences. For details of automatic differentiation, see Automatic Differentiation Background.

For the default and allowed solvers that
`solve`

calls, depending on the problem objective and constraints, see
`'solver'`

. You
can override the default by using the `'solver'`

name-value pair argument when calling `solve`

.

For the algorithm that
`intlinprog`

uses to solve MILP problems, see intlinprog Algorithm. For
the algorithms that `linprog`

uses to solve linear programming problems,
see Linear Programming Algorithms.
For the algorithms that `quadprog`

uses to solve quadratic programming
problems, see Quadratic Programming Algorithms. For linear or nonlinear least-squares solver
algorithms, see Least-Squares (Model Fitting) Algorithms. For nonlinear solver algorithms, see Unconstrained Nonlinear Optimization Algorithms and
Constrained Nonlinear Optimization Algorithms.

For nonlinear equation solving, `solve`

internally represents each
equation as the difference between the left and right sides. Then `solve`

attempts to minimize the sum of squares of the equation components. For the algorithms for
solving nonlinear systems of equations, see Equation Solving Algorithms. When
the problem also has bounds, `solve`

calls `lsqnonlin`

to minimize the sum of squares of equation components. See Least-Squares (Model Fitting) Algorithms.

**Note**

If your objective function is a sum of squares, and you want `solve`

to recognize it as such, write it as `sum(expr.^2)`

, and not as
`expr'*expr`

or any other form. The internal parser recognizes only
explicit sums of squares. For details, see Write Objective Function for Problem-Based Least Squares. For an example, see
Nonnegative Linear Least Squares, Problem-Based.

`intlinprog`

| `linprog`

| `prob2struct`