Documentation |
On this page… |
---|
To use the GlobalSearch or MultiStart solvers, you must first create a problem structure. There are two ways to create a problem structure: using the createOptimProblem function and exporting from the Optimization app.
For information on creating inputs for the problem structure, see Inputs for Problem Structure.
Follow these steps to create a problem structure using the createOptimProblem function.
Define your objective function as a file or anonymous function. For details, see Compute Objective Functions. If your solver is lsqcurvefit or lsqnonlin, ensure the objective function returns a vector, not scalar.
If relevant, create your constraints, such as bounds and nonlinear constraint functions. For details, see Write Constraints.
Create a start point. For example, to create a three-dimensional random start point xstart:
xstart = randn(3,1);
(Optional) Create an options structure using optimoptions. For example,
options = optimoptions(@fmincon,'Algorithm','interior-point');
Enter
problem = createOptimProblem(solver,
where solver is the name of your local solver:
For GlobalSearch: 'fmincon'
For MultiStart the choices are:
'fmincon'
'fminunc'
'lsqcurvefit'
'lsqnonlin'
For help choosing, see Optimization Decision Table.
Set an initial point using the 'x0' parameter. If your initial point is xstart, and your solver is fmincon, your entry is now
problem = createOptimProblem('fmincon','x0',xstart,
Include the function handle for your objective function in objective:
problem = createOptimProblem('fmincon','x0',xstart, ... 'objective',@objfun,
Set bounds and other constraints as applicable.
Constraint | Name |
---|---|
lower bounds | 'lb' |
upper bounds | 'ub' |
matrix Aineq for linear inequalities Aineq x ≤ bineq | 'Aineq' |
vector bineq for linear inequalities Aineq x ≤ bineq | 'bineq' |
matrix Aeq for linear equalities Aeq x = beq | 'Aeq' |
vector beq for linear equalities Aeq x = beq | 'beq' |
nonlinear constraint function | 'nonlcon' |
If using the lsqcurvefit local solver, include vectors of input data and response data, named 'xdata' and 'ydata' respectively.
Best practice: validate the problem structure by running your solver on the structure. For example, if your local solver is fmincon:
[x fval eflag output] = fmincon(problem);
This example minimizes the function from Run the Solver, subject to the constraint x_{1} + 2x_{2} ≥ 4. The objective is
sixmin = 4x^{2} – 2.1x^{4} + x^{6}/3 + xy – 4y^{2} + 4y^{4}.
Use the interior-point algorithm of fmincon, and set the start point to [2;3].
Write a function handle for the objective function.
sixmin = @(x)(4*x(1)^2 - 2.1*x(1)^4 + x(1)^6/3 ... + x(1)*x(2) - 4*x(2)^2 + 4*x(2)^4);
Write the linear constraint matrices. Change the constraint to "less than" form:
A = [-1,-2]; b = -4;
Create the local options structure to use the interior-point algorithm:
opts = optimoptions(@fmincon,'Algorithm','interior-point');
Create the problem structure with createOptimProblem:
problem = createOptimProblem('fmincon', ... 'x0',[2;3],'objective',sixmin, ... 'Aineq',A,'bineq',b,'options',opts)
The resulting structure:
problem = objective: @(x)(4*x(1)^2-2.1*x(1)^4+x(1)^6/3+x(1)*x(2)-4*x(2)^2+4*x(2)^4) x0: [2x1 double] Aineq: [-1 -2] bineq: -4 Aeq: [] beq: [] lb: [] ub: [] nonlcon: [] solver: 'fmincon' options: [1x1 optim.options.Fmincon]
Best practice: validate the problem structure by running your solver on the structure:
[x fval eflag output] = fmincon(problem);
Follow these steps to create a problem structure using the Optimization app.
Define your objective function as a file or anonymous function. For details, see Compute Objective Functions. If your solver is lsqcurvefit or lsqnonlin, ensure the objective function returns a vector, not scalar.
If relevant, create nonlinear constraint functions. For details, see Nonlinear Constraints .
Create a start point. For example, to create a three-dimensional random start point xstart:
xstart = randn(3,1);
Open the Optimization app by entering optimtool at the command line, or by choosing the Optimization app from the Apps tab.
Choose the local Solver.
For GlobalSearch: fmincon (default).
For MultiStart:
fmincon (default)
fminunc
lsqcurvefit
lsqnonlin
For help choosing, see Optimization Decision Table.
Choose an appropriate Algorithm. For help choosing, see Choosing the Algorithm.
Set an initial point (Start point).
Include the function handle for your objective function in Objective function, and, if applicable, include your Nonlinear constraint function. For example,
Set bounds, linear constraints, or local Options. For details on constraints, see Writing Constraints.
Best practice: run the problem to verify the setup.
Choose File > Export to Workspace and select Export problem and options to a MATLAB structure named
This example minimizes the function from Run the Solver, subject to the constraint x_{1} + 2x_{2} ≥ 4. The objective is
sixmin = 4x^{2} – 2.1x^{4} + x^{6}/3 + xy – 4y^{2} + 4y^{4}.
Use the interior-point algorithm of fmincon, and set the start point to [2;3].
Write a function handle for the objective function.
sixmin = @(x)(4*x(1)^2 - 2.1*x(1)^4 + x(1)^6/3 ... + x(1)*x(2) - 4*x(2)^2 + 4*x(2)^4);
Write the linear constraint matrices. Change the constraint to "less than" form:
A = [-1,-2]; b = -4;
Launch the Optimization app by entering optimtool at the MATLAB^{®} command line.
Set the solver, algorithm, objective, start point, and constraints.
Best practice: run the problem to verify the setup.
The problem runs successfully.
Choose File > Export to Workspace and select Export problem and options to a MATLAB structure named