# Expression holder not working?

Dear cvx community,

I just began using cvx with Matlab and I encountered the following problem:

``````cvx_begin
variable Tlb(1,t);
expression T(n,t);
expression Tlsys(1,t);
expression b(n,1);
T(:,1) = T_init;
Tlsys(1) = Tsys_init;

for j=2:t
% compute vector b; b depends on T(:,j-1) and Tlb(j-1)
b(1) = -rho*Ac*dx*cp*T(1,j-1)/dt - (Tlb(j)*cp*mbnom+U*Ap*Tamb);
for i = 2:n-1
b(i) = -rho*Ac*dx*cp/dt*T(i,j-1) - U*Ap*Tamb;
end
b(n) = -rho*Ac*dx*cp/dt*T(n,j-1) -(U*Ap*Tamb+msys*cp*Tlsys(j));

T(:,j) = A\b;
Tlsys(j) = T(1,j) - Qsys(j-1)/msys/cp;
end

minimize sum((Tlb(2:end)-T(n,1:end-1)).*mbnom.*cp.*c(1:end-1)).*dt;
subject to
Tlb(1) == Tlbinit;
Tlb(2:length(tijd)) <=80;
(Tlb(2:length(tijd))-T(n,1:length(tijd)-1))*mbnom*cp <=Qbnom;
Tlsys(2:length(tijd)) >=20;

cvx_end
``````

When I run the code I get the following error:
??? The following error occurred converting from cvx to double:
Error using ==> double
Conversion to double from cvx is not possible.

The errors come up in the lines where I calculate b(1), b(i) and b(n). When I replace these lines with b(1), b(i), b(n) = 5 (just a random constant for testing if this is the problem) I don’t get any errors and cvx minimizes the problem.

I specifically defined T, Tlsys and b to be cvx expressions, yet I get the error. What should I do?

I’m not seeing an obvious problem with your construction here. You might want to submit a bug report to http://support.cvxr.com, with enough data to reproduce the problem (In particular, the numeric values of T_init, etc.)

I have found the problem myself. Previously I constructed b in this way:

``````for i = 1 ... n
b(i) = (some expression)
end
``````

And then I received an error.

Now I do it like this:

``````b = [];
for i = 1..n
b = [b; (some expression)];
end
``````

And the error is gone!

I’m glad you found a solution! But in fact, the first approach should still work as long as b is declared as an expression in advance. Regardless, well done!

I’m glad you give a suitable solution,but it’s hard for me to understand it as if the initial value would affect the optimization! If you can give some numerical results to explain the problem, we will be happy !