# createOptimProblem

Create optimization problem structure

## Description

creates an empty optimization problem structure for the
`problem`

= createOptimProblem(`solverName`

)`solverName`

solver.

specifies additional options using one or more name-value arguments.`problem`

= createOptimProblem(`solverName`

,`Name,Value`

)

## Examples

### Create and Run `fmincon`

Problem Structure

Create a problem structure with the following specifications:

`fmincon`

solver`"interior-point"`

algorithmRandom 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(x)=100{({x}_{2}-{x}_{1}^{2})}^{2}+{(1-{x}_{1})}^{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

`solverName`

— Optimization solver

`"fmincon"`

| `@fmincon`

| `"fminunc"`

| `@fminunc`

| `"lsqnonlin"`

| `@lsqnonlin`

| `"lsqcurvefit"`

| `@lsqcurvefit`

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)))`

`Aeq`

— Linear equality constraints

real matrix

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

*x*_{1}
+ 2*x*_{2} +
3*x*_{3} =
10

2*x*_{1}
+ 4*x*_{2} +
*x*_{3} =
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`

`Aineq`

— Linear inequality constraints

real matrix

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

*x*_{1} +
2*x*_{2} ≤
10

3*x*_{1}
+ 4*x*_{2} ≤
20

5*x*_{1}
+ 6*x*_{2} ≤ 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`

`beq`

— Linear equality constraints

real vector

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

*x*_{1}
+ 2*x*_{2} +
3*x*_{3} =
10

2*x*_{1}
+ 4*x*_{2} +
*x*_{3} =
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`

`bineq`

— Linear inequality constraints

real vector

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

*x*_{1} +
2*x*_{2} ≤
10

3*x*_{1}
+ 4*x*_{2} ≤
20

5*x*_{1}
+ 6*x*_{2} ≤ 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`

`lb`

— Lower bounds

`[]`

(default) | real vector or array

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`

`nonlcon`

— Nonlinear constraints

function handle | function name

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.

For more information, see Nonlinear Constraints.

**Data Types: **`char`

| `string`

| `function_handle`

`objective`

— Objective function

function handle | function name

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`

`options`

— Optimization options

output of `optimoptions`

Optimization options, specified as the output of `optimoptions`

.

**Example: **`optimoptions("fmincon","SpecifyObjectiveGradient",true)`

`ub`

— Upper bounds

`[]`

(default) | real vector or array

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`

`x0`

— Initial point

real vector | real array

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`

`xdata`

— Input data for model

real vector | real array

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`

`ydata`

— Response data for model

real vector | real array

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

`problem`

— Optimization problem

structure

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**

## Open Example

You have a modified version of this example. Do you want to open this example with your edits?

## MATLAB Command

You clicked a link that corresponds to this MATLAB command:

Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.

# Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list:

## How to Get Best Site Performance

Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.

### Americas

- América Latina (Español)
- Canada (English)
- United States (English)

### Europe

- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)

- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)