problems with fmincon, how to solve?
5 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
fernando brito
am 28 Aug. 2020
Beantwortet: Hussein Ammar
am 29 Aug. 2020
can someone help me solve this problem, I tried to modify the code but it didn't solve it. I need to minimize the following equation.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/353225/image.jpeg)
objective function:
function cosmyfun(x)
cost = @(x) dfghjhgfx(1)^2 + x(2)^2 - 5*x(1) - 6*x(2) + 15;
end
function with restrictions of inequality and equality:
function [c,ceq]=mycon(x)
c(1) = x(1)^2 + x(2)^2 - 4*x(1) - 3*x(2) + 5;
c(2) = -x(1)^2 - x(2)^2 + 4*x(1) + 3*x(2) - 10;
ceq = 2*x(1)^2 + 2*x(2)^2 - 3*x(1) - 3*x(2) - 2;
end
script:
clc
clear all
Aeq=[];
beq=[];
A=[];
b=[];
lb=[-3,-3];
ub=[3,3];
x0=[4,4];
options = optimoptions('fmincon','Algorithm','sqp','Display','iter-detailed','MaxFunctionEvaluations',100000, 'MaxIterations',2000, 'OptimalityTolerance' ,1e-10);
[x,cost] = fmincon(@(x) myfun,x0,A,b,Aeq,beq,lb,ub,mycon)
error:
Error in mycon (line 2)
c(1) = x(1)^2 + x(2)^2 - 4*x(1) - 3*x(2) + 5;
Error in Example (line 11)
[x,cost] = fmincon(@(x) myfun,x0,A,b,Aeq,beq,lb,ub,mycon)
0 Kommentare
Akzeptierte Antwort
Hussein Ammar
am 29 Aug. 2020
Your problem is convex, so you can use the CVX package if you want. Also, the solution should be the global minimum; not a local minimum. To use fmincon, do the following:
Equality/nonEquality constraint:
function [c,ceq]=mycon(x)
c(1) = x(1)^2 + x(2)^2 - 4*x(1) - 3*x(2) + 5;
c(2) = -x(1)^2 - x(2)^2 + 4*x(1) + 3*x(2) - 6; % notice the fixed typo
ceq = 2*x(1)^2 + 2*x(2)^2 - 3*x(1) - 3*x(2) - 2;
end
Construct the problem and call fmincon:
clc
clear all
Aeq=[];
beq=[];
A=[];
b=[];
% if you are not sure that vector x is within lb and ub do not restrict it
lb = []; %lb=[-3,-3];
ub = []; %ub=[3,3]; %
x0=[4,4];
myObj = @(x) x(1)^2 + x(2)^2 - 5*x(1) - 6*x(2) + 15;
nonlcon = @mycon;
options = optimoptions('fmincon','Algorithm','sqp','Display','iter-detailed','MaxFunctionEvaluations',100000, 'MaxIterations',2000, 'OptimalityTolerance' ,1e-10);
[x,cost] = fmincon(myObj,x0,A,b,Aeq,beq,lb,ub,nonlcon)
Output (global minimum):
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 =
1.6450 1.9007
cost =
1.6896
0 Kommentare
Weitere Antworten (0)
Siehe auch
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!