# createOptimProblem

Create optimization problem structure

## Description

problem = createOptimProblem(solverName) creates an empty optimization problem structure for the solverName solver.

example

problem = createOptimProblem(solverName,Name,Value) specifies additional options using one or more name-value arguments.

## Examples

collapse all

Create a problem structure with the following specifications:

• fmincon solver

• "interior-point" algorithm

• Random 2-D initial point x0

• Rosenbrock's function as the objective

• Lower bounds of –2

• Upper bounds of 2

Rosenbrock's function for a 2-D variable $x$ is $f\left(x\right)=100{\left({x}_{2}-{x}_{1}^{2}\right)}^{2}+{\left(1-{x}_{1}\right)}^{2}$ (for details, see Constrained Nonlinear Problem Using Optimize Live Editor Task or Solver). To specify the "interior-point" algorithm, create options using optimoptions.

anonrosen = @(x)(100*(x(2) - x(1)^2)^2 + (1-x(1))^2);
opts = optimoptions(@fmincon,Algorithm="interior-point");
rng default % For reproducibility
problem = createOptimProblem("fmincon",...
x0=randn(2,1),...
objective=anonrosen,...
lb=[-2;-2],...
ub=[2;2],...
options=opts);

Solve the problem starting from problem.x0 by calling fmincon.

[x,fval] = fmincon(problem)
Local minimum possible. Constraints satisfied.

fmincon stopped because the size of the current step is less than
the value of the step size tolerance and constraints are
satisfied to within the value of the constraint tolerance.
x = 2×1

1.0000
1.0000

fval = 2.0603e-11

Look for a better solution by calling GlobalSearch.

gs = GlobalSearch;
[x2,fval2] = run(gs,problem)
GlobalSearch stopped because it analyzed all the trial points.

All 16 local solver runs converged with a positive local solver exit flag.
x2 = 2×1

1.0000
1.0000

fval2 = 2.1093e-11

In this case, both fmincon and GlobalSearch reach the same solution.

## Input Arguments

collapse all

Optimization solver, specified as one of the following.

• For GlobalSearch, specify "fmincon" or @fmincon.

• For MultiStart, specify "fmincon" or @fmincon, "fminunc" or @fminunc, "lsqnonlin" or @lsqnonlin, or "lsqcurvefit" or @lsqcurvefit.

Example: "fmincon"

Data Types: char | string | function_handle

### Name-Value Arguments

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Before R2021a, use commas to separate each name and value, and enclose Name in quotes.

Example: createOptimProblem("fmincon","x0",x0,"objective",fun,"lb",zeros(size(x0)))

Linear equality constraints, specified as a real matrix. Aeq is an Me-by-nvars matrix, where Me is the number of equalities.

Aeq encodes the Me linear equalities

Aeq*x = beq,

where x is the column vector of N variables x(:), and beq is a column vector with Me elements.

For example, to specify

x1 + 2x2 + 3x3 = 10
2x1 + 4x2 + x3 = 20,

give these constraints:

Aeq = [1,2,3;2,4,1];
beq = [10;20];

Example: To specify that the control variables sum to 1, give the constraints Aeq = ones(1,N) and beq = 1.

Data Types: double

Linear inequality constraints, specified as a real matrix. Aineq is an M-by-nvars matrix, where M is the number of inequalities.

Aineq encodes the M linear inequalities

Aineq*x <= bineq,

where x is the column vector of nvars variables x(:), and bineq is a column vector with M elements.

For example, to specify

x1 + 2x2 ≤ 10
3x1 + 4x2 ≤ 20
5x1 + 6x2 ≤ 30,

give these constraints:

Aineq = [1,2;3,4;5,6];
bineq = [10;20;30];

Example: To specify that the control variables sum to 1 or less, give the constraints Aineq = ones(1,N) and bineq = 1.

Data Types: double

Linear equality constraints, specified as a real vector. beq is an Me-element vector related to the Aeq matrix. If you pass beq as a row vector, solvers internally convert beq to the column vector beq(:).

beq encodes the Me linear equalities

Aeq*x = beq,

where x is the column vector of N variables x(:), and Aeq is a matrix of size Meq-by-N.

For example, to specify

x1 + 2x2 + 3x3 = 10
2x1 + 4x2 + x3 = 20,

give these constraints:

Aeq = [1,2,3;2,4,1];
beq = [10;20];

Example: To specify that the control variables sum to 1, give the constraints Aeq = ones(1,N) and beq = 1.

Data Types: double

Linear inequality constraints, specified as a real vector. bineq is an M-element vector related to the Aineq matrix. If you pass bineq as a row vector, solvers internally convert bineq to the column vector bineq(:).

bineq encodes the M linear inequalities

Aineq*x <= bineq,

where x is the column vector of N variables x(:), and Aineq is a matrix of size M-by-N.

For example, to specify

x1 + 2x2 ≤ 10
3x1 + 4x2 ≤ 20
5x1 + 6x2 ≤ 30,

give these constraints:

Aineq = [1,2;3,4;5,6];
bineq = [10;20;30];

Example: To specify that the control variables sum to 1 or less, give the constraints Aineq = ones(1,N) and bineq = 1.

Data Types: double

Lower bounds, specified as a real vector or array of doubles. lb represents the lower bounds element-wise in lb  x  ub.

Internally, createOptimProblem converts an array lb to the vector lb(:).

Example: lb = [0;-Inf;4] means x(1) ≥ 0, x(3) ≥ 4.

Data Types: double

Nonlinear constraints, specified as a function handle or function name. nonlcon is a function that accepts an array x and returns two arrays, c(x) and ceq(x).

• c(x) is the array of nonlinear inequality constraints at x. The solver attempts to satisfy c(x) <= 0 for all entries of c.

• ceq(x) is the array of nonlinear equality constraints at x. The solver attempts to satisfy ceq(x) = 0 for all entries of ceq.

For example, nonlcon is a MATLAB® function such as the following:

function [c,ceq] = nonlcon(x)
c = ...     % Compute nonlinear inequalities at x.
ceq = ...   % Compute nonlinear equalities at x.

Data Types: char | string | function_handle

Objective function, specified as a function handle or function name.

• For all solvers except lsqnonlin and lsqcurvefit, the objective function must accept an array x and return a scalar. If the SpecifyObjectiveGradient option is true, then the objective function must return a second output, a vector representing the gradient of the objective. For details, see fun.

• For lsqnonlin, the objective function must accept a vector x and return a vector. If the SpecifyObjectiveGradient option is true, then the objective function must return a second output, a matrix representing the Jacobian of the objective. For details, see fun.

• For lsqcurvefit, the objective function must accept two inputs, x and xdata, and return a vector. If the SpecifyObjectiveGradient option is true, then the objective function must return a second output, a matrix representing the Jacobian of the objective. For details, see fun.

Example: @sin

Example: "sin"

Data Types: char | string | function_handle

Optimization options, specified as the output of optimoptions.

Upper bounds, specified as a real vector or array of doubles. ub represents the upper bounds element-wise in lb  x  ub.

Internally, createOptimProblem converts an array ub to the vector ub(:).

Example: ub = [Inf;4;10] means x(2) ≤ 4, x(3) ≤ 10.

Data Types: double

Initial point, specified as a real vector or real array. Solvers use the number of elements in x0 and the size of x0 to determine the number and size of variables that fun accepts.

Example: x0 = [1,2,3,4]

Data Types: double

Input data for the model, specified as a real vector or real array. The model is

ydata = fun(x,xdata),

where xdata and ydata are fixed arrays, and x is the array of parameters that lsqcurvefit changes to search for a minimum sum of squares.

Example: xdata = [1,2,3,4]

Data Types: double

Response data for the model, specified as a real vector or real array. The model is

ydata = fun(x,xdata),

where xdata and ydata are fixed arrays, and x is the array of parameters that lsqcurvefit changes to search for a minimum sum of squares.

The ydata array must be the same size and shape as the array fun(x0,xdata).

Example: ydata = [1,2,3,4]

Data Types: double

## Output Arguments

collapse all

Optimization problem, returned as a structure. Use problem as the second input argument of run, as in the following examples:

x = run(gs,problem)
x = run(ms,problem,k)

You can also solve the problem by calling the named solver on the problem. For example, if problem is created for fmincon, enter

x = fmincon(problem)

## Version History

Introduced in R2010a