Main Content

Optimize Live Editor Task

What Is theOptimizeLive Editor Task?

TheOptimizeLive Editor task provides a visual interface for thefminbnd,fminsearch,fzero, andlsqnonnegsolvers. To start the task, click theNew Live Scriptbutton. Then click theInserttab and selectTask > Optimize.

Optimize Live Editor task.

Minimize a Nonlinear Function of Several Variables

This example shows how to minimize the function f ( x , y ) = 100 ( y x 2 ) 2 + ( a x ) 2 where the variablea=πusing theOptimizeLive Editor task.

视频描述类似的优化问题lem, seeHow to Use the Optimize Live Editor Task.

  1. On theHometab, in theFilesection, click theNew Live Scriptbutton.

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

    Insert task Optimize

  3. For use in entering problem data, click theSection Breakbutton. New sections appear above and below the task.

  4. In the section above theOptimizetask, enter the following code.

    a = pi; x0 = [-1 2];
  5. To place these variables into the workspace, pressCtrl + Enter.

  6. In theSpecify problem typesection of the task, click theObjective > Nonlinearbutton and theConstraints > Unconstrainedbutton. The task shows that the recommended solver isfminsearch.

    Note

    If you have Optimization Toolbox™, your recommended solver at this point is different. Choosefminsearchto proceed with the example.

  7. In theSelect problem datasection, selectObjective function > Local functionand then click theNewbutton. A function script appears in a new section below the task. Edit the resulting code to contain the following uncommented lines.

    functionf = objectiveFcn(optimInput,a) x = optimInput(1); y = optimInput(2); f = 100*(y - x^2)^2 + (a - x)^2;end
  8. In theSelect problem datasection, selectobjectiveFcnas the local function.

  9. In theSelect problem datasection, underFunction inputs, selectOptimization input > optimInputandFixed input: a > a.

    Local function objectiveFcn, optimization input optimInput, fixed input a

  10. SelectInitial point (x0) > x0.

  11. In theDisplay progresssection, selectObjective valuefor the plot.

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

    Run Section

    The following plot appears.

    Plot showing values decreasing from 120 to 4e-11 in fewer than 150 iterations

  13. To view the solution point, look at the top of theOptimizetask.

    Variables solution and objectiveValue are returned

    ThesolutionandobjectiveValuevariables are returned to the workspace. To view their values, insert a section break below the task and enter this code.

    disp(solution) disp(objectiveValue)
  14. Run the section by pressingCtrl+Enter.

    disp(solution)
    3.1416 9.8696
    disp(objectiveValue)
    3.9946e-11

解一个标量方程

This example shows how to use theOptimizeLive Editor task to find the pointxwhere cos(x) =x.

  1. On theHometab, in theFilesection, click theNew Live Scriptbutton. Enter these lines of code in the live script.

    fun = @(x)cos(x) - x; x0 = 0;

    The first line defines the anonymous functionfun, which takes the value 0 at the pointxwhere cos(x) =x. The second line defines the initial pointx0= 0, wherefzerobegins its search for a solution.

  2. Put these variables in the MATLAB®workspace by pressingCtrl+Enter.

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

  4. In theSpecify problem typesection of the task, selectSolver > fzero.

  5. In theSelect problem datasection, selectObjective function > Function handleand then selectfun. SelectInitial point (x0) > x0.

    fzero with choices made

  6. In the Display progress section, selectObjective valuefor the plot.

    Plot function is Objective value.

  7. Run the solver by pressingCtrl+Enter.

    Plot of solution process, seven iterations with final five all nearly zero.

  8. To see the solution value, insert a new section below the task by clicking theSection Breakbutton on theInserttab. In the new section, entersolutionand pressCtrl+Enter.

    solution
    solution = 0.7391

See Also

||

Related Topics