Note: This page has been translated by MathWorks. Click here to see

To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

There are two different Optimization Toolbox™ approaches for solving single-objective optimization problems. This section describes the problem-based approach. Solver-Based Optimization Problem Setup describes the solver-based approach.

To solve an optimization problem, perform the following steps.

Create a problem object by using

`optimproblem`

. A problem object is a container that you fill with objective and constraint expressions. These expressions define the problem, along with bounds that exist in the problem variables.For example, create a maximization problem.

prob = optimproblem('ObjectiveSense','maximize');

Create named variables by using

`optimvar`

. An optimization variable is a symbolic variable that you use to describe the problem objective and constraints. Include any bounds in the variable definitions.For example, create a 15-by-3 array of binary variables named

`'x'`

.x = optimvar('x',15,3,'Type','integer','LowerBound',0,'UpperBound',1);

Define the objective function in the problem object as an expression in the named variables.

### Note

If you have a nonlinear function that is not a polynomial or rational expression, convert it to an optimization expression by using

`fcn2optimexpr`

. See Convert Nonlinear Function to Optimization Expression.For example, assume that you have a real matrix

`f`

of the same size as a matrix of variables`x`

, and the objective is the sum of the entries in`f`

times the corresponding variables`x`

.prob.Objective = sum(sum(f.*x));

Define constraints in the problem object as expressions in the named variables.

### Note

If you have a nonlinear function that is not a polynomial or rational expression, convert it to an optimization expression by using

`fcn2optimexpr`

. See Convert Nonlinear Function to Optimization Expression.For example, assume that the sum of the variables in each row of

`x`

must be one, and the sum of the variables in each column must be no more than one.onesum = sum(x,2) == 1; vertsum = sum(x,1) <= 1; prob.Constraints.onesum = onesum; prob.Constraints.vertsum = vertsum;

Solve the optimization problem by using

`solve`

.sol = solve(prob);

In addition to these basic steps, you can review the problem definition before solving
the problem by using `showproblem`

and related functions. Set options for `solve`

by using `optimoptions`

, as explained in Change Default Solver or Options.

For a basic mixed-integer linear programming example, see Mixed-Integer Linear Programming Basics: Problem-Based or the video version Solve a Mixed-Integer Linear Programming Problem using Optimization Modeling. For a nonlinear example, see Solve a Constrained Nonlinear Problem, Problem-Based. For more extensive examples, see Problem-Based Nonlinear Optimization, Linear Programming and Mixed-Integer Linear Programming, or Quadratic Programming.

`fcn2optimexpr`

| `optimproblem`

| `optimvar`

| `solve`