fmincon: proble defining the function

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

Torsten
Torsten on 9 Mar 2023
Edited: Torsten on 9 Mar 2023
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 Comments
Walter Roberson
Walter Roberson on 9 Mar 2023
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.)
Vittoria Iannotta
Vittoria Iannotta on 10 Mar 2023
Thanks for the hints, I will try and come back. ok sorry for the rand(). I did it because in the routine I would have actual values coming from a regression, I did not want to overload the example. But you are right that written like this does not make sense.

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!