where t(i) are free variables, my code is the following:
cvx_solver mosek
%cvx_precision best
variable t(m)
S = zeros(n,n);
variable S(n,n) symmetric
minimize 1
subject to
S == t(1)*C{1}+t(2)*C{2}+t(3)*C{3}+t(4)*C{4}+t(5)*C{5}+t(6)*C{6}…
+t(7)*C{7}+t(8)*C{8}+t(9)*C{9}+t(10)*C{10}+t(11)*C{11}+t(12)*C{12}+t(13)*C{13};
S == semidefinite(n);
trace(S) == 1;
t’*ds == 0;
cvx_end
I want to replace the constraint S == t(1)*C{1}+t(2)*C{2}+t(3)*C{3}+t(4)*C{4}+t(5)*C{5}+t(6)*C{6}…
+t(7)*C{7}+t(8)*C{8}+t(9)*C{9}+t(10)*C{10}+t(11)*C{11}+t(12)*C{12}+t(13)*C{13} by a summation, so I can use the parameter n for the numbers of terms in the summand. Here C{} is a cell array, each cell is a matrix. How do I implement this in CVX? Thanks!
Use a 3D array. If you declare symmetric orsemidefinite, for instance, variable A(n,n,m) symmetric
each of the by by n matrices (2D slices of A), A(:,:,k) for k=1,…,m
will be symmetric. Similarly for semidefinite declaration in variable statement…
Perhaps this can be vectoriized… But here is a brute force way to it.
variable A(n,n,m) symmetric
S = zeros(n,n);
for k=1:m
S = S + t(k)*A(:,:,k);
end
S == semidefinite(n)
If you just wanted the sum of the 2D slices without multiplying them by their own t(i), you could use sum(A,3), which is what the for loop would create if t is a vector of ones.
Thank you! Here A(:,:,k) are actually data from the problem. the variables are actualy t(k). So I think I need to construct A(n,n,m) before I start the CVX, right?
In my previous post, just change variable A(n,n,m) symmetric
to variable t(n)
A has to be populated with the numerical values prior to the stattement in which it is used. That can be after cvx_begin if you want, provided it is populated before it is used. You can do a thousand lines of whatever you want in MATLAB in the middle of cvx_begin ... cvx_end. If the statements don’t involve CVX variables or expressions or commands, CVX (speaking as though it was a person), doesn’t know anything about them, and so doesn’t care.