Solver-Based Optimization in MATLAB
Define and solve optimization and least-squares problems and systems of nonlinear equations. Use the Optimize Live Editor task to guide you through this workflow.
1. Group the optimization variables into a single vector \( x \). Write the objective and constraints in terms of \( x \).
| Objective Type | Mathematical Form | Example |
|---|---|---|
| Linear | \( f^{T} x \) | f = [-1 0 -5]; |
| Quadratic | \( x^{T}H x + f^{T} x \) | H = [5 1 0; 1 3 0; 0 0 0]; |
| Least Squares | \( \| Cx - d \|_2 \) \( \sum F_i(x)^2 \) |
C = [7 8 10; 1 3 4; 2 5 7];d = [2; 1; 1.5];function F = myF(x)F(1) = f1(x);F(2) = f2(x);end |
| General | \( f(x) \) | function objval = fobj(x)objval = 3*(x(1)-x(2))^4;end |
| Constraint Type | Mathematical Form | Example |
|---|---|---|
| Bound | \( l \leq x \leq u \) | lb = zeros(n,1);ub = 5*ones(n,1); |
| Linear | \( A x \leq b \) \( A_{eq} x = b_{eq} \) |
A = [1 0 1; 0 -2 1];b = [4; 2];Aeq = [1 0 2];beq = 1; |
| Second-Order Cone | \( \| A_{sc} x - b_{sc} \| \leq d_{sc} x - \gamma \) | A = diag([1,1/2,0]);b = zeros(3,1);d = [0;0;1];gamma = 0;socConstraints =secondordercone(A,b,d,gamma); |
| General | \( c(x) \leq 0 \) \( c_{eq}(x) = 0 \) |
function[c,ceq] = nlcons(x)c(1) = x(1).^2 + x(2).^2 - 1;c(2) = x(1)*x(3) - 5;ceq = [];end |
| Integer | \( x_j \in \mathbb{Z}^n \) | intcon = [1 2] |
2. Choose a solver matching the types of objective and constraints.
Solvers in Optimization Toolbox™ use derivatives, are usually faster, and scale to large problems. Solvers in Global Optimization Toolbox (italic) and MATLAB (*) do not use derivatives and search for global minima.
| Constraint Type | Objective Type | ||||||
|---|---|---|---|---|---|---|---|
| Linear | Quadratic | Least Squares | General Smooth | General Nonsmooth | Multiobjective | ||
| None | quadprog |
lsqcurvefitlsqnonlinmldivide |
fminsearch* |
fminsearch* |
fgoalattain |
||
| Bound | linprog |
quadprog |
lsqcurvefit |
fmincon |
surrogateopt |
fgoalattain |
|
| Linear | linprog |
quadprog |
lsqlin |
fmincon |
patternsearchsurrogateopt |
fgoalattain |
|
| Second-Order Cone | coneprog |
coneprog |
|||||
| General Smooth | fmincon |
fmincon |
fmincon |
fmincon |
patternsearchga |
fgoalattain |
|
| General Nonsmooth | patternsearchsurrogateopt |
patternsearchsurrogateopt |
patternsearchsurrogateopt |
patternsearchsurrogateopt |
patternsearch |
paretosearch |
|
| Integer | intlinprog |
ga |
|||||
3. Define initial point if required and options if desired. Call solver and obtain solution.
| Options |
|---|
Use Examples: opts = optimoptions('fmincon','Display','iter')
|
| Do More |
|---|
|