QUICK START GUIDE
Problem-Based Optimization with Optimization Toolbox™
Use a natural syntax for defining and solving linear and mixed-integer linear, quadratic, linear least squares,
and nonlinear optimization problems.
1. Define Problem
Following the problem-based workflow, first create an optimization problem with optimproblem to hold the objective, constraints,
and associated variables.
Examples:
assignmentProb = optimproblem
responseProb = optimproblem
2. Define Variables
Create optimization variables with optimvar. Set display name and optional dimensions, bounds, and type. Index with integers or
character strings.
Examples:
x = optimvar("x");
y = optimvar("y");
employees = ["a","b","c"];
tasks = ["t1","t2","t3"];
assign = optimvar("assign",employees,tasks,"LowerBound",0,"UpperBound",1,"Type","integer")
3. Define Expressions to Use in Objective and Constraints
Directly specify an OptimizationExpression that is a Specify other expressions as MATLAB functions and convert to
ratio of polynomials. optimization expressions with fcn2optimexpr.
Examples: Examples:
response = -3*(y - x.^3 - x).^2 - (x - 4/3).^2; a = 4;
totalCost = sum(sum(cost.*assign)); xyfcn = @(x,y,a)exp(y)*a*x.^2;
sumByEmployee = sum(assign,2); xyexpr = fcn2optimexpr(xyfcn,x,y,a);
sumByTask = sum(assign,1);
4. Define Objective
Set the sense of the optimization. Set the objective function with a Examples:
scalar OptimizationExpression. responseProb.ObjectiveSense = "maximize";
responseProb.Objective = response;
assignmentProb.ObjectiveSense = "minimize";
assignmentProb.Objective = totalCost;
mathworks.com
5. Define Constraints
Combine OptimizationExpressions with a relational operator to specify an OptimizationConstraint. Assign to a problem.
Examples:
responseProb.Constraints.ellipse = x.^2/2 + y.^2/4 <= 1;
responseProb.Constraints.xyconstr = xyexpr >= 1;
assignmentProb.Constraints.oneTaskPerEmployee = sumByTask <= 1;
assignmentProb.Constraints.oneEmployeePerTask = sumByEmployee == 1;
6. Review
Display with showexpr, showconstr, showbounds, View with the Workspace browser.
and showproblem .
7. Solve and Analyze
Solve the problem, returning the solution values, objective value, Solve with optimization options.
and reason the solver stopped. Provide an initial point for nonlinear
problems. Example:
o = optimoptions(assignProb,"MaxTime",10);
Example: sol = solve(assignmentProb,"Options",o)
x0.x = 0;
x0.y = 0; Do More
[sol,fval,exitflag] = solve(responseProb,x0) • Use evaluate and infeasibility to analyze results
• Interpret and improve results
• Convert to solver-based form with prob2struct
• Include derivatives
Learn more: mathworks.com/help/optim
mathworks.com
© 2019 The MathWorks, Inc. MATLAB and Simulink are registered trademarks of The MathWorks, Inc. See mathworks.com/trademarks for a list of additional trademarks.
Other product or brand names may be trademarks or registered trademarks of their respective holders.