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 where is in
subject to for in , for in
The Optimization package handles several types of objective and constraint functions. Each type of problem is a subtype of the next one.
Objective Function/Constraint/Command
Linear/Linear/LPSolve
Quadratic/Linear/QPSolve
Nonlinear(General)/Nonlinear(General)/NLPSolve
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
subject to
>
|
|
| (2.1) |
>
|
|
| (2.2) |
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.
>
|
|
| (2.3) |
Alternatively, we could use the first two constraints and the nonnegative option.
>
|
|
| (2.4) |
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.
>
|
|
| (3.1) |
>
|
|
| (3.2) |
>
|
|
| (3.3) |
|
|
Quadratic Programming Example
|
|
For a first example of a quadratic program, we will use:
min
subject to
>
|
|
| (4.1) |
>
|
|
| (4.2) |
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.
>
|
|
| (4.3) |
|
|
Nonlinear Programming
|
|
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.
>
|
|
| (5.1) |
This plot shows the function and the minimum found by NLPSolve.
We can minimize this function in the range [0..6].
>
|
|
| (5.2) |
We can also maximize the function.
>
|
|
| (5.3) |
We can minimize more complex functions such as:
>
|
|
| (5.4) |
>
|
|
| (5.5) |
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
>
|
|
| (5.6) |
with
>
|
|
| (5.7) |
|
|
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, ():
>
|
|
| (6.1) |
and we want to fit the data with a quintic polynomial. Let our quintic be,
.
Now we need to find a, b, and c such that
is a minimum.
LSSolve takes as input a list of the residues .
>
|
|
| (6.2) |
>
|
|
| (6.3) |
Now we can call LSSolve with the residues.
>
|
|
| (6.4) |
Our polynomial becomes:
>
|
|
| (6.5) |
We can now plot the data and the curve
|
Return to Index for Example Worksheets
|
Download Help Document
Was this information helpful?