Creating Dynamic Variables with Constraints (avoid for loop)

Uncategorized
Feb 6, 2014
D

Hello,

I am trying to avoid using for loops as instructed in the cvx user guide, however, I cant seem to figure out how to do so for the constraints:
My code:

% declare initial variables - avoid doing this in while loop ever time
% size of X is set at runtime and does not change
dim = 2;
Zcell = cell(length(X));
for i = 1:length(X)
    Zcell{i} = eval(sprintf('Z%d (%d,%d)',i,d,d));
end

while (abs(diff) >= tol)
    cvx_begin quiet
        variables Zcell
        minimize( myFunction(Z) )
        subject to
            ?????
    cvx_end
    
    ...
end

How can I substitute ???? with something that says each Zi is PSD?

M

If all of the matrices are the same size, put them in an N-D array. Then you can constrain them all to be positive definite at once:

variables Z(d,d,n) semidefinite

or

variables Z(d,d,n)
Z == semidefinite([d,d,n])

That said, if you can’t avoid a for loop, don’t worry about it. It’s not going to prevent the model from being solved. It’s a performance issue, but your first priority is to get the model to work!

D

thanks! It does indeed work, we’re in the process of trying to speed it up. Ill try that thanks!