Main Content

Convert function to optimization expression

When possible, create your objective or nonlinear constraint functions by using supported operations on optimization variables and expressions instead of

`fcn2optimexpr`

. Doing so has these advantages:`solve`

includes gradients calculated by automatic differentiation. See Effect of Automatic Differentiation in Problem-Based Optimization.`solve`

has a wider choice of available solvers. When using`fcn2optimexpr`

,`solve`

uses only`fmincon`

or`fminunc`

.

For details, see Convert Nonlinear Function to Optimization Expression and Supported Operations on Optimization Variables and Expressions.

To find the output size of each returned expression when you do not specify
`OutputSize`

, `fcn2optimexpr`

evaluates the function
at the following point for each element of the problem variables.

Variable Characteristics | Evaluation Point |
---|---|

Finite upper bound `ub` and finite lower bound
`lb` | `(lb + ub)/2 + ((ub - lb)/2)*eps` |

Finite lower bound and no upper bound | `lb + max(1,abs(lb))*eps` |

Finite upper bound and no lower bound | `ub - max(1,abs(ub))*eps` |

No bounds | `1 + eps` |

Variable is specified as an integer | `floor` of the point given previously |

An evaluation point might lead to an error in function evaluation. To avoid this error,
specify '`OutputSize`

'.