From the series: Mathematical Modeling with Optimization
Heather Gorr, MathWorks
Express and solve a nonlinear optimization problem with the problem-based approach of Optimization Toolbox™. Interactively define the variables, objective function, and constraints to reflect the mathematical statement of the nonlinear program.
Start by creating an optimization problem to hold the problem. Next, define optimization variables and their bounds. Each optimization variable has its own display name, dimension, type, and bounds. Define one or more scalar or array variables to match the variables used in the mathematical statement.
Create the objective and constraints with optimization expressions built with the optimization variables. Specify them directly for rational expressions. Specify other expressions with MATLAB® functions and convert into optimization expressions with a conversion function. The conversion facility makes it easy to define an optimization problem using existing functions.
Use the display functions to review the completed optimization problem. Then specify an initial point and solve. The type of solver is automatically selected based on the type of variables, objective, and constraints, relieving you of needing to know the many available solvers.
This video shows how to set up and solve a constrained nonlinear optimization problem in MATLAB®. In this example, the goal is to minimize this multivariable objective function subject to the following constraints. Plot the objective function and constraints. The contour lines show the objective function. The feasible region is inside the blue ellipse and below the red curve.
This is a nonlinear optimization problem. There are two ways to solve nonlinear optimization problems in MATLAB: using a problem-based approach or a solver-based approach. This example uses a problem-based approach, which uses optimization variables to define the objective and constraints. See the documentation for the solver-based approach.
There are common steps to solving a nonlinear problem with this approach. First, you set up the problem, define optimization variables, define the objective function and constraints, and solve the problem.
Now that we have expressed the problem mathematically, we need to express the problem in MATLAB. Create an empty optimization problem container. The optimization problem holds the problem information, including the objective function and constraints.
Next, we will define the optimization variables. Generally, optimization variables can be scalars, vectors, matrices, or N-D arrays. This example uses variables x and y, which are scalars. Create scalar optimization variables for this problem. Include the bounds on the variables.
Next, we'll create an optimization expression for the objective function. Currently, optimization expressions do not support exponentials, so write this as a standard MATLAB function. To use this objective function in the problem-based approach, you must use a conversion function, which creates an optimization expression. The file name of the objective function is passed with the @ "at" symbol, which creates a "function handle." This tells MATLAB to identify or "point to" the function, but not to execute the function as MATLAB typically would do without the symbol. Now, add the objective function to the optimization problem. The problem now shows a non-empty objective and associated variables.
This problem has the following nonlinear constraints. The first is a constraint that the solution lies in the ellipse. You can define this constraint as it is written and add it to the problem. The previous constraint was a polynomial inequality and could be expressed as an optimization expression. The second constraint has an exponential term and cannot be written as an optimization expression. This also has extra parameters beyond x and y and includes the variable a. Create a function with inputs x, y, and a. Convert the function to an optimization expression. Include the optimization variables and the parameter a, defined in the MATLAB workspace. Express the inequality and add the constraint to the problem.
Now we'll check that the problem formulation is complete. The optimization variables, objective function, constraints, and bounds all look correct.
Before solving, we need to define an initial point. The initial values for x and y must be defined as a structure. Create a structure representing the initial point as x = -3, y = 3. Solve the problem from the initial point. In general, the exit message indicates the stopping conditions and any problems encountered during the optimization. Here, the exit message and exit flag indicate that the optimization completed successfully.
Try solving the problem from a different initial point. Request additional outputs about the solution. The optimization again completed successfully but converged to a different solution. This has a higher objective function value than the first, which indicates this solution is not as good.
Add the solution points to the visualization. The plot shows that one solution lies on the boundary of the ellipse and the other lies on the boundary of the exponential constraint and the ellipse.
This video illustrated solving a constrained nonlinear optimization problem. See the documentation for additional examples.
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .Select web site
You can also select a web site from the following list:
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.