Dummy variable in loop

43 views
Skip to first unread message

Marc Wijnand

unread,
May 15, 2015, 11:31:22 AM5/15/15
to yal...@googlegroups.com
I construct constraints inside a for loop. As this constraints involve Minkowski differences, I have to use dummy variables (see 'w' in this text).
Is it sufficient to define one sdpvar w outside of the loop and use this variable in each iteration, or should I define a matrix of different w's and use another one in each iteration?

SKETCH:
X = ...;
x = sdpvar(2,N);
w = sdpvar(2,1);
for i=1:N
  constraints=[constraints ismember(x(:,i)+w,X)];
end


Johan Löfberg

unread,
May 15, 2015, 2:25:33 PM5/15/15
to yal...@googlegroups.com
Is it the same variable or different variables? If it is the same w, you can define it outside loop. If it is different w, you can either define w as a matrix and index it, or define a new w variable inside the loop every iteration

Marc Wijnand

unread,
May 18, 2015, 4:20:33 AM5/18/15
to yal...@googlegroups.com
It are different variables (that are used in optimize() later on), so I will define w as a matrix.
_____

Just to be sure: is it possible to reuse an sdpvar in constraint set definitions?
It works for this example:
x = sdpvar(2,1);
P = eye(2);
Set1=[x'*P*x<=1];
Set2=[x'*P*x<=2];
plot(Set1)
plot(Set2)

Johan Löfberg

unread,
May 18, 2015, 4:23:39 AM5/18/15
to yal...@googlegroups.com
Not sure what the question is, but perhaps this is the answer

x = sdpvar(2,1);
P = -1 <= x <= 2;
Q = 3 <= x <= 4;
% Plot the two sets
plot(P);plot(Q)
% The set involving both P and Q is of course empty, as the same x is used  in both
plot([P,Q])



Message has been deleted

Marc Wijnand

unread,
May 18, 2015, 4:32:49 AM5/18/15
to yal...@googlegroups.com
Thank you for the quick reply.
You define the set X1
x=sdpvar(2,1);
P=eye(2);
X1=[x'*P*x<=1];
and afterwards
X2=[x'*P*x<=2];
The question was if x obtains any properties during the X1 definition that would cause the X2 definition to be invalid. Then it would be necessary to use two separate sdpvariables:
x=sdpvar(2,1);
y
=sdpvar(2,1);
X1
=[x'*P*x<=1];
X2=[y'
*P*y<=2]; %Typo corrected
Or is it safe to reuse x?

Johan Löfberg

unread,
May 18, 2015, 4:44:47 AM5/18/15
to yal...@googlegroups.com
A variable does not have any reference to which constraints it is being used in
Reply all
Reply to author
Forward
0 new messages