跑代码的时候遇到的问题
matlab
%% 分解为功率和单位模,交替优化功率分量和单位模
clc;clear;
Nt = 8; % 发射天线数目 8
Nr = 4; % 接收天线数目 4
Ne = 6; % Eve天线数目 6
noisePower = 1;
SNRTotaldB = -5:5:35; %%%%最大发射功率单位dB
SNRTotal = 10.^(SNRTotaldB./10); %%%% 最大发射功率约束,单位dB
channelNum = 2000; %%% 生成10次信道
%tilde_f = sqrt(1/2) * ( randn(Nt,1) + j*randn(Nt,1) );
% tilde_f = tilde_f / norm(tilde_f);
[hBsUserSet] = channelGenerate(Nr,Nt,channelNum);%生成100次信道HBsUe,H维度NrxNt
[hBsEveSet] = channelGenerate(Ne,Nt,channelNum);%生成100次信道HBsEve,H维度NexNt
rateSet = zeros(channelNum,length(SNRTotal));%生成最终速率数组
rateSetCmp = zeros(channelNum,length(SNRTotal));%生成最终速率数组
cvx_begin
cvx_end
for indexh = 1:channelNum
hBsUser = hBsUserSet(:,:,indexh); %具体单次信道hBsUser实现
hBsEve = hBsEveSet(:,:,indexh); %具体单次信道hBsEve实现
objBeamformingCollect = [];
objCmpUnit = [];
for indexp = 1:length(SNRTotaldB)
%Q维度为NtxNt,qCmpUnit是对比方案,代表在各个天线上分配相同发射功率
QCmpUnit = diag(SNRTotal(indexp)/Nt*ones(1,Nt));
%%%% 计算基于协方差矩阵QCmpUnit的安全可达造率
rateCmpUnit = 1/log(2)*log_det(eye(Nr)+hBsUser*QCmpUnit*hBsUser')... % matlab 换行指令。。
- 1/log(2)*log_det(eye(Ne)+hBsEve*QCmpUnit*hBsEve') ;
objCmpUnit = [objCmpUnit, max(real(rateCmpUnit),0)];
%% 采用交替优化方法,表达式为 tilde_f 和 tilde_p
%% log(1+tilde_p * norm(hBsUser*tilde_f)^2) - log(1+tilde_p * norm(hBsEve*tilde_f)^2)
objBefore =10;
objAfter = 10 * objBefore;
count = 0;
objAfterSet = [];
tilde_f = sqrt(1/2) * ( randn(Nt,1) + j*randn(Nt,1) );
while(abs((objAfter-objBefore)/objBefore)>= 5e-4 && count~=50)
fprintf('第%g个信道实现,SNR为%g时的,第%g次内部迭代\n',indexh, SNRTotaldB(indexp), count);
objBefore = objAfter;
%%%%%%%
% tilde_p
if( norm(hBsUser*tilde_f) >= norm(hBsEve*tilde_f) )
tilde_p = SNRTotal(indexp);
else
tilde_p = 0;
end
% tilde_p = SNRTotal(indexp);
fprintf('\n\n');
fprintf('tilde_p的值为%g\n', tilde_p);
% tilde_f
A = eye(Nt) + tilde_p * (hBsUser'*hBsUser);
B = eye(Nt) + tilde_p * (hBsEve'*hBsEve);
[V,D] = eig(A,B);
% [V,D] = eig(A,B) 返回广义特征值的对角矩阵 D 和满矩阵 V,其列是对应的右特征向量,使得 A*V = B*V*D。
Max_Eigen_value= max(max(D)); %%最大广义特征值
d = diag(D);%%% D是对角矩阵,提取对角元素
[pos1] = find(d == Max_Eigen_value);
tilde_f = V(:,pos1)/norm(V(:,pos1)); %最大广义特征值对应的归一化广义特征向量
%%%%%
objAfter = 1/log(2)*log(1+tilde_p * norm(hBsUser*tilde_f)^2)...
- 1/log(2)*log(1+tilde_p * norm(hBsEve*tilde_f)^2);
objAfter2 = 1/log(2)*log_det(eye(Nr)+tilde_p * hBsUser* (tilde_f*tilde_f') *hBsUser')... % matlab 换行指令。。
- 1/log(2)*log_det(eye(Ne)+tilde_p*hBsEve* (tilde_f*tilde_f') *hBsEve') ;
fprintf('obj2 - obj1 = %g\n', objAfter2 - objAfter );
if(abs(objAfter2 - objAfter) > 1)
fprintf('1/log(2)*log(1+tilde_p * norm(hBsUser*tilde_f)^2) is %g \n', 1/log(2)*log(1+tilde_p * norm(hBsUser*tilde_f)^2) );
fprintf('1/log(2)*log_det(eye(Nr)+tilde_p * hBsUser* (tilde_f*tilde_f) *hBsUser is %g \n', 1/log(2)*log_det(eye(Nr)+tilde_p * hBsUser* (tilde_f*tilde_f') *hBsUser') );
fprintf('- 1/log(2)*log(1+tilde_p * norm(hBsEve*tilde_f)^2) is %g \n', - 1/log(2)*log(1+tilde_p * norm(hBsEve*tilde_f)^2) );
fprintf('- 1/log(2)*log_det(eye(Ne)+tilde_p*hBsEve* (tilde_f*tilde_f) *hBsEve) is %g \n', real(- 1/log(2)*log_det(eye(Ne)+tilde_p*hBsEve* (tilde_f*tilde_f') *hBsEve')) );
fprintf('内部 is %g\n', det(eye(Ne)+tilde_p*hBsEve* (tilde_f*tilde_f') *hBsEve'));
fprintf('用log_det了 is %g\n', log_det(eye(Ne)+tilde_p*hBsEve* (tilde_f*tilde_f') *hBsEve'));
fprintf('用log(det(了 is %g\n', log(det(eye(Ne)+tilde_p*hBsEve* (tilde_f*tilde_f') *hBsEve')));
end
count = count + 1;
objAfterSet=[objAfterSet, objAfter];
end
objIter = max(real(objAfter),0);
objBeamformingCollect = [objBeamformingCollect, objIter];
end
rateSet(indexh, :) = objBeamformingCollect;
rateSetCmp(indexh, :) = objCmpUnit;
fprintf('The %gth channel realization is over \n', indexh);
end
average_rateSet = mean(rateSet);
average_rateSetCmp = mean(rateSetCmp);
%% 画图
hold off;
plot(SNRTotaldB, average_rateSet,'color',[0.22 0.37 0.06],'linestyle','-','marker','^','LineWidth',1.5); %
hold on;
plot(SNRTotaldB, average_rateSetCmp,'m->','LineWidth',1.5);
% axis tight;
xlabel('最大发射功率(dB)','fontsize',11);
ylabel('可达安全速率(bps/Hz)','fontsize',11);
legend('波束赋形优化方案','等功率发送对比方案','Location','northwest','fontsize',11);
grid on;
%%%%%%
% A = rand(2,9)
%
% A * A'
%
% norm(A)^2
%
%
%
其中时不时会出现这种情况:也就是当我不是在用cvx来优化问题,只是想用log_det函数去代替log(det())函数的功能时,log_det有时候会跳出这样的问题。
cvx forum上没有查到相关的解释