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 as a variable. is a CVX expression, because it involves the variable .The, ) is tassigned to a CVX expression involving , but is a double, not a CVX expression; so triggers the error message.sigmamy_holdE_user sigma(tmy_holdsigma
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