How to Use the Solver-Based Optimize Live Editor Task
Interactively create and solve optimization problems with MATLAB®, Optimization Toolbox™, or Global Optimization Toolbox using a visual interface. Specify the objective and constraints, choose solvers, and set options. Use the Optimize task to specify and solve constrained and unconstrained nonlinear programs, linear programs, quadratic programs, second-order cone programs, mixed-integer programs, linear and nonlinear least squares problems, and systems of nonlinear equations.
The Optimize task guides you through specifying the type of problem by selecting the types of objectives and constraints. The example in the video is to minimize the Rosenbrock function subject to a nonlinear constraint and bound constraints.
Nonlinear objectives and constraints are defined by MATLAB functions. You can browse for their definitions in files or scripts, or you can create new ones when you select “New.” The Optimize task creates templates for these new functions that you can edit. The example constraint function depends on a parameter; the video shows how to modify the function for this case. The Optimize task adds the parameter to its selections and automatically creates the anonymous function that the optimization solver requires when using parameterized functions.
The final specification step is to select algorithm and display options. Once this is done, run the task and view the results. Use the generated MATLAB code to reproduce these results in applications or to share with others.
The Optimize Live Editor task makes it easier to solve your optimization problems by guiding you through the specification of the problem and its data, adapting as you make selections, generating MATLAB code, and providing tool tips and links to documentation throughout.
The Optimize Live Editor Task in R2020b enable you to interactively set up and run an optimization using the Live Editor. It guides you through the process of specifying the problem and exploring options. As with all Live Tasks, it automatically generates the MATLAB code corresponding to your selections which you can use in other applications and enable others to quickly replicate your findings.
As an example, we’ll find the minimum of the Rosenbrock function in a circle of radius R subject to these bounds on x and y.
We’ll open a new Live Script and start by adding a section at the top where we specify some parameters that we can easily change later, namely the radius of the circle and the initial starting point. We will quickly run this section to put the variables into the workspace.
We’ll then navigate up here to insert the Optimize Live Task.
Depending on the objective and constraints, the list of solvers will change to list those able to solve the problem. If you want to learn more about the solvers, click on this icon to open the relevant documentation.
In the case of the Rosenbrock function, we are minimizing a nonlinear function subject to lower and upper bounds and a nonlinear constraint. We will go with the recommended solver.
Nonlinear objectives such as the Rosenbrock function are specified as MATLAB functions. You can specify an existing function from a file, a function local to the Live Script, or create a new one. Let’s create a local function in our Live Script. After putting in the details, we’ll return to the task and make sure that it’s selected.
The initial point will be set to the variable we defined earlier. We can change it here as needed and quickly rerun our code. After inputting our bound constraints, we need to define our nonlinear constraints which will also be specified in a new local function.
Here’s the code that corresponds to our constraint. As a quick overview, nonpositive values of c satisfy the constraint while positive values do not. Since we don’t have a nonlinear equality constraint, ceq is set to an empty array. Select the constraint function in the Live Task and set its parameters. Finally, we’ll specify what kind of plot we’d like to see.
We are all set. Note that unlike other Live Tasks, the autorun button is off by default because it doesn’t make sense to run the solver until the problem is completely set up. Let’s run our script and see what we get.
We can generate the code for reproducing these results from the option menu. You can use this code in applications or share it with others.
One last thing to mention is that you can specify additional options for the solver. For example, we can run a different algorithm to see if it runs faster or converges to a different local solution. In this case, the algorithm takes fewer iterations and converges to the same solution as before.
Use the Optimize Live Task to interactively set up and run an optimization. To learn more, check out documentation at the links below or access it directly from the app.
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.