To use the GlobalSearch or MultiStart solvers, you must first create a problem structure. The
recommended way to create a problem structure is using the
createOptimProblem function. You can create a
structure manually, but doing so is error-prone.
createOptimProblem FunctionFollow 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 options 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);
createOptimProblemThis example minimizes the function from Run the Solver, subject to the constraint x1 + 2x2 ≥ 4. The objective is
| sixmin = 4x2 – 2.1x4 + x6/3 + xy – 4y2 + 4y4. | (1) |
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 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 =
struct with fields:
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);