How to create constraints inside a loop?

78 views
Skip to first unread message

Daniel Dimitrov

unread,
Oct 26, 2016, 6:37:00 AM10/26/16
to YALMIP
Hello,

I an trying to create a YALMIP constraint object with a loop.

I the end I need something of the type

constraint = [x1 + x2 + x3 +....+xN >= 0]



So, instead of typing all that I'm trying to create it using a loop, but I have the feeling that YALMIP doesn't work as I expect it. Is this correct

x = sdpvar(N,1,'full');
constraints
= 0;

for  i = 1:N
constraints
= x(i) + constraints
end

constraints
= [constraints >=0]


Johan Löfberg

unread,
Oct 26, 2016, 6:43:46 AM10/26/16
to YALMIP
In your for-loop your adding variables and constraints, which doesn't make sense.

A for-loop would be
thesum = 0;

for  i = 1:N
thesum = x(i) + thesum
end
constraints = thesum >= 0

of course, in reality, you would preferably use some vectorization
constraints = sum(x)>=0



Daniel Dimitrov

unread,
Oct 26, 2016, 7:10:41 AM10/26/16
to YALMIP
I have tried that but I can still see that but I still see that after a couple of iterations of the loop - the variable thesum
still stays of size "sdpvar 1x1". Shouldn't it grow with i? Is there a way that I can eventually what the constraint looks like? I'm afraid that the after the loop is finished, the constraints won't look as I would expect them to

Johan Löfberg

unread,
Oct 26, 2016, 7:14:07 AM10/26/16
to yal...@googlegroups.com
No, why should it. Do you expect thesum to grow in this matlab code

x = [1 2 3];
thesum
= 0;
for i = 1:3
 thesum
= thesum + x(i);
end

Your expression x1 + x2 + x3 +....+xN is a scalar 1x1.

Daniel Dimitrov

unread,
Oct 26, 2016, 7:19:12 AM10/26/16
to YALMIP
I see, this makes sense, thank you
Reply all
Reply to author
Forward
0 new messages