The Optimization Package
The Optimization package provides commands to find the minimum or maximum of various types of functions subject to various types of constraints. This worksheet provides a few basic examples of each type of problem the package is capable of solving. Further information can be found on the help pages for the package and each of the individual package members.
In this worksheet, we will be minimizing a function (referred to as the objective function) subject to a number of constraints. Certain maximize examples are included as well. In any of the examples, the maximize option can be added to the command to find the maximum instead of the minimum.
Also note that in each case we are only finding a locally optimal value.
Introduction to the Optimization Package
The following command allows you to use the short form of the command names in the Optimization package.
The commands in the Optimization package allow you to specify the objective function and the constraints in several different ways. This worksheet deals with the algebraic form of the objective function and constraints. The other forms are Matrix form and procedure form where Matrix form is covered in a separate example worksheet.
In general, an optimization problem is of the form
min fx where x is in ℝn
subject to cix≤0 for i in I, cix=0 for i in E
The Optimization package handles several types of objective and constraint functions. Each type of problem is a subtype of the next one.
There is also a minimize command that will attempt to automatically select the best algorithm for a given problem.
In addition, there is a command, LSSolve, which minimizes functions of the form
Linear Programming Example 1
For a first example, we have a simplex two dimensional linear programming problem.
min −2 x−y
The plot below shows the feasible region in yellow, the contours of the objective function, and the optimal solution as a green circle.
The command returns the optimal function values, as well as the point at which the optimal value occurs.
Alternatively, we could use the first two constraints and the nonnegative option.
The first element of the solution is the minimum value that the objective function obtains, while satisfying the constraints. The second element indicates a point where the minimum is reached. This point is not necessarily unique.
Linear Programming Example 2
We can also include equality constraint as the next example shows. This example also demonstrates the maximize option.
Quadratic Programming Example
For a first example of a quadratic program, we will use:
subject to −11≤x+y−7 x
The plot below shows the feasible region in yellow and the contours of the objective function. The green circle indicates the optimal point.
The QPSolve command returns the optimal function values, as well as the point at which the optimal value occurs.
The NLPSolve command allows us to solve problems of a fairly general nature. As long as the derivative of the objective function and constraint function is continuous, we can use NLPSolve.
Our first basic example of nonlinear programming is a simple univariate minimization of a polynomial.
This plot shows the function and the minimum found by NLPSolve.
We can minimize this function in the range [0..6].
We can also maximize the function.
We can minimize more complex functions such as:
Below we plot the function f and the minimum point. We can see that NLPSolve finds a local minimum, not necessarily a global one.
We can also minimize multivariate functions such as
Curve Fitting with Least Squares
We can use the least squares command to do curve fitting. For example, if we have the following data points, (xi,yi):
and we want to fit the data with a quintic polynomial. Let our quintic be,
Now we need to find a,b,c,d,e, and, f such that
is a minimum.
LSSolve takes as input a list of the residues p⁡xi−yi.
Now we can call LSSolve with the residues.
Our polynomial becomes:
We can now plot the data and the curve
Return to Index for Example Worksheets
Download Help Document
What kind of issue would you like to report? (Optional)