Why does it seem that the ~isinf(cvx_optval) and co( ~isinf(cvx_optval)) are both not work
1 次查看(过去 30 天)
显示 更早的评论
I write a code to produce cvx_optval,however,in some result of the loop is inf,so i use the "~isinf" to know how many value is not inf,but i don't know why,the result of "~isinf " is wrong
bd=3
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%The code below will find the optimal value,and will produce cvx_optval
for l=1:bd
hat_p_up=0.0824
%OP4
%declare
K=4;
N=4;
L=5;%distance between RX & TX
xi=10^-4%tolerence between
nois_var_hk_2pow=0.1*(L^(-2.5))*10;%W,0.1*(L^(-2.5)),if this unit is dbm
%w_nois_var_hk_2pow=10^(-3)*10^(0.1*nois_var_hk_2pow)
nois_var_ak_2pow=[1.0000e-10 1.0000e-10 1.0000e-10 1.0000e-10 ];
nois_var_dk_2pow=[1.0000e-08 1.0000e-08 1.0000e-08 1.0000e-08 ];
bar_r=[10 10 10 10]
P_T=10
co = zeros(3,1);
h_1=sqrt(nois_var_hk_2pow/2)*(randn(N,1)+1i*randn(N,1));
h_2=sqrt(nois_var_hk_2pow/2)*(randn(N,1)+1i*randn(N,1));
h_3=sqrt(nois_var_hk_2pow/2)*(randn(N,1)+1i*randn(N,1));
h_4=sqrt(nois_var_hk_2pow/2)*(randn(N,1)+1i*randn(N,1));
h_kk=cat(2,h_1 ,h_2 ,h_3, h_4)
for n=1:4
h_k{n}=h_kk(1:4 , n);
n=n+1;
end
%==========================
cvx_begin
variable FNNK_up(N,N,K) semidefinite;%c7
variable rho_k_up(1,1,K) semidefinite;
%==========================
Fkk_up=cat(2,FNNK_up);
up=0
for o_up=1:4
Fk_up{o_up}=Fkk_up(1:4,o_up+3*up:4*o_up)
up=up+1;
end
tr_ace_up=0
for t=1:K
tr_ace_up=tr_ace_up+trace(Fk_up{t})
end
%====================================
%object function
minimize( tr_ace_up )
%====================================
%Constraint
subject to
%c3
rho_k_up<=1;
%===================================================
%c5
c5_left_hand_up = 0;
for k = 1:K
sum_5_up = 0;
for j = 1:K
if j ~= k
sum_5_up = sum_5_up + h_k{k}' * Fk_up{j} * h_k{k};
end
end
c5_left_hand_up = c5_left_hand_up - sum_5_up+ (h_k{k}' * Fk_up{k} * h_k{k}*inv_pos(bar_r(1)))
c5_right_hand_up= nois_var_ak_2pow(1)+ ( nois_var_dk_2pow(1)*inv_pos(rho_k_up(k)) )
real( c5_left_hand_up ) >= c5_right_hand_up
end
%===================================================
%c10
c10_left_hand_up = 0;
sum_10_up = 0;
for j = 1:K
sum_10_up= sum_10_up + h_k{k}' * Fk_up{j} * h_k{k};
end
c10_left_hand_up = c10_left_hand_up + sum_10_up+nois_var_ak_2pow(1)
c10_right_hand_up=hat_p_up*inv_pos(1-rho_k_up(k))
real(c10_left_hand_up)>= c10_right_hand_up
cvx_end
end
%The code above will find the optimal value,and will produce cvx_optval
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
P_Tp=pig_tra_up/1
co(bd) = ~isinf(cvx_optval)
co_number=co( ~isinf(cvx_optval))
The window show me
co= 0
0
1
co_number=
0
However,i see the number of every cvx_optval in every loop,i found the cvx_optval=10.7234,inf ,349071,so theorically,the real answer should be
co= 1
0
1
co_number=
10.7234
(there is nothing,because the value is inf)
34.9071
So it seems that ~isinf(cvx_optval) and co( ~isinf(cvx_optval)) are both not work,so if i want to let the code become the theorical value,there must be a mistake in my code,can anyone help me?
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/210620/image.jpeg)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/210621/image.jpeg)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/210622/image.jpeg)
0 个评论
回答(1 个)
Stephen23
2019-3-27
编辑:Stephen23
2019-3-27
"...so theorically,the real answer should be"
co= 1
0
1
Nope. You defined co like this:
co(bd) = ...
where bd is always 3, so you never allocate to any element of co except the third one. The other elements will simply use the default logical value (false), which matches exactly what you describe.
Most likely you need to move that allocation inside the loop, and use the (badly named) loop iterator l as the index:
co(l) = ...
"...there must be a mistake in my code..."
Part of the problem is that your code is very badly aligned. Badly aligned code is one way that beginners hide basic bugs in their code, because badly aligned code makes understanding the code logic much harder. You should align your code using the default MATLAB Editor settings. You can also align existing code: select all code, then press ctrl+i.
5 个评论
Stephen23
2019-3-28
Undefined function or variable 'cvx_begin'.
Error in Untitled (line 30)
cvx_begin
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Startup and Shutdown 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!