I have error in CVX code
code is
clc
clear all
iterations = 10;
%% Parameters
Mr=4; % no.of BS antenna
K=16; % no. of RIS1 reflecting elements
U=4; % no. of uplink users
L=1; % no. of evesdroppers
P_T=db2pow(30); % total power budget
P_u=db2pow(10); % consume power at uplink transmission
P_d=db2pow(10); % consume power at downlink transmission
eta_i=db2pow(20); %amplification factor
epsilon=0.05; %estimation error
sigg=-120;
sigma_i=db2pow(sigg); % (watts) noise power
sigma_I=db2pow(sigg); %(watts) noise power
sigma_E=db2pow(sigg); %(watts) noise power
noise =1;
alpha= 2.1; %large scale fading
d0=1; % Reference distance in meter
PL_0=db2pow(-30); % pathloss
b_u=rand(K, Mr)+1j*rand(K, Mr); % Beamforming vector
%% Channels
f_Ku = rand(K, U) + 1j * rand(K, U); % uplink user to RIS1
disp(f_Ku) % display channel matrix
G_AK = rand(Mr, K) + 1j * rand(Mr, K); % RIS1 to BS
disp(G_AK) % display channel matrix
f_eK = rand(1, K) + 1j * rand(1, K); % RIS1 to Eve
disp(f_eK) % display channel matrix
%% Rician Factor
K_factor = 5; % no. of rician factor
%% Channel Coordinates
x_bs = 0;
y_bs = 0;
x_Ru = -70;
y_Ru = 0;
x_RIS1 = -50;
y_RIS1 = 50;
x_Lu = 0;
y_Lu = 100;
%% Channel Matrix and vectors
si = zeros(1, iterations);
for sim=1:10
fKr = sqrt((x_RIS1-x_Ru)^2 + (y_RIS1-y_Ru)^2); % distance form uplink user to RIS1
pathloss_fKr = sqrt(PL_0*(fKr)^(2.1)); % Large-scale pass loss from the user to RIS1
GAK = sqrt((x_bs-x_RIS1)^2 + (y_bs-y_RIS1)^2); % distance from RIS1 to BS
pathloss_GAK = sqrt(PL_0*(GAK)^(2.1)); % Large-scale pass loss from the RIS1 to BS
feK = sqrt((x_Lu-x_RIS1)^2 + (y_Lu-y_RIS1)^2); % distance from RIS1 to Eve
pathloss_feK = sqrt(PL_0*(feK)^(2.1)); % Large-scale pass loss from the RIS1 to Eve
%% Rician Channel Fading
%%ricianCh(chSize, ricianFactor, dist, pathLossRef, distRef, pathLossExp, freq, phi)
for u = 1:U
f_Kr(:,:,u) = sqrt(1/2).*ricianCh([K 1], 5, fKr, PL_0, d0, alpha, 2.4e9, pi/10, noise); % Rician Channel for f_Kr
G_AK = sqrt(1/2).*ricianCh([Mr K], 5, GAK, PL_0, d0, alpha, 2.4e9, pi/10, noise); % Rician Channel for G_AK
f_eK = sqrt(1/2).*ricianCh([1 K], 5, feK, PL_0, d0, alpha, 2.4e9, pi/10, noise); % Rician Channel for f_eK
end
%% Self Interferece(SI) at BS
rho = 10^-9;
J_s = 0.2;
P_SI = rho*P_u*norm(J_s).^2;
%% Now generating the phase shift atrix in RIS
% Create phase shift matrix
theta = rand(K, K) + 1j * rand(K, K);
S_j = ones(K, U);
t_Eu = 0.5;
gamma_u = (exp(t_Eu)-1);
for ite=1:iterations
%% Optimization
cvx_begin sdp
variable S_u(Mr, Mr) hermitian
variable mu_u nonnegative
for u = 1:U
S_j(u,:,:) = ones(size(b_u(u,:),1), size(b_u(u,:),1), U);
A1(:,:,u) = gamma_u*theta*theta'*sigma_I;
A2_sum(:,:,u) = ones(size(theta(:,u),1), size(theta(:,u), 1));
A3(:,:,u) = theta*f_Ku*S_u*f_Ku'*theta';
for j = 1:U
if j ~= u
if size(f_Ku, 2) == size(theta, 1) && size(S_j, 1) == size(theta, 1) && size(S_j, 2) == size(theta, 1)
A2_sum(:,:,u) = (gamma_u * (theta * f_Ku)' * S_j(:,j))' * f_Ku' * theta';
end
end
A_u(:,:,K) = A1(:,:,u) + A2_sum(:,:,u) - A3(:,:,u);
cu1 = gamma_u*sigma_E;
cu2(:,:,K) = gamma_u*f_eK*theta*theta'*f_eK'*sigma_I;
cu_sum(:,:,L) = ones(size(theta(u,:),1), size(theta(u,:),1));
if j ~= u
cu_sum = f_eK*theta*f_Ku*(S_j(:,j)*(f_Ku'*theta'*f_eK')')';
end
cu_3 = f_eK*theta*f_Ku*S_u*f_Ku'*theta'*f_eK';
c_u = cu1+cu2(:,:,L)+cu_sum(:,:,L)-cu_3;
I_u = sigma_i+norm(G_AK(u,:)*theta).^2 * sigma_I;
G_u = theta*f_Ku;
G_Au(:,:,u) = G_AK(u,:)*theta*f_Ku;
numerator = trace(G_Au(:,:,u)*S_u*G_Au(:,:,u)');
sum_term = ones(size(G_Au(:,:,u), 1));
if j~=u
sum_term = sum_term+G_Au(:,:,u)*(S_j(:,j)*G_Au(:,:,u))';
end
denominator = I_u + sum_term;
end
end
obj1 = numerator / denominator;
maximize obj1
% end
%end
subject to
trace(S_u)<=P_T;
trace(G_u*S_u*G_u')+norm(theta)^2*sigma_I<=P_u;
Q = [A_u(:,:,K) +mu_u*eye(K), A_u(:,:,K)*f_eK';
f_eK* A_u(:,:,K) , c_u-mu_u*epsilon^2];
S_u>=0;
Q>=0;
mu_u>=0;
cvx_end
%% Define the Data Rate
R_AK = log(1 + abs( G_AK * theta * f_Ku).^2 * P_u ./ sigma_i*norm(G_AK*theta).^2 + P_SI + sigma_I);
R_eK = log(1 + abs(f_eK*theta*f_Ku).^2 * P_u ./ sigma_i*norm(f_eK*theta).^2 + sigma_I);
%% Calculate the secrecy rates
R_k_sec = max(R_AK - R_eK);
%% Calculate the sum of secrecy rates
C = sum(R_k_sec);
disp(size(C))
disp(['The secrecy sum rate (SSR) is: ', num2str(C)]);
si(sim, ite)= C;
end
end
Rician channel code is
function [ch,Ld] = ricianCh(chSize, ricianFactor, dist, pathLossRef, distRef, pathLossExp, freq, phi,noise)
if ~isequal(size(chSize), [1 2]) || ~isreal(chSize)
disp(‘ERROR! input chSize must be in size of 2x1 and real’);
disp('your chSize : ‘); disp(chSize);
error(’ ‘);
end
if ~isreal(ricianFactor) || ~isscalar(ricianFactor) || ricianFactor<0
disp(‘ERROR! input kappa must be a real scalar positive’);
disp(‘your kappa :’); disp(ricianFactor);
error(’ ‘);
end
if ~isreal(dist) || ~isscalar(dist) || dist<0
disp(‘ERROR! input dist must be a real scalar positive’);
disp(‘your dist :’); disp(dist);
error(’ ‘);
end
if ~isreal(pathLossRef) || ~isscalar(pathLossRef) || pathLossRef<0
disp(‘ERROR! input pathLossRef must be a real scalar positive’);
disp(‘your pathLossRef :’); disp(pathLossRef);
error(’ ‘);
end
if ~isreal(distRef) || ~isscalar(distRef) || distRef<0
disp(‘ERROR! input distRef must be a real scalar positive’);
disp(‘your distRef :’); disp(distRef);
error(’ ‘);
end
if ~isreal(pathLossExp) || ~isscalar(pathLossExp) || pathLossExp<0
disp(‘ERROR! input pathLossExp must be a real scalar positive’);
disp(‘your pathLossExp :’); disp(pathLossExp);
error(’ ‘);
end
if ~isreal(freq) || ~isscalar(freq) || freq<0
disp(‘ERROR! input freq must be a real scalar positive’);
disp(‘your freq :’); disp(freq);
error(’ ');
end
Ld = (pathLossRef*((dist/distRef)^(-pathLossExp)))/noise; %disp(['Ld : ', num2str(Ld)]);
waveLen = 3e8/freq; %disp(['waveLen : ‘, num2str(waveLen)]);
k = 2pi/waveLen; %disp([‘k : ‘, num2str(k)]);
N_ar = chSize(1); rowCnt = (0:(N_ar-1)).’; %disp(‘N_ar :’); disp(N_ar);
N_at = chSize(2); colCnt = (0:(N_at-1)).’; %disp(‘N_at :’); disp(N_at);
d_a = waveLen/2; %disp(‘d_a :’); disp(d_a);
ar = (1/sqrt(N_ar))exp(1jkd_arowCntsin(phi)); %disp(‘ar :’); disp(ar);
at = (1/sqrt(N_at))exp(1jkd_acolCntsin(phi)); %disp(‘at :’); disp(at);
H_NLOS = randn(chSize) + 1jrandn(chSize); %disp(‘H_NLOS :’); disp(H_NLOS);
ch = sqrt(Ld/2)*(sqrt(ricianFactor/(1+ricianFactor))arat’ + sqrt(1/(1+ricianFactor))*H_NLOS);
end
error is
Error using cvx/cat
All dimensions but the one being concatenated (1) must be equal.
Error in cvx/vertcat (line 6)
y = cat( 1, varargin{:} );
Error in Uplink_opt_try1 (line 116)
Q = [A_u(:,:,K) +mu_u*eye(K), A_u(:,:,K)*f_eK’;