problems with fmincon, how to solve?

4 次查看(过去 30 天)
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)

采纳的回答

Hussein Ammar
Hussein Ammar 2020-8-29
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 个)

类别

Help CenterFile Exchange 中查找有关 Solver Outputs and Iterative Display 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by