# SDP constraint formulation with forloop

This is a syntax question. I am trying to construct a constraint ensuring sum(A*x) is PSD while minimizing x

In the following code I am adding each term of the summation manually (6 in this case but may be a lot more), it solves with no problem. None of the loops I tried work, is it possible to construct the matrix first including the variables, then set the >=0 constraint? If so, how?

Also, how do I set a constraint ensuring each item of the X array is greater than or equal to 0 in the SDP mode? The constraint I put in (commented out below) results in an error.

``````cvx_begin sdp
variables X(6)
minimize(sum(X))
subject to
A(:,:,1).*X(1)+...
A(:,:,2).*X(2)+...
A(:,:,3).*X(3)+...
A(:,:,4).*X(4)+...
A(:,:,5).*X(5)+...
A(:,:,6).*X(6)>=0
% X>0
cvx_end
``````

Thank you so much for reading!

Tim

``````n = size(A,3);
cvx_begin sdp
variable X(n)
expression Constraint_sum
Constraint_sum = 0;
minimize(sum(X))
for i=1:n, Constraint_sum = Constraint_sum + A(:,:,i) * X(i); end
Constraint_sum >= 0
X(:) >= 0 % but X >= 0 is also fine, so I don't know what your problem is
cvx_end
``````

I ran this with the A in your linked data_2.mat problem (18 by 18 by 10); and it solved to a result of numerically about all zeros.
I’m not sure exactly how well formulated or interesting your model is, but that’s for you to work out.

You might also want to think about the scaling on your sample A matrix. Having your entries be around 1e9 probably is not a wonderful thing, but I’m not sure it is causing harm either, though. Perhaps you should use

``````A = A/1e9;
``````

The loop works perfectly, and scaling works as well.

Though the constraint X( >= 0 or X>=0 is giving the following error for any of the solvers.

``````Subscript indices must either be real positive integers or logicals.

Error in cvx_extract (line 344)
tmpv = sum(dbcA(temp,:)~=0,2)==1;

Error in cvx_solve (line 26)
[ At, cones, sgn, Q, P, exps, dualized ] = cvx_extract( shim.config, shim.name );

Error in cvx_finish (line 57)
[ status, result, bound, iters, tol ] = cvx_solve;

Error in cvx_end (line 11)
evalin( 'caller', 'cvx_finish' );

Error in optTest_3 (line 10)
cvx_end
``````

Any ideas?

Thanks! Tim

Tim,

I had run my posted code without error message when I first posted it. I just tried it again in a fresh MATLAB session, and it executes just fine, whether using X >= 0 or X( >= 0, which should be and are equivalent. By the way, if X is square, you are in SDP mode, and you want X >= 0 elementwise, not in LMI sense, you can use X( >= 0. That’s why I showed it, even though in your case, X is not square, so X >= 0 can’t be interpreted to be in LMI sense.

Please post you entire MATLAB/CVX code starting from a fresh MATLAB session. What version/build of CVX are you using? If your problem is not due to something which is obvious to me, you may have to wait until mcg arrives.

I ran the exact same code as I posted above, and the same error results.

The version is as follows,

``````>> cvx_version

---------------------------------------------------------------------------
CVX: Software for Disciplined Convex Programming       (c)2014 CVX Research
Version 3.0beta, Build 1161 (07cf164)              Mon Mar 30 18:43:44 2015
---------------------------------------------------------------------------
``````

Update: I reverted back to the following, and it runs perfectly!

``````---------------------------------------------------------------------------
CVX: Software for Disciplined Convex Programming       (c)2014 CVX Research
Version 2.1, Build 1107 (ad3582d)                  Mon Mar 30 18:43:44 2015
---------------------------------------------------------------------------
``````

Something to look into?

You should file a bug report for 3.0beta, Build 1161 .

Well, that is unless my code is illegal (invalid expression operations?) and mcg decided to “lay down the law” in 3.0 and close a loophole in 2.1.

Because I haven’t yet felt a compelling need for the new features in 3.0, can’t have that installed simultaneously with 2.1, and don’t want the hassle of swtiching back and forth, I’m biding my time and waiting for 3.0 to mature a little more before switching over.

Hi Tim,

It seems I run into the same issue that you had before. Could you let me know how you reverted back into the old version? Also, have you successfully ran it in the up-to-date CVX?

Thanks,

I just ran my above program successfully using cvx_3.0beta build 1177, which is the most current version of cvx 3.0beta. I never tried it in a previous build of 3.0beta.