Этот раздел содержит пример нелинейной проблемы минимизации со всеми возможными типами ограничений. Целевая функция находится в локальной функции myobj(x). Нелинейные ограничения находятся в локальной функции myconstr(x). Этот пример не использует градиенты.
function [x fval exitflag] = fullexample
x0 = [1; 4; 5; 2; 5];
lb = [-Inf; -Inf; 0; -Inf; 1];
ub = [ Inf; Inf; 20; Inf; Inf];
Aeq = [1 -0.3 0 0 0];
beq = 0;
A = [0 0 0 -1 0.1
0 0 0 1 -0.5
0 0 -1 0 0.9];
b = [0; 0; 0];
opts = optimoptions(@fmincon,'Algorithm','sqp');
[x,fval,exitflag]=fmincon(@myobj,x0,A,b,Aeq,beq,lb,ub,...
@myconstr,opts)
%---------------------------------------------------------
function f = myobj(x)
f = 6*x(2)*x(5) + 7*x(1)*x(3) + 3*x(2)^2;
%---------------------------------------------------------
function [c, ceq] = myconstr(x)
c = [x(1) - 0.2*x(2)*x(5) - 71
0.9*x(3) - x(4)^2 - 67];
ceq = 3*x(2)^2*x(5) + 3*x(1)^2*x(3) - 20.875;fullexample производит следующее отображение в Командном окне:[x fval exitflag] = fullexample;
Local minimum found that satisfies the constraints.
Optimization completed because the objective function is non-decreasing in
feasible directions, to within the value of the optimality tolerance,
and constraints are satisfied to within the value of the constraint tolerance.
x =
0.6114
2.0380
1.3948
0.1572
1.5498
fval =
37.3806
exitflag =
1