# How to solve ''Unable to perform assignment because value of type 'cvx' is not convertible to 'double'

Here I got this code and error message, could you please help me to solve it?

`cvx_begin
variable E_user(n) nonnegative
variable sigma(n) nonnegative
variable Sigma(n) nonnegative
variable Omega(n) nonnegative
obj=0;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

``````        sigma=zeros(1,K_num); Sigma=zeros(1,K_num); Omega=zeros(1,K_num);

% Calculate sigma
for t=1:K_num
my_hold=zeros(M*N,M*N);
for i=1:K_num
if i~=t
my_hold=my_hold+E_user(i)*G_DDMA(:,:,i)*G_DDMA(:,:,i)';
end
end
sigma(t)=trace(pinv(D(:,:,t))*U(:,:,t)*H(:,:,t)*my_hold*H(:,:,t)'*U(:,:,t)'*(pinv(D(:,:,t)))');
end

% Calculate Sigma
for t=2:K_num
my_hold=zeros(M*N,M*N);
for i=2:K_num
if i~=t
my_hold=my_hold+E_user(i)*G_DDMA(:,:,i)*G_DDMA(:,:,i)';
end
end
Sigma(t)=trace(pinv(D(:,:,1))*U(:,:,1)*H(:,:,1)*my_hold*H(:,:,1)'*U(:,:,1)'*(pinv(D(:,:,1)))');
end
my_hold=zeros(M*N,M*N);
for i=3:K_num
my_hold=my_hold+E_user(i)*G_DDMA(:,:,i)*G_DDMA(:,:,i)';
end
Sigma(1)=trace(pinv(D(:,:,2))*U(:,:,2)*H(:,:,2)*my_hold*H(:,:,2)'*U(:,:,2)'*(pinv(D(:,:,2)))');

% Calculate Omega
for i=2:K_num
Omega(i)=trace(pinv(D(:,:,1))*U(:,:,1)*H(:,:,1)*G_DDMA(:,:,i)*G_DDMA(:,:,i)'*H(:,:,1)'*U(:,:,1)'*(pinv(D(:,:,1)))');
end
Omega(1)=trace(pinv(D(:,:,2))*U(:,:,2)*H(:,:,2)*G_DDMA(:,:,1)*G_DDMA(:,:,1)'*H(:,:,2)'*U(:,:,2)'*(pinv(D(:,:,2)))');

sigma=real(sigma);
Sigma=real(Sigma);
Omega=real(Omega);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for i=2:n
obj=obj+log(1+2*beta(i)*sqrt(E_user(1)+alpha*Sigma(1)+N0)-beta(i)^2)*(N0+alpha*sigma(i)*Omega(1))/log(2);
end
obj=obj+log(1+2*beta(1)*sqrt(E_user(2)+alpha*Sigma(2)+N0)-beta(1)^2)*(N0+alpha*sigma(1)*Omega(2))/log(2);
maximize (obj)
E_sum=0;
for i=1:n
E_sum=E_sum+E_user(i);
end
subject to
E_sum<=E_total;
cvx_end`
``````

The error was ‘Unable to perform assignment because value of type ‘cvx’ is not convertible to ‘double’.’ in the line of ‘sigma(t)’

You seem to be confusing variable with expressions.

`sigma=zeros(1,K_num);` supersedes (overwrites) the earlier declaration of `sigma` as a variable. `my_hold` is a CVX expression, because it involves the variable `E_user`.The, ` sigma(t`) is tassigned to a CVX expression involving `my_hold`, but `sigma` is a double, not a CVX expression; so triggers the error message.

You should declare

``````expression sigma(n)
expression Sigma(n)
expression Omega(n)
``````

instead of as variables. Declared expressions are automatically initialized to all zeros by CVX. If you need them to be nonnegative, you will need to add the constraints

``````sigma >= 0
Sigma >= 0
Omega >= 0
``````

Thank you so much for your answer!

After I correct the expression error, I still got another error in the line to calculate the ‘obj’ within a for loop

``````cvx_begin
variable E_user(n) nonnegative
expression sigma(n)
expression Sigma(n)
expression Omega(n)
obj=0;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%             sigma=zeros(1,K_num); Sigma=zeros(1,K_num); Omega=zeros(1,K_num);

% Calculate sigma
for t=1:K_num
my_hold=zeros(M*N,M*N);
for i=1:K_num
if i~=t
my_hold=my_hold+E_user(i)*G_DDMA(:,:,i)*G_DDMA(:,:,i)';
end
end
sigma(t)=trace(pinv(D(:,:,t))*U(:,:,t)*H(:,:,t)*my_hold*H(:,:,t)'*U(:,:,t)'*(pinv(D(:,:,t)))');
end

% Calculate Sigma
for t=2:K_num
my_hold=zeros(M*N,M*N);
for i=2:K_num
if i~=t
my_hold=my_hold+E_user(i)*G_DDMA(:,:,i)*G_DDMA(:,:,i)';
end
end
Sigma(t)=trace(pinv(D(:,:,1))*U(:,:,1)*H(:,:,1)*my_hold*H(:,:,1)'*U(:,:,1)'*(pinv(D(:,:,1)))');
end
my_hold=zeros(M*N,M*N);
for i=3:K_num
my_hold=my_hold+E_user(i)*G_DDMA(:,:,i)*G_DDMA(:,:,i)';
end
Sigma(1)=trace(pinv(D(:,:,2))*U(:,:,2)*H(:,:,2)*my_hold*H(:,:,2)'*U(:,:,2)'*(pinv(D(:,:,2)))');

% Calculate Omega
for i=2:K_num
Omega(i)=trace(pinv(D(:,:,1))*U(:,:,1)*H(:,:,1)*G_DDMA(:,:,i)*G_DDMA(:,:,i)'*H(:,:,1)'*U(:,:,1)'*(pinv(D(:,:,1)))');
end
Omega(1)=trace(pinv(D(:,:,2))*U(:,:,2)*H(:,:,2)*G_DDMA(:,:,1)*G_DDMA(:,:,1)'*H(:,:,2)'*U(:,:,2)'*(pinv(D(:,:,2)))');

sigma=real(sigma);
Sigma=real(Sigma);
Omega=real(Omega);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for i=2:n
obj=obj+log(1+2*beta(i)*sqrt(E_user(1)+alpha*Sigma(1)+N0)-beta(i)^2)*(N0+alpha*sigma(i)*Omega(1))/log(2);
end
obj=obj+log(1+2*beta(1)*sqrt(E_user(2)+alpha*Sigma(2)+N0)-beta(1)^2)*(N0+alpha*sigma(1)*Omega(2))/log(2);
maximize (obj)
E_sum=0;
for i=1:n
E_sum=E_sum+E_user(i);
end
subject to
E_sum<=E_total;
cvx_end
``````

The error is ‘Disciplined convex programming error:
Cannot perform the operation: {concave} .* {real affine}’

Does this mean my objective is becoming a concave problem which can not be solved, where the cvx code are all fine.

You are violating CVX’s rules. Your first task is to prove that your optimization problem is a convex optimization problem. if it is, a reformulation to satisfy CVX’s DCP rules might be possible. If it is not a convex optimization problem, such a reformulation is not possible.

Ok, make sense. Thank you so much for your time and patience!