fmincon: proble defining the function
Show older comments
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)
Accepted Answer
More Answers (0)
Categories
Find more on Write Constraints in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!