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.

Convert optimization problem to solver form

`problem = prob2struct(prob)`

`problem = prob2struct(prob,x0)`

`problem = prob2struct(___,Name,Value)`

returns an optimization problem structure suitable for solving problems using
`problem`

= prob2struct(`prob`

)`linprog`

, `intlinprog`

,
`quadprog`

, `lsqlin`

,
`fmincon`

, or `fminunc`

. For nonlinear
problems, `prob2struct`

creates files for the objective function,
and, if necessary, for nonlinear constraint functions and supporting files.

,
for any input arguments, specifies additional options using one or more name-value
pair arguments. For example, for a nonlinear problem, `problem`

= prob2struct(___,`Name,Value`

)```
problem =
prob2struct(prob,'ObjectiveFunctionName','objfun1')
```

specifies that
`prob2struct`

creates an objective function file named
`objfun1.m`

in the current folder.

If you call

`prob2struct`

multiple times in the same MATLAB^{®}session for nonlinear problems, use the`ObjectiveFunctionName`

and, if appropriate,`ConstraintFunctionName`

name-value pair arguments. Specifying unique names ensures that the resulting problem structures refer to the correct objective and constraint functions. Otherwise, subsequent calls to`prob2struct`

can cause the generated nonlinear function files to overwrite existing files.To avoid causing an infinite recursion, do not call

`prob2struct`

inside an objective or constraint function.When calling

`prob2struct`

in parallel for nonlinear problems, ensure that the resulting objective and constraint function files have unique names. Doing so avoids each pass of the loop writing to the same file or files.

The basis for the problem structure is an implicit ordering of all problem variables
into a single vector. The order of the problem variables is the same as the order of the
`Variables`

property in `prob`

. See `OptimizationProblem`

. You can also find the order by using `varindex`

.

For example, suppose that the problem variables are in this order:

`x`

— a 3-by-2-by-4 array`y`

— a 3-by-2 array

In this case, the implicit variable order is the same as if the problem variable is
`vars = [x(:);y(:)]`

.

The first 24 elements of `vars`

are equivalent to
`x(:)`

, and the next six elements are equivalent to
`y(:)`

, for a total of 30 elements. The lower and upper bounds
correspond to this variable ordering, and each linear constraint matrix has 30
columns.

For problems with general nonlinear objective or constraint functions,
`prob2struct`

creates function files in the current folder or in
the folder specified by `FileLocation`

. The returned
`problem`

structure refers to these function files.