This is my optimization code:

```
%************************************************************
% Alternating Optimization Algorithm
%************************************************************
% Random power (Is feasible ?)
P_rand = P1; % Maximum power allocated to each UE
% Feasible receive beamforming for users
w_k_initial=w_k;
% Objective function related to Non-optimized
[~,~,~,main_obj1] = objective_main(K,w_k_initial,h_theta,P_rand,g_theta,h_s_i,v,sigma_sqr_t,sigma_sqr,rho_UE,rho_BS,P_BS,P_UE,P_RIS);
EE_small_unopt(:,sim)=main_obj1; % Store unoptimized EE for each small scale setup
% Objective function
J = zeros(1,N_iter);
for iter = 1 : N_iter
% Update y (eq.2) and z_k (eq.4)
[~,SINR_sqr,update_y,~] = objective_main(K,w_k_initial,h_theta,P_rand,g_theta,h_s_i,v,sigma_sqr_t,sigma_sqr,rho_UE,rho_BS,P_BS,P_UE,P_RIS);
y=update_y; % Update y
z=SINR_sqr; % Update z
% Modern optimization solver
cvx_solver mosek
cvx_begin quiet
variable P_opt(K) nonnegative
% Compute objective function (eq.5)
[obj_power] = objective_power(K,w_k_initial,h_theta,P_opt,g_theta,h_s_i,v,sigma_sqr_t,sigma_sqr,rho_UE,rho_BS,P_BS,P_UE,P_RIS,y,z);
obj = obj_power;
maximize real(obj)
% constraints
% Target sensing constraint (eq.6)
temp = 0;
for k = 1:K
temp = temp + ( norm( w_0'*h_theta(:,k) )^2 )*(P_opt(k));
end
Gamma_t*( temp + norm(w_0'*h_s_i*v)^2 + sigma_sqr*( norm(w_0')^2 ) )...
<= sigma_sqr_t*(norm(w_0'*g_theta*v)^2);
% Individual Rate constraint (QoS) (eq.7)
for k=1:K
a2 = (norm(w_k_initial(:,k)'*h_theta(:,k))^2)*P_opt(k);
b2=zeros(1,1);
for mm=1:K
if mm~=k
b2 = b2 + (norm(w_k_initial(:,k)'*h_theta(:,mm))^2)*P_opt(mm);
%b1(:,k) = b;
end
end
c2(k) = norm(w_k_initial(:,k)'*g_theta*v)^2;
d2(k) = norm(w_k_initial(:,k)'*h_s_i*v)^2;
e2(k) = norm(w_k_initial(:,k)')^2 ;
(2^(R_min)-1)*( b2 + c2(k)*sigma_sqr_t + d2(k) + e2(k)*sigma_sqr)<= a2 ;
end
% Individual Power Constraint (eq.8c)
for k = 1:K
P_opt(k) <= P;
P_opt(k) >=0;
end
cvx_end
% cvx_status
if strcmp(cvx_status,'Solved') %|| strcmp(cvx_status, 'Inaccurate/Solved') %|| strcmp(cvx_status,'Failed')
P_rand = P_opt;
else
% P_opt = P_rand; % default value
end
P_rand;
% First optimize uplink transmit power allocation
% [P_rand]=power_optimize(K,P,R_min,P_BS,P_UE,P_RIS,Gamma_t,sigma_sqr_t,sigma_sqr,rho_UE,rho_BS,P_rand,h_theta,g_theta,h_s_i,v,w_k_initial,w_0);
% Optimize Receiving beamforming for users
[SINR,SINR_sqr,~,~] = objective_main(K,w_k_initial,h_theta,P_rand,g_theta,h_s_i,v,sigma_sqr_t,sigma_sqr,rho_UE,rho_BS,P_BS,P_UE,P_RIS);
a_k=SINR; % SINR corresponding to optimized w_k
b_k=sqrt(1+a_k).*SINR_sqr;
% Modern optimization solver
cvx_solver mosek
% Solving problem (P4)
cvx_begin %quiet
variable w_k_opt(Mr_rec,K) complex
% Compute objective function (eq.11)
[obj_Rxbeam] = objective_Rxbeam(K,w_k_opt,h_theta,P_rand,g_theta,h_s_i,v,sigma_sqr_t,sigma_sqr,a_k,b_k);
obj = obj_Rxbeam;
maximize real(obj)
% constraints
% Individual Rate constraint (QoS) (eq.15)
for k=1:K
%n = pow_pos((norm(w_k_opt(:,k)'*h_theta(:,k))),2)*P1(k);
f=zeros(1,1);
for mm=1:K
if mm~=k
f= f + pow_pos(norm(w_k_opt(:,k)'*h_theta(:,mm)),2)*P_rand(mm);
%b1(:,k) = b;
end
end
c1 = pow_pos(norm(w_k_opt(:,k)'*g_theta*v),2);
d1 = pow_pos(norm(w_k_opt(:,k)'*h_s_i*v),2);
e1 = pow_pos(norm(w_k_opt(:,k)'),2);
(2^(R_min)-1)*( f + c1*sigma_sqr_t + d1 + e1*sigma_sqr)...
<= (2*real ( w_k_initial(:,k)'*h_theta(:,k)*h_theta(:,k)'*w_k_opt(:,k))...
- norm ( w_k_initial(:,k)'*h_theta(:,k) )^2 ) *P_rand(k);
end
cvx_end
% cvx_status
if strcmp(cvx_status,'Solved') || strcmp(cvx_status, 'Inaccurate/Solved') %|| strcmp(cvx_status,'Failed')
w_k_initial = w_k_opt;
else
% w_k_opt = w_k_initial; % default value
end
```

Error occurs at:

Error using +

Disciplined convex programming

error:

Illegal operation: {concave} +

{convex}

Error in objective_Rxbeam (line 14)

i2 = i2 + pow_pos(norm(w_k(:,k)'*h_theta(:,mm)),2)*P(mm);

Error in Opt_power_beamforming (line 199)

[obj_Rxbeam] = objective_Rxbeam(K,w_k_opt,h_theta,P_rand,g_theta,h_s_i,v,sigma_sqr_t,sigma_sqr,a_k,b_k);

Even I specify the first sub probelm as nonnegative as given as:

variable P_opt(K) nonnegative

But optimization powers at this junction is obtained as:

P_opt =

-0.0000

-0.0000

0.0000

0.0000

0.0016

Because of the negative values, second problem becomes concave + convex.

And second problem is that Some powers are not satisfying the constraint as:

% Individual Power Constraint (eq.8c)

```
for k = 1:K
P_opt(k) <= P;
P_opt(k) >=0;
end
But solution status is solved.
```

But if I run subproblem 1 individually, no negative powers obtained. When combined, negative powers obtained.

Please help in this regard!