| Products & Services | Industries | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → Optimization Toolbox |
| Contents | Index |
| Learn more about Optimization Toolbox |
| On this page… |
|---|
About Optimization Tool Examples |
This section contains two examples showing how to use the Optimization Tool to solve representative problems. There are other examples available: Problem Formulation: Rosenbrock's Function and Example: Constrained Minimization Using fmincon's Interior-Point Algorithm With Analytic Hessian in this User's Guide, and several in the Genetic Algorithm and Direct Search Toolbox User's Guide.
This example shows how to use the Optimization Tool with the fmincon solver to minimize a quadratic subject to linear and nonlinear constraints and bounds.
Consider the problem of finding [x1, x2] that solves
![]()
subject to the constraints

The starting guess for this problem is x1 = 3 and x2 = 1.
function f = objfun(x) f = x(1)^2 + x(2)^2;
function [c,ceq] = nonlconstr(x)
c = [-x(1)^2 - x(2)^2 + 1;
-9*x(1)^2 - x(2)^2 + 9;
-x(1)^2 + x(2);
-x(2)^2 + x(1)];
ceq = [];Enter optimtool in the Command Window to open the Optimization Tool.
Select fmincon from the selection of solvers and change the Algorithm field to Active set.

Enter @objfun in the Objective function field to call the M-file objfun.m.
Enter [3; 1] in the Start point field.

Define the constraints.
Set the bound 0.5 ≤ x1 by entering 0.5 in the Lower field.
Set the linear inequality constraint by entering [-1 -1] in the A field and enter -1 in the b field.
Set the nonlinear constraints by entering @nonlconstr in the Nonlinear constraint function field.

In the Options pane, expand the Display to command window option if necessary, and select Iterative to show algorithm information at the Command Window for each iteration.

Click the Start button as shown in the following figure.

When the algorithm terminates, under Run solver and view results the following information is displayed:

The Current iteration value when the algorithm terminated, which for this example is 7.
The final value of the objective function when the algorithm terminated:
Objective function value: 2.0000000268595803
The algorithm termination message:
Local minimum found that satisfies the constraints. Optimization completed because the objective function is non-decreasing in feasible directions, to within the default value of the function tolerance, and constraints were satisfied to within the default value of the constraint tolerance.
The final point, which for this example is
1
1 In the Command Window, the algorithm information is displayed for each iteration:
Max Line search Directional First-order
Iter F-count f(x) constraint steplength derivative optimality Procedure
0 3 10 2 Infeasible start point
1 6 4.84298 -0.1322 1 -5.22 1.74
2 9 4.0251 -0.01168 1 -4.39 4.08 Hessian modified twice
3 12 2.42704 -0.03214 1 -3.85 1.09
4 15 2.03615 -0.004728 1 -3.04 0.995 Hessian modified twice
5 18 2.00033 -5.596e-005 1 -2.82 0.0664 Hessian modified twice
6 21 2 -5.327e-009 1 -2.81 0.000522 Hessian modified twice
Local minimum found that satisfies the constraints.
Optimization completed because the objective function is non-decreasing in
feasible directions, to within the default value of the function tolerance,
and constraints were satisfied to within the default value of the constraint tolerance.
Active inequalities (to within options.TolCon = 1e-006):
lower upper ineqlin ineqnonlin
3
4[1] Schittkowski, K., "More Test Examples for Nonlinear Programming Codes," Lecture Notes in Economics and Mathematical Systems, Number 282, Springer, p. 45, 1987.
This example shows how to use the Optimization Tool to solve a constrained least-squares problem.
The problem in this example is to find the point on the plane x1 + 2x2 + 4x3 = 7 that is closest
to the origin. The easiest way to solve this problem is to minimize
the square of the distance from a point x = (x1,x2,x3) on
the plane to the origin, which returns the same optimal point as minimizing
the actual distance. Since the square of the distance from an arbitrary
point (x1,x2,x3) to
the origin is
, you can describe
the problem as follows:
![]()
subject to the constraint
x1 + 2x2 + 4x3 = 7.
The function f(x) is called the objective function and x1 + 2x2 + 4x3 = 7 is an equality constraint. More complicated problems might contain other equality constraints, inequality constraints, and upper or lower bound constraints.
This section shows how to set up the problem with the lsqlin solver in the Optimization Tool.
Enter optimtool in the Command Window to open the Optimization Tool.
Select lsqlin from the selection of solvers. Use the default large-scale algorithm.

Enter the following to create variables for the objective function:
In the C field, enter eye(3).
In the d field, enter zeros(3,1).
The C and d fields should appear as shown in the following figure.
![]()
Enter the following to create variables for the equality constraints:
In the Aeq field, enter [1 2 4].
In the beq field, enter 7.
The Aeq and beq fields should appear as shown in the following figure.

Click the Start button as shown in the following figure.

When the algorithm terminates, under Run solver and view results the following information is displayed:

The Current iteration value when the algorithm terminated, which for this example is 1.
The final value of the objective function when the algorithm terminated:
Objective function value: 2.333333333333333
The algorithm termination message:
Optimization terminated.
The final point, which for this example is
0.3333
0.6667
1.3333![]() | Importing and Exporting Your Work | Optimization Algorithms and Examples | ![]() |

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.
| © 1984-2009- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |