Main Content

Optimize Live Editor Task withfminconSolver

This example shows how to use the solver-basedOptimizeLive Editor task with thefminconsolver to minimize a quadratic subject to linear and nonlinear constraints and bounds.

Consider the problem of finding [x1,x2] that solves

min x f ( x ) = x 1 2 + x 2 2

subject to the constraints

0.5 x 1 (bound) x 1 x 2 + 1 0 (linear inequality) x 1 2 x 2 2 + 1 0 9 x 1 2 x 2 2 + 9 0 x 1 2 + x 2 0 x 2 2 + x 1 0 } (nonlinear inequality)

The starting pointx0for this problem isx1= 3 andx2= 1.

Start Optimize Live Editor Task

Create a new live Script by clicking theNew Live Scriptbutton in theFilesection on theHometab.

New Live Script button

Insert anOptimizeLive Editor task. Click theInserttab and then, in theCodesection, selectTask > Optimize.

Insert Optimize Live Editor task

Optimize task in Live Editor: Choose between problem-based (recommended) and solver-based

For this example, choose the solver-based task.

View of the Optimize Live Task

For later use in entering problem data, selectInsert > Section Break. New sections appear above and below the task.

Enter Problem Data

  1. Starting from the top of the task, enter the problem type and constraint types. Click theObjective > Quadraticbutton and theConstraints > Lower bounds,Linear inequality, andNonlinearbuttons. The task shows that the recommended solver isfmincon.

  2. 目标函数

    我们的目标function is simple enough to represent as an anonymous function. Position the cursor in the section above the task and enter this code.

    fun = @(x)sum(x.^2);
  3. Lower Bound

    The problem contains the lower boundx1≥ 0.5. Express this bound as a variablelb. With the cursor at the end of the line defining the objective function, pressEnter, and enter the following code to specify the lower bound.

    lb = [0.5 -Inf];
  4. Initial Point

    With the cursor at the end of the line defining the lower bound, pressEnter, and enter the following code to set the initial point.

    x0 = [3,1];
  5. Linear Constraint

    With the cursor at the end of the line defining the initial point, pressEnter, and enter the following code to set the linear constraint.

    A = [-1,-1]; b = -1;
  6. Run Section

    The top section now includes five parameters.

    Five lines of code defining initial parameters

    Next, you need to run the section to place the parameters in the workspace as variables. To do so, click the left-most area of the section, which contains a bar of diagonal stripes. After you click this area, the bar becomes a solid bar, indicating the variables are now in the workspace. (Note: You can also pressCtrl+Enterto run the section.)

  7. Set Problem Data

    Enter the variables in theSelect problem datasection of the task. To specify the objective function, selectObjective function > Function handleand choosefun.

  8. Set the initial pointx0.

  9. Select下界>从工作区and selectlb.

  10. Set the linear inequality constraint variablesAandbin theLinear inequalityarea.

  11. Now specify the nonlinear inequality constraints. In theSelect problem datasection, selectNonlinear > Local function, and then click theNewbutton. The function appears in a new section below the task. Edit the resulting code to contain the following uncommented lines.

    function[c,ceq] = constraintFcn(x)% You can include commented code lines or not.% Be sure that just these uncommented lines remain:c = [-x(1)^2 - x(2)^2 + 1; -9*x(1)^2 - x(2)^2 + 9; -x(1)^2 + x(2); -x(2)^2 + x(1)]; ceq = [];end
  12. In theSelect problem datasection, select theconstraintFcnfunction.

  13. 监测进展

    In theDisplay progresssection of the task, selectText display > Each iterationso you can monitor the solver progress. SelectObjective valuefor the plot.

    Your setup looks like this:

    fmincon solver, objective function handle fun, initial point x0, lower bounds lb, linear inequality constraints A and b, nonlinear local function constraintFcn, display each iteration, plot objective value

Run Solver and Examine Results

To run the solver, click the options buttonat the top right of the task window, and selectRun Section.

Run solver; keyboard equivalent is ctrl+enter

The plot appears in a separate figure window and in the task output area.

Plot showing 12 iterations and a final function value 2

To see where the solution variables are returned, look at the top of the task.

solution, objectiveValue = minimize fun using fmincon solver

The final point and its associated objective function value appear in thesolutionandobjectiveValuevariables in the workspace. View these values by entering this code in the live editor section below the task.

solution, objectiveValue

PressCtrl+Enterto run the section.

solution = [1 1], objectiveValue = 2

See Also

|

Related Topics