fmincon: proble defining the function
13 次查看(过去 30 天)
显示 更早的评论
Hi, I am trying to solve a non linear constrained optimization. I report the few lines of code below:
%Here are the functions I use to define the objective function and the constraints
function Objective = definefunction(x)
comp1 = double(rand(180,3))
X = reshape(x,3,3);
comp2 = comp1*X;
comp3 = comp2(1:end, 3);
Objective = 0.5*comp3'*comp3/size(comp3,1);
end
function [c,ceq] = constraints(x)
Betas= rand(1,3)
c = [];
ceq(1) = x(1)^2 + x(4)^2 + x(7)^2;
ceq(2) = x(2)^2 + x(5)^2 + x(8)^2;
ceq(3) = x(3)^2 + x(6)^2 + x(9)^2;
ceq(4) = x(1)*x(2) + x(4)*x(5) + x(7)*x(8);
ceq(5) = x(1)*x(3) + x(4)*x(6) + x(7)*x(9);
ceq(6) = x(2)*x(3) + x(5)*x(6) + x(8)*x(9);
ceq(7) = x(4)*Betas(1,1) + x(5)*Betas(1,2) + x(6)*Betas(1,3);
ceq(8) = x(7)*Betas(1,1) + x(8)*Betas(1,2) + x(9)*Betas(1,3) ;
end
% define the objective function
f=definefunction(x);
% define constraints
nonlcon = @constraints;
% other inputs to the fmincon function
A = []; % No other constraints
b = [];
Aeq = [];
beq = [];
lb = [1., 1., 1., 0., 0., 0., 0., 0., .0];
ub = [1., 1., 1., 0., 0., 0., 0., .0, .0];
x0 = [0., 0., 0., 0., 0., 0., 0., .0, .0];
%implement
x = fmincon(f,x0,A,b,Aeq,beq,lb,ub,nonlcon)
0 个评论
采纳的回答
Torsten
2023-3-9
编辑:Torsten
2023-3-9
f = @definefunction;
instead of
f=definefunction(x);
And you cannot use random inputs on each call - thus
comp1 = double(rand(180,3))
and
Betas= rand(1,3)
within definefunction and nonlcon is not allowed because "fmincon" is a deterministic solver.
And the constraints
ceq(1) = x(1)^2 + x(4)^2 + x(7)^2;
ceq(2) = x(2)^2 + x(5)^2 + x(8)^2;
ceq(3) = x(3)^2 + x(6)^2 + x(9)^2;
immediately yield x(1) = x(2) = ... = x(9) = 0.
Maybe you could explain in your own words what problem you are trying to solve. It cannot be deduced from the code you provided.
2 个评论
Walter Roberson
2023-3-9
Same with the betas in the constraints. The only routine that might work with stochastic systems is patternsearch (at least until you get into stochastic differential equations.)
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!