fmincon error "Subscript indices must either be real positive integers or logicals"
1 view (last 30 days)
Show older comments
Sheldon Jadamson
on 17 Oct 2015
Answered: Walter Roberson
on 18 Oct 2015
I'm trying to solve a constrained optimisation problem with 4 non-linear inequality constraints and I'm getting the error "Subscript indices must either be real positive integers or logicals". I've created a dummy version of my code for a really simple optimisation problem to try to understand what I'm doing wrong (I realise I have linear constraints here where the non-linear constraints are supposed to go). I've read through quite a few other answers here and have just ended up more confused. Any help would be much appreciated.
syms bh bl wh wl
x(1)=bh;
x(2)=bl;
x(3)=wh;
x(4)=wl;
obj=@(x)(-(bh*bl*wh*wl));
c=@(x)[(bh+bl+wh+wl-20);(bh-2)];
ceq=[];
constraints=@(x)deal(c(x),ceq(x));
z=fmincon(obj,[0;0;0;0],[],[],[],[],[],[],constraints);
0 Comments
Accepted Answer
Walter Roberson
on 18 Oct 2015
Constraints must evaluate to numeric values, not symbolic values.
Objective functions must evaluate to numeric values, not symbolic values.
It is not forbidden to use symbolic formula as part of your objective, but what is returned by the objective must be numeric, not symbolic.
I think what you are looking for is more like
obj = @(x) -prod(x);
c = (@x) [sum(x)-20; x(1)-2];
0 Comments
More Answers (0)
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!