Tune Solution for Analysis
When using the problem-based approach, you can try different options and methods to tune the solver and produce results more quickly.
Try Differentiation Options
solve function typically uses the most effective
automatic differentiation method. However, your problem might run faster using a
different method, or no automatic differentiation at all.
To obtain faster solutions, try different automatic differentiation options for
solve. Set the
ObjectiveDerivative name-value argument to a nondefault value. For
[sol,fval] = solve(prob,x0,ObjectiveDerivative="auto-forward")
Try Evaluation Methods
When applicable, try evaluating an optimization expression directly, without using
fcn2optimexpr. Then evaluate the expression with
fcn2optimexpr and compare the results. Similarly, try using
fcn2optimexpr with the
"off" in addition to using it with the default setting
"on". Although the default settings can be the most
efficient, a nondefault setting might work best for your problem.
Break Up Expressions for
solve runs more effectively when you break
expressions into smaller pieces and call
fcn2optimexpr on each
piece separately. With this approach,
the type of each expression as the most general type in the included expressions.
For example, if an expression contains both quadratic and exponential terms,
fcn2optimexpr labels the expression as a general nonlinear
expression. If the expression is split into a quadratic-only expression and an
exponential expression, then
fcn2optimexpr can correctly label
the first expression as quadratic and the second as general nonlinear.
expr1 = sum(x.^2 + y.^2,"all"); expr2 = sum(x.*exp(x).*(x.^2 + y.^2),"all"); express1 = fcn2optimexpr(expr1,x,y); express2 = fcn2optimexpr(expr2,x); prob.Objective = express1; prob.Constraints.expconstr = express2 >= 1/10;
In this example, having separate expressions for the objective and constraint allows the solver to recognize that the objective is a pure sum of squares. In contrast, the following formulation does not allow the solver to recognize that the objective is a sum of squares.
expr1 = x.^2 + y.^2; [express1,express2] = ... fcn2optimexpr([sum(expr1,"all),sum(x.*exp(x).*expr1,"all)],x,y); prob.Objective = express1; prob.Constraints.expconstr = express2 >= 1/10;