# How to change Matlab sym to cvx variable?

I have a 3D matrix `At`, `n x n x nvar`, and `nvar` variables `y1...y25`. I was easily able to feed the variables into cvx:

``````cvx_begin sdp;
variable A(2*nbus,2*nbus) symmetric;
variable y(1,nvar);
maximize sum(y*Mfun);
A ==  subs(At,yt,y(1,nvar));
``````

However, the equality on the last line is giving me this error:

``````Error using sym/subs>normalize (line 197)
Substitution expression X must be a symbolic, cell, or numeric array.
[X2,Y2,symX,symY] = normalize(X,Y); %#ok
Error in sym/subs (line 125)
``````

I have tried to set up that equality a few different ways:

Using the same `for` loop I created `At` with in the first place (using `Cfun`, a matrix of doubles):

``````for k = 1:nvar
At = At + yt(k)*Cfun(:,:,k);
end
``````

Which gives this error:

``````Error: File: proj.m Line: 124 Column: 11
Illegal use of reserved keyword "for".
``````

I didn’t really expect that one to work, but figured it was worth a try.

I have also tried using `bsxfun` to create the matrix inside cvx, but I don’t believe bsxfun is capable of handling symbolics. `repmat` is an alternative to ‘bsxfun’ which I haven’t attempted yet.

The reason I am trying to do the problem in this way is to create a routine that can use cvx to solve an optimal power flow for any number/combination of buses, without have to create variable individually each time.

Posted this three places, then spent 30 more minutes on it and figured it out. Rather than writing the for loop inside the equality constraint, I wrote it cvx_begin after the variable declaration:

``````% Solve using CVX
cvx_begin sdp;
variable A(2*nbus,2*nbus) symmetric;
variable y(1,nVar);
maximize sum(y*Mfun);
for k = 1:nVar
At = At + y(k)*Cfun(:,:,k);
end
for k = 1:size(cY,3)
At = At + cY(:,:,k);
end
A ==  At;
for k = 1:nIneq
y(k)>=0;
end
A>=0;
cvx_end;``````