Complete code:

for iter = 1 : N_iter

p = iter;

```
% Update y (eq.2) and z_k (eq.4)
[SINR,SINR_sqr,~,sum_rate] = Data_rate(K,w_k,h_theta,P_rand,g_theta,h_s_i,v,sigma_sqr_t,sigma_sqr);
y=sqrt(sum_rate)/(sum(P_rand)+( (1/rho)*norm(v)^2)+P_BS + P_UE + P_RIS);
z=SINR_sqr;
cvx_begin %quiet
variable P_opt(K)
% Compute objective function (eq.5)
[obj_power] = objective_power(K,w_k,h_theta,P_opt,g_theta,h_s_i,v,sigma_sqr_t,sigma_sqr,rho,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(1));
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
a = (norm(w_k(:,k)'*h_theta(:,k))^2)*P_opt(k);
b=zeros(1,1);
for mm=1:K
if mm~=k
b = b + (norm(w_k(:,k)'*h_theta(:,mm))^2)*P_opt(mm);
%b1(:,k) = b;
end
end
c(k) = norm(w_k(:,k)'*g_theta*v)^2;
d(k) = norm(w_k(:,k)'*h_s_i*v)^2;
e(k) = norm(w_k(:,k)')^2 ;
(2^(R_min)-1)*( b + c(k)*sigma_sqr_t + d(k) + e(k)*sigma_sqr)...
<= a;
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,'Failed') || strcmp(cvx_status, 'Inaccurate/Solved')
P_rand = P_opt;
else
P_opt = P_rand; % default value
end
% compute objective function
%[main_obj] = objective_main(K,w_k,h_theta,P_opt,g_theta,h_s_i,v,sigma_sqr_t,sigma_sqr,rho,P_BS,P_UE,P_RIS);
% obj = obj_power;
[obj_power] = objective_power(K,w_k,h_theta,P_opt,g_theta,h_s_i,v,sigma_sqr_t,sigma_sqr,rho,P_BS,P_UE,P_RIS,y,z);
J(p) = real(obj_power);
% test stopping criterion
if N_iter > 1
abs(J(N_iter) - J(N_iter-1)) <= tol;
break;
end
end
```

And the function related to objective function is shown below:

function [obj_power] = objective_power(K,w_k,h_theta,P_rand,g_theta,h_s_i,v,sigma_sqr_t,sigma_sqr,rho,P_BS,P_UE,P_RIS,y,z)

% Calculating the terms related to an objective function of Uplink transmit power optimization

for k=1:K

a(k) = 2*sqrt(P_rand(k)).*z(k).*(real( (w_k(:,k)'*h_theta(:,k))));

```
b=zeros(1,1);
for mm=1:K
if mm~=k
b = b + (norm(w_k(:,k)'*h_theta(:,mm))^2)*P_rand(mm);
%b1(:,k) = b;
end
end
%b1(:,k) = b;
c(k) = norm(w_k(:,k)'*g_theta*v)^2;
d(k) = (norm(w_k(:,k)'*h_s_i*v)^2);
e(k)= norm(w_k(:,k)')^2 ;
f(k)= (z(k)^2).*(b + c(k)*sigma_sqr_t+d(k)+e(k)*sigma_sqr); % SINR
r(k)= a(k)- f(k); %
j=log(2);
transmit_rate(k)=j*log(1+2*sqrt(P_rand(k)).*z(k).*(real( (w_k(:,k)'*h_theta(:,k)))) - (z(k)^2).*(b + c(k)*sigma_sqr_t+d(k)+e(k)*sigma_sqr));
end
sum_rate=sum(transmit_rate);
obj_power=2*y*sqrt(sum_rate)-((y^2)*( sum(P_rand)+( (1/rho)*norm(v)^2 ) + P_BS + P_UE + P_RIS));
```

end

i am getting error at the line:

transmit_rate(k)=j*log(1+2*sqrt(P_rand(k)).*z(k).*(real( (w_k(:,k)'*h_theta(:,k)))) - (z(k)^2).*(b + c(k)*sigma_sqr_t+d(k)+e(k)*sigma_sqr));