Trying to minimise a function subject to nonlinear constraints
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Adam Stevenson
am 28 Apr. 2019
Kommentiert: Adam Stevenson
am 29 Apr. 2019
function= xz+y^(2)
constraints: xz=3
y^2+z^2=1
Code:
fun = @(x)x(1)*x(3)+x(2)^2;
options = optimoptions('fmincon','Display','iter','Algorithm','sqp');
nonlcon = @confuneq;
A = [];
b = [];
Aeq = [];
beq = [];
lb = [];
ub = [];
x0 = [0,0,0];
x = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon,options)
Separate file:
function [c,ceq] = confuneq(x)
c = x(2)^2 + x(3)^2 -1;
ceq = x(2)*x(3) -3;
end
Can't seem to get the correct answers of (3,0,1) and (-3,0,-1)
Any help is welcome
0 Kommentare
Akzeptierte Antwort
John D'Errico
am 28 Apr. 2019
Bearbeitet: John D'Errico
am 28 Apr. 2019
Simple mistake(s) here. You have TWO equality constraints. But your constraint function is written to have ONE equality constraint, and ONE inequality constraint. And you also wrote the x*z=3 constraint wrong. You wrote it as y*z=3.
Instead, try this:
function [c,ceq] = confuneq(x)
c = [];
ceq = [x(2)^2+x(3)^2-1; x(1)*x(3)-3];
end
That should now be a correct way to write the constraints.
Of course, don't expect fmincon to provide both solutions in one call. You need to start the optimizer out in the correct basin of attraction to get the solution you might want to see.
x = fmincon(fun,[1 1 2],A,b,Aeq,beq,lb,ub,nonlcon,options)
Iter Func-count Fval Feasibility Step Length Norm of First-order
step optimality
0 4 3.000000e+00 4.000e+00 1.000e+00 0.000e+00 2.000e+00
1 9 2.928889e+00 3.352e+00 7.000e-01 1.504e+00 9.644e-01
2 13 2.571265e+00 7.559e-01 1.000e+00 1.060e+00 3.470e-01
3 17 2.789068e+00 2.110e-01 1.000e+00 7.947e-01 3.581e-01
4 21 2.988095e+00 1.191e-02 1.000e+00 3.066e-01 4.893e-02
5 25 2.999988e+00 1.161e-05 1.000e+00 1.437e-02 9.121e-04
6 29 3.000000e+00 5.045e-09 1.000e+00 7.238e-05 1.451e-04
7 33 3.000000e+00 5.265e-09 1.000e+00 7.255e-05 1.646e-07
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.
<stopping criteria details>
x =
2.99999999210178 -1.73125747607761e-08 1.00000000263274
>> x = fmincon(fun,[-1 1 0],A,b,Aeq,beq,lb,ub,nonlcon,options)
Iter Func-count Fval Feasibility Step Length Norm of First-order
step optimality
0 4 1.000000e+00 3.000e+00 1.000e+00 0.000e+00 2.000e+00
1 12 1.720300e+00 2.280e+00 2.401e-01 7.203e-01 1.520e+00
2 19 1.688289e+00 1.455e+00 3.430e-01 9.684e-01 6.358e-01
3 23 2.587102e+00 4.133e-01 1.000e+00 1.342e+00 6.046e-01
4 27 2.929079e+00 7.341e-02 1.000e+00 6.240e-01 2.038e-01
5 31 3.000225e+00 6.050e-04 1.000e+00 9.457e-02 5.761e-02
6 35 3.000027e+00 5.576e-04 1.000e+00 2.362e-02 1.620e-02
7 39 3.000000e+00 2.761e-05 1.000e+00 5.308e-03 4.556e-04
8 43 3.000000e+00 2.435e-09 1.000e+00 6.453e-05 2.124e-05
9 47 3.000000e+00 0.000e+00 1.000e+00 4.398e-09 1.192e-06
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.
<stopping criteria details>
x =
-3.00000000000053 5.92273862321821e-07 -0.999999999999825
Weitere Antworten (0)
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!