Optimization in MATLAB: Video Tutorial
Model linear, integer, quadratic and nonlinear optimization problems in MATLAB. Follow worked examples and check feasibility, solver termination and local solutions.

On this page
This video tutorial reviews optimization and shows how to implement it in MATLAB. It is part of the Numerical Computations course.
Optimization problems in MATLAB
An optimization problem asks for values of the decision variables that improve an objective function , subject to constraints. The solvers used in this tutorial minimize. To maximize a function with these solvers, minimize its negative and change the sign of the returned objective value.
The three videos follow the type of the objective and the constraints:
- Linear programming (LP): a linear objective with linear constraints, solved with
linprog. If some variables must be integers, the problem is a mixed integer linear program (MILP), solved withintlinprog. - Quadratic programming (QP): a quadratic objective with linear constraints, solved with
quadprog. - Nonlinear optimization: a general objective, with or without constraints, solved with
fminbnd,fminsearch,fminuncorfmincon.
linprog, intlinprog, quadprog, fminunc and fmincon are part of Optimization Toolbox. fminbnd and fminsearch are in core MATLAB.
Watch online
The three sections of this tutorial are on YouTube and are embedded here as a playlist.
What each video covers
Section 1: Linear programming and mixed integer LP
Linear Programming and Mixed-Integer LP (YouTube (opens in a new tab)). MATLAB writes a linear program in this standard form:
So a model is defined by seven matrices and vectors: f, A, b, Aeq, beq, lb and ub. Missing parts are passed as empty matrices [].
The video solves three examples:
- Minimize with , and . The bounds go into
lbandub, andlinprogreturns an objective value of . - Maximize subject to six linear inequalities. The objective is negated, and each “greater than or equal” constraint is multiplied by to fit the form . The solution is with a maximum of about . The video then adds integer constraints with
intlinprog, whose second inputintconlists the integer variables. With an integer the solution is , and with an integer it is . - Minimize subject to , , and . The LP solution is about . With an integer,
intlinprogreturns .
f = [5; -2];
A = [-1 3; -4 1; 1 -2];
b = [14; -33; 25];
lb = [0; 0];
x = intlinprog(f, 2, A, b, [], [], lb)For a longer treatment of linprog, with the transportation problem as the example, see the Linear Programming in MATLAB video tutorial.
Section 2: Quadratic programming
Quadratic Programming (YouTube (opens in a new tab)). A quadratic program adds a quadratic term to the LP form:
The video notes that training a support vector machine leads to a QP. Its example is
The main step is to find . The objective is written as , and the cross term is split equally between the two off-diagonal elements, because a symmetric is preferred:
H = [1 -1; -1 2];
f = [-2; -6];
A = [1 1; 2 1];
b = [2; 5];
[x, fval] = quadprog(H, f, A, b)The solution is with an objective value of .
Section 3: Constrained and unconstrained nonlinear optimization
Constrained and Unconstrained Nonlinear Optimization (YouTube (opens in a new tab)). The video compares four functions:
fminbnd: one variable, searched within an interval.fminsearch: several variables, no constraints. It searches without requiring derivatives, and the video uses it for a small problem. This does not guarantee reliable behavior for discontinuous functions or large problems.fminunc: several variables, no constraints. The video uses it for smooth objectives, for which derivative information is useful.fmincon: several variables with constraints.
The first example is . fminbnd on finds the local minimum at about , and fminsearch and fminunc started from 2 find the same point. The video also turns root finding into optimization: minimizing with fminbnd on different intervals finds each of the three roots, which roots([1 0 -5 1]) confirms.
The second example is the Rosenbrock function
whose minimum is at . Both fminsearch and fminunc, started from , find it.
The last example is a constrained design problem: a box with sides , and should have the largest volume, while its surface area is at most 150:
The nonlinear constraint goes into a function that returns c (inequalities, ) and ceq (equalities, empty here). Since fmincon minimizes, the objective is the negative volume. Started from , the solution is a cube with all sides equal. Adding the constraint gives a box that is no longer a cube, and the volume drops to about 117. The video adds this constraint to c, and notes that a linear constraint can also go into A and b.
Applications
- Planning and allocation problems with linear models, such as production planning and transportation.
- Problems with yes or no decisions or whole units, modeled with integer variables.
- Machine learning models such as support vector machines (quadratic programming).
- Engineering design, such as the box example, where the objective and constraints are nonlinear.
Check feasibility and optimality
Substitute the result into every constraint and evaluate the original objective, especially after negating a maximization problem. Inspect the solver’s termination information before accepting a result. A local solution of a nonlinear problem need not be the global best, so compare starting points when the model has several basins. Keep integer decisions in an integer model rather than rounding a continuous solution, which can break the constraints.
How to cite
If you use this code or tutorial in your work, articles or publications, please cite it.
Mostapha Kalami Heris, Optimization in MATLAB: Video Tutorial (YPTNM190520S20), Yarpiz, 2019. https://yarpiz.com/optimization-video-tutorial-yptnm190520s20@misc{yarpiz-yptnm190520s20,
author = {{Mostapha Kalami Heris}},
title = {{Optimization in MATLAB: Video Tutorial}},
howpublished = {Yarpiz},
year = {2019},
note = {Code YPTNM190520S20},
url = {https://yarpiz.com/optimization-video-tutorial-yptnm190520s20}
}Published Updated

Comments