problems with fmincon, how to solve?

8 views (last 30 days)
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.
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)

Accepted Answer

Hussein Ammar
Hussein Ammar on 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

More Answers (0)

Tags

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!