Solve optimization problem or equation problem
Use solve
to find the solution of an optimization problem
or equation problem.
Tip
For the full workflow, see ProblemBased Optimization Workflow or ProblemBased Workflow for Solving Equations.
modifies the solution process using one or more namevalue pair arguments in
addition to the input arguments in previous syntaxes.sol
= solve(___,Name,Value
)
Solve a linear programming problem defined by an optimization problem.
x = optimvar('x'); y = optimvar('y'); prob = optimproblem; prob.Objective = x  y/3; prob.Constraints.cons1 = x + y <= 2; prob.Constraints.cons2 = x + y/4 <= 1; prob.Constraints.cons3 = x  y <= 2; prob.Constraints.cons4 = x/4 + y >= 1; prob.Constraints.cons5 = x + y >= 1; prob.Constraints.cons6 = x + y <= 2; sol = solve(prob)
Solving problem using linprog. Optimal solution found.
sol = struct with fields:
x: 0.6667
y: 1.3333
Find a minimum of the peaks
function, which is included in MATLAB®, in the region $${x}^{2}+{y}^{2}\le 4$$. To do so, create optimization variables x
and y
.
x = optimvar('x'); y = optimvar('y');
Create an optimization problem having peaks
as the objective function.
prob = optimproblem("Objective",peaks(x,y));
Include the constraint as an inequality in the optimization variables.
prob.Constraints = x^2 + y^2 <= 4;
Set the initial point for x
to 1 and y
to –1, and solve the problem.
x0.x = 1; x0.y = 1; sol = solve(prob,x0)
Solving problem using fmincon. Local minimum found that satisfies the constraints. Optimization completed because the objective function is nondecreasing in feasible directions, to within the value of the optimality tolerance, and constraints are satisfied to within the value of the constraint tolerance.
sol = struct with fields:
x: 0.2283
y: 1.6255
Unsupported Functions Require fcn2optimexpr
If your objective or nonlinear constraint functions are not entirely composed of elementary functions, you must convert the functions to optimization expressions using fcn2optimexpr
. See Convert Nonlinear Function to Optimization Expression and Supported Operations on Optimization Variables and Expressions.
To convert the present example:
convpeaks = fcn2optimexpr(@peaks,x,y); prob.Objective = convpeaks; sol2 = solve(prob,x0)
Solving problem using fmincon. Local minimum found that satisfies the constraints. Optimization completed because the objective function is nondecreasing in feasible directions, to within the value of the optimality tolerance, and constraints are satisfied to within the value of the constraint tolerance.
sol2 = struct with fields:
x: 0.2283
y: 1.6255
Copyright 2018–2020 The MathWorks, Inc.
Compare the number of steps to solve an integer programming problem both with and without an initial feasible point. The problem has eight integer variables and four linear equality constraints, and all variables are restricted to be positive.
prob = optimproblem; x = optimvar('x',8,1,'LowerBound',0,'Type','integer');
Create four linear equality constraints and include them in the problem.
Aeq = [22 13 26 33 21 3 14 26 39 16 22 28 26 30 23 24 18 14 29 27 30 38 26 26 41 26 28 36 18 38 16 26]; beq = [ 7872 10466 11322 12058]; cons = Aeq*x == beq; prob.Constraints.cons = cons;
Create an objective function and include it in the problem.
f = [2 10 13 17 7 5 7 3]; prob.Objective = f*x;
Solve the problem without using an initial point, and examine the display to see the number of branchandbound nodes.
[x1,fval1,exitflag1,output1] = solve(prob);
Solving problem using intlinprog. LP: Optimal objective value is 1554.047531. Cut Generation: Applied 8 strong CG cuts. Lower bound is 1591.000000. Branch and Bound: nodes total num int integer relative explored time (s) solution fval gap (%) 10000 1.03 0   18027 1.71 1 2.906000e+03 4.509804e+01 21859 2.17 2 2.073000e+03 2.270974e+01 23546 2.35 3 1.854000e+03 1.180593e+01 24121 2.41 3 1.854000e+03 1.563342e+00 24294 2.43 3 1.854000e+03 0.000000e+00 Optimal solution found. Intlinprog stopped because the objective value is within a gap tolerance of the optimal value, options.AbsoluteGapTolerance = 0 (the default value). The intcon variables are integer within tolerance, options.IntegerTolerance = 1e05 (the default value).
For comparison, find the solution using an initial feasible point.
x0.x = [8 62 23 103 53 84 46 34]'; [x2,fval2,exitflag2,output2] = solve(prob,x0);
Solving problem using intlinprog. LP: Optimal objective value is 1554.047531. Cut Generation: Applied 8 strong CG cuts. Lower bound is 1591.000000. Relative gap is 59.20%. Branch and Bound: nodes total num int integer relative explored time (s) solution fval gap (%) 3627 0.50 2 2.154000e+03 2.593968e+01 5844 0.75 3 1.854000e+03 1.180593e+01 6204 0.79 3 1.854000e+03 1.455526e+00 6400 0.80 3 1.854000e+03 0.000000e+00 Optimal solution found. Intlinprog stopped because the objective value is within a gap tolerance of the optimal value, options.AbsoluteGapTolerance = 0 (the default value). The intcon variables are integer within tolerance, options.IntegerTolerance = 1e05 (the default value).
fprintf('Without an initial point, solve took %d steps.\nWith an initial point, solve took %d steps.',output1.numnodes,output2.numnodes)
Without an initial point, solve took 24294 steps. With an initial point, solve took 6400 steps.
Giving an initial point does not always improve the problem. For this problem, using an initial point saves time and computational steps. However, for some problems, an initial point can cause solve
to take more steps.
Solve the problem
$$\underset{x}{\mathrm{min}}\left(3{x}_{1}2{x}_{2}{x}_{3}\right)\phantom{\rule{0.2777777777777778em}{0ex}}subject\phantom{\rule{0.2777777777777778em}{0ex}}to\{\begin{array}{l}{x}_{3}\phantom{\rule{0.2777777777777778em}{0ex}}binary\\ {x}_{1},{x}_{2}\ge 0\\ {x}_{1}+{x}_{2}+{x}_{3}\le 7\\ 4{x}_{1}+2{x}_{2}+{x}_{3}=12\end{array}$$
without showing iterative display.
x = optimvar('x',2,1,'LowerBound',0); x3 = optimvar('x3','Type','integer','LowerBound',0,'UpperBound',1); prob = optimproblem; prob.Objective = 3*x(1)  2*x(2)  x3; prob.Constraints.cons1 = x(1) + x(2) + x3 <= 7; prob.Constraints.cons2 = 4*x(1) + 2*x(2) + x3 == 12; options = optimoptions('intlinprog','Display','off'); sol = solve(prob,'Options',options)
sol = struct with fields:
x: [2x1 double]
x3: 1
Examine the solution.
sol.x
ans = 2×1
0
5.5000
sol.x3
ans = 1
intlinprog
to Solve a Linear ProgramForce solve
to use intlinprog
as the solver for a linear programming problem.
x = optimvar('x'); y = optimvar('y'); prob = optimproblem; prob.Objective = x  y/3; prob.Constraints.cons1 = x + y <= 2; prob.Constraints.cons2 = x + y/4 <= 1; prob.Constraints.cons3 = x  y <= 2; prob.Constraints.cons4 = x/4 + y >= 1; prob.Constraints.cons5 = x + y >= 1; prob.Constraints.cons6 = x + y <= 2; sol = solve(prob,'Solver', 'intlinprog')
Solving problem using intlinprog. LP: Optimal objective value is 1.111111. Optimal solution found. No integer variables specified. Intlinprog solved the linear problem.
sol = struct with fields:
x: 0.6667
y: 1.3333
Solve the mixedinteger linear programming problem described in Solve Integer Programming Problem with Nondefault Options and examine all of the output data.
x = optimvar('x',2,1,'LowerBound',0); x3 = optimvar('x3','Type','integer','LowerBound',0,'UpperBound',1); prob = optimproblem; prob.Objective = 3*x(1)  2*x(2)  x3; prob.Constraints.cons1 = x(1) + x(2) + x3 <= 7; prob.Constraints.cons2 = 4*x(1) + 2*x(2) + x3 == 12; [sol,fval,exitflag,output] = solve(prob)
Solving problem using intlinprog. LP: Optimal objective value is 12.000000. Optimal solution found. Intlinprog stopped at the root node because the objective value is within a gap tolerance of the optimal value, options.AbsoluteGapTolerance = 0 (the default value). The intcon variables are integer within tolerance, options.IntegerTolerance = 1e05 (the default value).
sol = struct with fields:
x: [2x1 double]
x3: 1
fval = 12
exitflag = OptimalSolution
output = struct with fields:
relativegap: 0
absolutegap: 0
numfeaspoints: 1
numnodes: 0
constrviolation: 0
message: 'Optimal solution found....'
solver: 'intlinprog'
For a problem without any integer constraints, you can also obtain a nonempty Lagrange multiplier structure as the fifth output.
Create and solve an optimization problem using named index variables. The problem is to maximize the profitweighted flow of fruit to various airports, subject to constraints on the weighted flows.
rng(0) % For reproducibility p = optimproblem('ObjectiveSense', 'maximize'); flow = optimvar('flow', ... {'apples', 'oranges', 'bananas', 'berries'}, {'NYC', 'BOS', 'LAX'}, ... 'LowerBound',0,'Type','integer'); p.Objective = sum(sum(rand(4,3).*flow)); p.Constraints.NYC = rand(1,4)*flow(:,'NYC') <= 10; p.Constraints.BOS = rand(1,4)*flow(:,'BOS') <= 12; p.Constraints.LAX = rand(1,4)*flow(:,'LAX') <= 35; sol = solve(p);
Solving problem using intlinprog. LP: Optimal objective value is 1027.472366. Heuristics: Found 1 solution using ZI round. Upper bound is 1027.233133. Relative gap is 0.00%. Optimal solution found. Intlinprog stopped at the root node because the objective value is within a gap tolerance of the optimal value, options.AbsoluteGapTolerance = 0 (the default value). The intcon variables are integer within tolerance, options.IntegerTolerance = 1e05 (the default value).
Find the optimal flow of oranges and berries to New York and Los Angeles.
[idxFruit,idxAirports] = findindex(flow, {'oranges','berries'}, {'NYC', 'LAX'})
idxFruit = 1×2
2 4
idxAirports = 1×2
1 3
orangeBerries = sol.flow(idxFruit, idxAirports)
orangeBerries = 2×2
0 980.0000
70.0000 0
This display means that no oranges are going to NYC
, 70 berries are going to NYC
, 980 oranges are going to LAX
, and no berries are going to LAX
.
List the optimal flow of the following:
Fruit Airports
 
Berries NYC
Apples BOS
Oranges LAX
idx = findindex(flow, {'berries', 'apples', 'oranges'}, {'NYC', 'BOS', 'LAX'})
idx = 1×3
4 5 10
optimalFlow = sol.flow(idx)
optimalFlow = 1×3
70.0000 28.0000 980.0000
This display means that 70 berries are going to NYC
, 28 apples are going to BOS
, and 980 oranges are going to LAX
.
To solve the nonlinear system of equations
$$\begin{array}{l}\mathrm{exp}(\mathrm{exp}(({x}_{1}+{x}_{2})))={x}_{2}(1+{x}_{1}^{2})\\ {x}_{1}\mathrm{cos}({x}_{2})+{x}_{2}\mathrm{sin}({x}_{1})=\frac{1}{2}\end{array}$$
using the problembased approach, first define x
as a twoelement optimization variable.
x = optimvar('x',2);
Create the first equation as an optimization equality expression.
eq1 = exp(exp((x(1) + x(2)))) == x(2)*(1 + x(1)^2);
Similarly, create the second equation as an optimization equality expression.
eq2 = x(1)*cos(x(2)) + x(2)*sin(x(1)) == 1/2;
Create an equation problem, and place the equations in the problem.
prob = eqnproblem; prob.Equations.eq1 = eq1; prob.Equations.eq2 = eq2;
Review the problem.
show(prob)
EquationProblem : Solve for: x eq1: exp(exp((x(1) + x(2)))) == (x(2) .* (1 + x(1).^2)) eq2: ((x(1) .* cos(x(2))) + (x(2) .* sin(x(1)))) == 0.5
Solve the problem starting from the point [0,0]
. For the problembased approach, specify the initial point as a structure, with the variable names as the fields of the structure. For this problem, there is only one variable, x
.
x0.x = [0 0]; [sol,fval,exitflag] = solve(prob,x0)
Solving problem using fsolve. Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient.
sol = struct with fields:
x: [2x1 double]
fval = struct with fields:
eq1: 2.4070e07
eq2: 3.8255e08
exitflag = EquationSolved
View the solution point.
disp(sol.x)
0.3532 0.6061
Unsupported Functions Require fcn2optimexpr
If your equation functions are not composed of elementary functions, you must convert the functions to optimization expressions using fcn2optimexpr
. For the present example:
ls1 = fcn2optimexpr(@(x)exp(exp((x(1)+x(2)))),x); eq1 = ls1 == x(2)*(1 + x(1)^2); ls2 = fcn2optimexpr(@(x)x(1)*cos(x(2))+x(2)*sin(x(1)),x); eq2 = ls2 == 1/2;
See Supported Operations on Optimization Variables and Expressions and Convert Nonlinear Function to Optimization Expression.
prob
— Optimization problem or equation problemOptimizationProblem
object  EquationProblem
objectOptimization problem or equation problem, specified as an OptimizationProblem
object or an EquationProblem
object. Create an optimization problem by using optimproblem
; create an equation problem by using eqnproblem
.
Warning
The problembased approach does not support complex values in an objective function, nonlinear equalities, or nonlinear inequalities. If a function calculation has a complex value, even as an intermediate value, the final result can be incorrect.
Example: prob = optimproblem; prob.Objective = obj; prob.Constraints.cons1 =
cons1;
Example: prob = eqnproblem; prob.Equations = eqs;
x0
— Initial pointInitial point, specified as a structure with field names equal to the variable names in prob
.
For an example using x0
with named index variables, see Create Initial Point for Optimization with Named Index Variables.
Example: If prob
has variables named x
and y
: x0.x = [3,2,17]; x0.y = [pi/3,2*pi/3]
.
Data Types: struct
Specify optional
commaseparated pairs of Name,Value
arguments. Name
is
the argument name and Value
is the corresponding value.
Name
must appear inside quotes. You can specify several name and value
pair arguments in any order as
Name1,Value1,...,NameN,ValueN
.
solve(prob,'options',opts)
'options'
— Optimization optionsoptimoptions
 options structureOptimization options, specified as the commaseparated pair consisting
of 'options'
and an object created by optimoptions
or an options
structure such as created by optimset
.
Internally, the solve
function calls a relevant
solver as detailed in the 'solver'
argument reference. Ensure that
options
is compatible with the solver. For
example, intlinprog
does not allow options to be a
structure, and lsqnonneg
does not allow options to be
an object.
For suggestions on options settings to improve an
intlinprog
solution or the speed of a solution,
see Tuning Integer Linear Programming. For linprog
, the
default 'dualsimplex'
algorithm is generally
memoryefficient and speedy. Occasionally, linprog
solves a large problem faster when the Algorithm
option is 'interiorpoint'
. For suggestions on
options settings to improve a nonlinear problem's solution, see Options in Common Use: Tuning and Troubleshooting and Improve Results.
Example: options =
optimoptions('intlinprog','Display','none')
'solver'
— Optimization solver'intlinprog'
 'linprog'
 'lsqlin'
 'lsqcurvefit'
 'lsqnonlin'
 'lsqnonneg'
 'quadprog'
 'fminunc'
 'fmincon'
 'fzero'
 'fsolve'
Optimization solver, specified as the commaseparated pair consisting
of 'solver'
and the name of a listed solver. For
optimization problems, this table contains the available solvers for
each problem type.
Problem Type  Default Solver  Other Allowed Solvers 

Linear objective, linear constraints  linprog  intlinprog ,
quadprog ,
fmincon ,
fminunc
(fminunc is not recommended
because unconstrained linear programs are either
constant or unbounded) 
Linear objective, linear and integer constraints  intlinprog  linprog
(integer constraints ignored) 
Quadratic objective, linear constraints  quadprog  fmincon ,
fminunc
(with no constraints) 
Linear objective, optional linear constraints, and
cone constraints of the form norm(linear
expression) + constant <= linear
expression or sqrt(sum of
squares) + constant <= linear
expression  coneprog  fmincon 
Minimize C*x  d^2 subject to linear constraints  lsqlin
when the objective is a constant plus a sum of squares
of linear expressions  quadprog ,
lsqnonneg
(Constraints other than x >= 0 are ignored for
lsqnonneg ), fmincon ,
fminunc
(with no constraints) 
Minimize C*x  d^2 subject to x >= 0  lsqlin  quadprog ,
lsqnonneg 
Minimize sum(e(i).^2) , where
e(i) is an optimization
expression, subject to bound constraints  lsqnonlin
when the objective has the form given in Write Objective Function for ProblemBased Least Squares  lsqcurvefit , fmincon ,
fminunc
(with no constraints) 
Minimize general nonlinear function f(x)  fminunc  fmincon 
Minimize general nonlinear function f(x) subject to some constraints, or minimize any function subject to nonlinear constraints  fmincon  (none) 
Note
If you choose lsqcurvefit
as the solver for a
leastsquares problem, solve
uses
lsqnonlin
. The
lsqcurvefit
and
lsqnonlin
solvers are identical for
solve
.
Caution
For maximization problems (prob.ObjectiveSense
is "max"
or "maximize"
), do
not specify a leastsquares solver (one with a name beginning
lsq
). If you do, solve
throws an error, because these solvers cannot maximize.
For equation solving, this table contains the available solvers for each problem type. In the table,
* indicates the default solver for the problem type.
Y indicates an available solver.
N indicates an unavailable solver.
Supported Solvers for Equations
Equation Type  lsqlin  lsqnonneg  fzero  fsolve  lsqnonlin 

Linear  *  N  Y (scalar only)  Y  Y 
Linear plus bounds  *  Y  N  N  Y 
Scalar nonlinear  N  N  *  Y  Y 
Nonlinear system  N  N  N  *  Y 
Nonlinear system plus bounds  N  N  N  N  * 
Example: 'intlinprog'
Data Types: char
 string
'ObjectiveDerivative'
— Indication to use automatic differentiation for objective function'auto'
(default)  'autoforward'
 'autoreverse'
 'finitedifferences'
Indication to use automatic differentiation (AD) for nonlinear
objective function, specified as the commaseparated pair consisting of
'ObjectiveDerivative'
and
'auto'
(use AD if possible),
'autoforward'
(use forward AD if possible),
'autoreverse'
(use reverse AD if possible), or
'finitedifferences'
(do not use AD). Choices
including auto
cause the underlying solver to use
gradient information when solving the problem provided that the
objective function is supported, as described in Supported Operations on Optimization Variables and Expressions. For
an example, see Effect of Automatic Differentiation in ProblemBased Optimization.
Solvers choose the following type of AD by default:
For a general nonlinear objective function, fmincon
defaults
to reverse AD for the objective function. fmincon
defaults to
reverse AD for the nonlinear constraint function when the number of nonlinear
constraints is less than the number of variables. Otherwise,
fmincon
defaults to forward AD for the nonlinear constraint
function.
For a general nonlinear objective function, fminunc
defaults
to reverse AD.
For a leastsquares objective function, fmincon
and
fminunc
default to forward AD for the objective function.
For the definition of a problembased leastsquares objective function, see Write Objective Function for ProblemBased Least Squares.
lsqnonlin
defaults to forward AD when the number of elements
in the objective vector is greater than or equal to the number of variables.
Otherwise, lsqnonlin
defaults to reverse AD.
fsolve
defaults to forward AD when the number of equations is
greater than or equal to the number of variables. Otherwise,
fsolve
defaults to reverse AD.
Example: 'finitedifferences'
Data Types: char
 string
'ConstraintDerivative'
— Indication to use automatic differentiation for constraint functions'auto'
(default)  'autoforward'
 'autoreverse'
 'finitedifferences'
Indication to use automatic differentiation (AD) for nonlinear
constraint functions, specified as the commaseparated pair consisting
of 'ConstraintDerivative'
and
'auto'
(use AD if possible),
'autoforward'
(use forward AD if possible),
'autoreverse'
(use reverse AD if possible), or
'finitedifferences'
(do not use AD). Choices
including auto
cause the underlying solver to use
gradient information when solving the problem provided that the
constraint functions are supported, as described in Supported Operations on Optimization Variables and Expressions. For
an example, see Effect of Automatic Differentiation in ProblemBased Optimization.
Solvers choose the following type of AD by default:
For a general nonlinear objective function, fmincon
defaults
to reverse AD for the objective function. fmincon
defaults to
reverse AD for the nonlinear constraint function when the number of nonlinear
constraints is less than the number of variables. Otherwise,
fmincon
defaults to forward AD for the nonlinear constraint
function.
For a general nonlinear objective function, fminunc
defaults
to reverse AD.
For a leastsquares objective function, fmincon
and
fminunc
default to forward AD for the objective function.
For the definition of a problembased leastsquares objective function, see Write Objective Function for ProblemBased Least Squares.
lsqnonlin
defaults to forward AD when the number of elements
in the objective vector is greater than or equal to the number of variables.
Otherwise, lsqnonlin
defaults to reverse AD.
fsolve
defaults to forward AD when the number of equations is
greater than or equal to the number of variables. Otherwise,
fsolve
defaults to reverse AD.
Example: 'finitedifferences'
Data Types: char
 string
'EquationDerivative'
— Indication to use automatic differentiation for equations'auto'
(default)  'autoforward'
 'autoreverse'
 'finitedifferences'
Indication to use automatic differentiation (AD) for nonlinear
constraint functions, specified as the commaseparated pair consisting
of 'EquationDerivative'
and 'auto'
(use AD if possible), 'autoforward'
(use forward AD
if possible), 'autoreverse'
(use reverse AD if
possible), or 'finitedifferences'
(do not use AD).
Choices including auto
cause the underlying solver to
use gradient information when solving the problem provided that the
equation functions are supported, as described in Supported Operations on Optimization Variables and Expressions. For
an example, see Effect of Automatic Differentiation in ProblemBased Optimization.
Solvers choose the following type of AD by default:
For a general nonlinear objective function, fmincon
defaults
to reverse AD for the objective function. fmincon
defaults to
reverse AD for the nonlinear constraint function when the number of nonlinear
constraints is less than the number of variables. Otherwise,
fmincon
defaults to forward AD for the nonlinear constraint
function.
For a general nonlinear objective function, fminunc
defaults
to reverse AD.
For a leastsquares objective function, fmincon
and
fminunc
default to forward AD for the objective function.
For the definition of a problembased leastsquares objective function, see Write Objective Function for ProblemBased Least Squares.
lsqnonlin
defaults to forward AD when the number of elements
in the objective vector is greater than or equal to the number of variables.
Otherwise, lsqnonlin
defaults to reverse AD.
fsolve
defaults to forward AD when the number of equations is
greater than or equal to the number of variables. Otherwise,
fsolve
defaults to reverse AD.
Example: 'finitedifferences'
Data Types: char
 string
sol
— SolutionSolution, returned as a structure. The fields of the structure are the names of the optimization variables. See optimvar
.
fval
— Objective function value at the solutionObjective function value at the solution, returned as a real number, or,
for systems of equations, a real vector. For leastsquares problems,
fval
is the sum of squares of the residuals at the
solution. For equationsolving problems, fval
is the
function value at the solution, meaning the lefthand side minus the
righthand side of the equations.
Tip
If you neglect to ask for fval
for an optimization
problem, you can calculate it using:
fval = evaluate(prob.Objective,sol)
exitflag
— Reason solver stoppedReason the solver stopped, returned as an enumeration variable. You can convert
exitflag
to its numeric equivalent using
double(exitflag)
, and to its string equivalent using
string(exitflag)
.
This table describes the exit flags for the intlinprog
solver.
Exit Flag for intlinprog  Numeric Equivalent  Meaning 

OptimalWithPoorFeasibility  3  The solution is feasible with respect to the relative

IntegerFeasible  2  intlinprog stopped prematurely, and found an
integer feasible point. 
OptimalSolution 
 The solver converged to a solution

SolverLimitExceeded 

See Tolerances and Stopping Criteria. 
OutputFcnStop  1  intlinprog stopped by an output function or plot
function. 
NoFeasiblePointFound 
 No feasible point found. 
Unbounded 
 The problem is unbounded. 
FeasibilityLost 
 Solver lost feasibility. 
Exitflags 3
and 9
relate
to solutions that have large infeasibilities. These usually arise from linear constraint
matrices that have large condition number, or problems that have large solution components. To
correct these issues, try to scale the coefficient matrices, eliminate redundant linear
constraints, or give tighter bounds on the variables.
This table describes the exit flags for the linprog
solver.
Exit Flag for linprog  Numeric Equivalent  Meaning 

OptimalWithPoorFeasibility  3  The solution is feasible with respect to the relative

OptimalSolution  1  The solver converged to a solution

SolverLimitExceeded  0  The number of iterations exceeds

NoFeasiblePointFound  2  No feasible point found. 
Unbounded  3  The problem is unbounded. 
FoundNaN  4 

PrimalDualInfeasible  5  Both primal and dual problems are infeasible. 
DirectionTooSmall  7  The search direction is too small. No further progress can be made. 
FeasibilityLost  9  Solver lost feasibility. 
Exitflags 3
and 9
relate
to solutions that have large infeasibilities. These usually arise from linear constraint
matrices that have large condition number, or problems that have large solution components. To
correct these issues, try to scale the coefficient matrices, eliminate redundant linear
constraints, or give tighter bounds on the variables.
This table describes the exit flags for the lsqlin
solver.
Exit Flag for lsqlin  Numeric Equivalent  Meaning 

FunctionChangeBelowTolerance  3  Change in the residual is smaller than the specified tolerance

StepSizeBelowTolerance 
 Step size smaller than

OptimalSolution  1  The solver converged to a solution

SolverLimitExceeded  0  The number of iterations exceeds

NoFeasiblePointFound  2  For optimization problems, the problem is infeasible. Or, for
the For equation problems, no solution found. 
IllConditioned  4  Illconditioning prevents further optimization. 
NoDescentDirectionFound  8  The search direction is too small. No further progress can be
made. ( 
This table describes the exit flags for the quadprog
solver.
Exit Flag for quadprog  Numeric Equivalent  Meaning 

LocalMinimumFound  4  Local minimum found; minimum is not unique. 
FunctionChangeBelowTolerance  3  Change in the objective function value is smaller than the
specified tolerance 
StepSizeBelowTolerance 
 Step size smaller than

OptimalSolution  1  The solver converged to a solution

SolverLimitExceeded  0  The number of iterations exceeds

NoFeasiblePointFound  2  The problem is infeasible. Or, for the

IllConditioned  4  Illconditioning prevents further optimization. 
Nonconvex 
 Nonconvex problem detected.
( 
NoDescentDirectionFound  8  Unable to compute a step direction.
( 
This table describes the exit flags for the coneprog
solver.
Exit Flag for coneprog  Numeric Equivalent  Meaning 

OptimalSolution  1  The solver converged to a solution

SolverLimitExceeded  0  The number of iterations exceeds

NoFeasiblePointFound  2  The problem is infeasible. 
Unbounded  3  The problem is unbounded. 
DirectionTooSmall 
 The search direction became too small. No further progress could be made. 
Unstable  10  The problem is numerically unstable. 
This table describes the exit flags for the lsqcurvefit
or
lsqnonlin
solver.
Exit Flag for lsqnonlin  Numeric Equivalent  Meaning 

SearchDirectionTooSmall  4  Magnitude of search direction was smaller than

FunctionChangeBelowTolerance  3  Change in the residual was less than

StepSizeBelowTolerance 
 Step size smaller than

OptimalSolution  1  The solver converged to a solution

SolverLimitExceeded  0  Number of iterations exceeded

OutputFcnStop  1  Stopped by an output function or plot function. 
NoFeasiblePointFound  2  For optimization problems, problem is infeasible: the bounds
For equation problems, no solution found. 
This table describes the exit flags for the fminunc
solver.
Exit Flag for fminunc  Numeric Equivalent  Meaning 

NoDecreaseAlongSearchDirection  5  Predicted decrease in the objective function is less than the

FunctionChangeBelowTolerance  3  Change in the objective function value is less than the

StepSizeBelowTolerance 
 Change in 
OptimalSolution  1  Magnitude of gradient is smaller than the

SolverLimitExceeded  0  Number of iterations exceeds

OutputFcnStop  1  Stopped by an output function or plot function. 
Unbounded  3  Objective function at current iteration is below

This table describes the exit flags for the fmincon
solver.
Exit Flag for fmincon  Numeric Equivalent  Meaning 

NoDecreaseAlongSearchDirection  5  Magnitude of directional derivative in search direction is less
than 2* 
SearchDirectionTooSmall  4  Magnitude of the search direction is less than
2* 
FunctionChangeBelowTolerance  3  Change in the objective function value is less than

StepSizeBelowTolerance 
 Change in 
OptimalSolution  1  Firstorder optimality measure is less than

SolverLimitExceeded  0  Number of iterations exceeds

OutputFcnStop  1  Stopped by an output function or plot function. 
NoFeasiblePointFound  2  No feasible point found. 
Unbounded  3  Objective function at current iteration is below

This table describes the exit flags for the fsolve
solver.
Exit Flag for fsolve  Numeric Equivalent  Meaning 

SearchDirectionTooSmall  4  Magnitude of the search direction is less than

FunctionChangeBelowTolerance  3  Change in the objective function value is less than

StepSizeBelowTolerance 
 Change in 
OptimalSolution  1  Firstorder optimality measure is less than

SolverLimitExceeded  0  Number of iterations exceeds

OutputFcnStop  1  Stopped by an output function or plot function. 
NoFeasiblePointFound  2  Converged to a point that is not a root. 
TrustRegionRadiusTooSmall  3  Equation not solved. Trust region radius became too small
( 
This table describes the exit flags for the fzero
solver.
Exit Flag for fzero  Numeric Equivalent  Meaning 

OptimalSolution  1  Equation solved. 
OutputFcnStop  1  Stopped by an output function or plot function. 
FoundNaNInfOrComplex  4 

SingularPoint  5  Might have converged to a singular point. 
CannotDetectSignChange  6  Did not find two points with opposite signs of function value. 
output
— Information about optimization processInformation about the optimization process, returned as a structure. The output
structure contains the fields in the relevant underlying solver output field, depending
on which solver solve
called:
solve
includes the additional field Solver
in
the output
structure to identify the solver used, such as
'intlinprog'
.
When Solver
is a nonlinear solver, solve
includes one or two extra fields describing the derivative estimation type. The
objectivederivative
and, if appropriate,
constraintderivative
fields can take the following values:
"reverseAD"
for reverse automatic differentiation
"forwardAD"
for forward automatic differentiation
"finitedifferences"
for finite difference
estimation
"closedform"
for linear or quadratic functions
lambda
— Lagrange multipliers at the solutionLagrange multipliers at the solution, returned as a structure.
Note
solve
does not return lambda
for
equationsolving problems.
For the intlinprog
and fminunc
solvers,
lambda
is empty, []
. For the other solvers,
lambda
has these fields:
Variables
– Contains fields for each problem variable. Each problem variable name is a structure with two fields:
Lower
– Lagrange multipliers associated with the variable LowerBound
property, returned as an array of the same size as the variable. Nonzero entries mean that the solution is at the lower bound. These multipliers are in the structure lambda.Variables.
.variablename
.Lower
Upper
– Lagrange multipliers associated with the variable UpperBound
property, returned as an array of the same size as the variable. Nonzero entries mean that the solution is at the upper bound. These multipliers are in the structure lambda.Variables.
.variablename
.Upper
Constraints
– Contains a field for each problem constraint. Each problem constraint is in a structure whose name is the constraint name, and whose value is a numeric array of the same size as the constraint. Nonzero entries mean that the constraint is active at the solution. These multipliers are in the structure lambda.Constraints.
.constraintname
Note
Elements of a constraint array all have the same comparison
(<=
, ==
, or
>=
) and are all of the same type (linear, quadratic,
or nonlinear).
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 leastsquares with linear constraints
lsqcurvefit
or lsqnonlin
for nonlinear leastsquares 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, 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'
namevalue 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 leastsquares solver
algorithms, see LeastSquares (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 LeastSquares (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 ProblemBased Least Squares. For an example, see
Nonnegative Linear Least Squares, ProblemBased.
Automatic differentiation (AD) applies to the solve
and
prob2struct
functions under the following conditions:
The objective and constraint functions are supported, as described in Supported Operations on Optimization Variables and Expressions. They do not
require use of the fcn2optimexpr
function.
The solver called by solve
is fmincon
, fminunc
, fsolve
, or lsqnonlin
.
For optimization problems, the 'ObjectiveDerivative'
and
'ConstraintDerivative'
namevalue pair arguments for
solve
or prob2struct
are set to
'auto'
, 'autoforward'
, or
'autoreverse'
.
For equation problems, the 'EquationDerivative'
option is set
to 'auto'
, 'autoforward'
, or
'autoreverse'
.
When AD Applies  All Constraint Functions Supported  One or More Constraints Not Supported 

Objective Function Supported  AD used for objective and constraints  AD used for objective only 
Objective Function Not Supported  AD used for constraints only  AD not used 
When these conditions are not satisfied, solve
estimates gradients by
finite differences, and prob2struct
does not create gradients in its
generated function files.
Solvers choose the following type of AD by default:
For a general nonlinear objective function, fmincon
defaults
to reverse AD for the objective function. fmincon
defaults to
reverse AD for the nonlinear constraint function when the number of nonlinear
constraints is less than the number of variables. Otherwise,
fmincon
defaults to forward AD for the nonlinear constraint
function.
For a general nonlinear objective function, fminunc
defaults
to reverse AD.
For a leastsquares objective function, fmincon
and
fminunc
default to forward AD for the objective function.
For the definition of a problembased leastsquares objective function, see Write Objective Function for ProblemBased Least Squares.
lsqnonlin
defaults to forward AD when the number of elements
in the objective vector is greater than or equal to the number of variables.
Otherwise, lsqnonlin
defaults to reverse AD.
fsolve
defaults to forward AD when the number of equations is
greater than or equal to the number of variables. Otherwise,
fsolve
defaults to reverse AD.
Note
To use automatic derivatives in a problem converted by prob2struct
, pass options specifying these derivatives.
options = optimoptions('fmincon','SpecifyObjectiveGradient',true,... 'SpecifyConstraintGradient',true); problem.options = options;
Currently, AD works only for first derivatives; it does not apply to second or higher
derivatives. So, for example, if you want to use an analytic Hessian to speed your
optimization, you cannot use solve
directly, and must instead use the
approach described in Supply Derivatives in ProblemBased Workflow.
solve(prob,solver)
, solve(prob,options)
, and solve(prob,solver,options)
syntaxes have been removedErrors starting in R2018b
To choose options or the underlying solver for solve
, use
namevalue pairs. For example,
sol = solve(prob,'options',opts,'solver','quadprog');
The previous syntaxes were not as flexible, standard, or extensible as namevalue pairs.
solve
estimates derivatives in parallel for nonlinear solvers
when the UseParallel
option for the solver is
true
. For example,
options = optimoptions('fminunc','UseParallel',true); [sol,fval] = solve(prob,x0,'Options',options)
solve
does not use parallel derivative estimation when all
nonlinear functions are supported, as described in Supported Operations on Optimization Variables and Expressions. In this case,
solve
uses automatic differentiation for calculating
derivatives. See Automatic Differentiation.
You can override automatic differentiation and use finite difference estimates in
parallel by setting the 'ObjectiveDerivative'
and 'ConstraintDerivative'
arguments to
'finitedifferences'
.
EquationProblem
 evaluate
 fcn2optimexpr
 OptimizationProblem
 optimoptions
 prob2struct
You have a modified version of this example. Do you want to open this example with your edits?
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
Select web siteYou can also select a web site from the following list:
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.