Main Content


Create optimization problem


Use optimproblem to create an optimization problem.


For the full workflow, see Problem-Based Optimization Workflow.


prob = optimproblem creates an optimization problem with default properties.


prob = optimproblem(Name,Value) uses additional options specified by one or more Name,Value pair arguments. For example, to specify a maximization problem instead of a minimization problem, use prob = optimproblem('ObjectiveSense','maximize').


collapse all

Create an optimization problem with default properties.

prob = optimproblem
prob = 
  OptimizationProblem with properties:

       Description: ''
    ObjectiveSense: 'minimize'
         Variables: [0x0 struct] containing 0 OptimizationVariables
         Objective: [0x0 OptimizationExpression]
       Constraints: [0x0 struct] containing 0 OptimizationConstraints

  No problem defined.

Create a linear programming problem for maximization. The problem has two positive variables and three linear inequality constraints.

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

Create positive variables. Include an objective function in the problem.

x = optimvar('x',2,1,'LowerBound',0);
prob.Objective = x(1) + 2*x(2);

Create linear inequality constraints in the problem.

cons1 = x(1) + 5*x(2) <= 100;
cons2 = x(1) + x(2) <= 40;
cons3 = 2*x(1) + x(2)/2 <= 60;
prob.Constraints.cons1 = cons1;
prob.Constraints.cons2 = cons2;
prob.Constraints.cons3 = cons3;

Review the problem.

  OptimizationProblem : 

	Solve for:

	maximize :
       x(1) + 2*x(2)

	subject to cons1:
       x(1) + 5*x(2) <= 100

	subject to cons2:
       x(1) + x(2) <= 40

	subject to cons3:
       2*x(1) + 0.5*x(2) <= 60

	variable bounds:
       0 <= x(1)
       0 <= x(2)

Solve the problem.

sol = solve(prob);
Solving problem using linprog.

Optimal solution found.
ans = 2×1


Input Arguments

collapse all

Name-Value Arguments

Specify optional comma-separated 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.

Example: To specify a maximization problem, use prob = optimproblem('ObjectiveSense','maximize').

Problem constraints, specified as an OptimizationConstraint array or a structure with OptimizationConstraint arrays as fields.

Example: prob = optimproblem('Constraints',sum(x,2) == 1)

Problem label, specified as a string or character vector. The software does not use Description for computation. Description is an arbitrary label that you can use for any reason. For example, you can share, archive, or present a model or problem, and store descriptive information about the model or problem in Description.

Example: "An iterative approach to the Traveling Salesman problem"

Data Types: char | string

Objective function, specified as a scalar OptimizationExpression object.

Example: prob = optimproblem('Objective',sum(sum(x))) for a 2-D variable x

Sense of optimization, specified as 'minimize' or 'maximize'. You can also specify 'min' to obtain 'minimize' or 'max' to obtain 'maximize'. The solve function minimizes the objective when ObjectiveSense is 'minimize' and maximizes the objective when ObjectiveSense is 'maximize'.

Example: prob = optimproblem('ObjectiveSense','max')

Data Types: char | string

Output Arguments

collapse all

Optimization problem, returned as an OptimizationProblem object. Typically, to complete the problem description, you specify an objective function and constraints. However, you can have a feasibility problem, which has no objective function, or you can have a problem with no constraints. Solve a complete problem by calling solve.


The problem-based 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.

Introduced in R2017b