Function SNR=compute_SNR includes parameters ‘a’ and ‘P’ ,which are defined as ‘variables’ within cvx,however the function itself isn’t included in cvx.The problem is function ‘compute_SNR’ can’t recognize ‘a’ and ‘P’ ,so how should solve it?

pictures of my code and problem

In the future, please copy and past code using Preformatted text icon, rather than posting an image.

I have no idea what you’re trying to do. `So you will need to explain more thoroughly.

`compute_SNR`

has `a`

and `P`

as inputs; but those are overwritten by declaring them as CVX variables; i.e., whatever values they had when the function was called, are ignored.

You are not allowed to do `a == round(a)`

; if you need `a`

to be integer, you should declare it as being a binary variable (because you separately imposed `0 <= a <= 1`

). Or you can apply `round`

after cvx_end if that is what you want.

If you want to perform some operations on CVX variables after they are declared, you can call another function between `cvx_begin`

and `cvx_end`

to perform those operations, and provide the CVX variables (`a`

and `P`

) as inputs to that function (but do not have a cvx_begin or cvx_end inside that other function).

If you want to use the optimal values of `a`

and `P`

outside `compute_SNR`

, you can list them as outputs of `compute_SNR`

.

```
clc;clear;
% 定义系统参数和随机种子
f_c = 2e9; % 中心频率 2 GHz
B = 10e6; % 带宽 10 MHz
N = 10; % 信道数量
p_max = 23; %用户最大发射功率为 23 dBm
SN0 = -174; % 热噪声谱密度（dBm/Hz）
P_gs = SN0 + 10*log10(B/N); %接收端热噪声功率
SNR_min = 7; % SINR 需求（dB）
% Path loss parameters
plec= 37.6; % Path loss exponent for cellular links
pled= 40; % Path loss exponent for D2D links
plcc= 128.1; % Path loss constant for cellular links
plcd= 148; % Path loss constant for D2D links
ssc = 10; % Shadowing standard deviation for cellular links in dB
ssd = 12; % Shadowing standard deviation for D2D links in dB
radius_bs = 1000; % 基站到边缘的半径 1 km
radius_d2d = 100; % D2D接收用户到上行D2D用户的半径 100 m
nc = 8; % 蜂窝网用户数量
nd = 7; % D2D用户数量
K = nc + nd; %上行用户数
rng(42); % 设置随机种子，这里使用42
% 基站位置
bs_x = 0;
bs_y = 0;
% 蜂窝网用户随机分布在一个圆形区域内
theta_c = 2*pi*rand(1, nc);
r_c = radius_bs * sqrt(rand(1, nc));
x_c = r_c .* cos(theta_c);
y_c = r_c .* sin(theta_c);
% 上行D2D用户随机分布在一个圆形区域内
theta_d = 2*pi*rand(1, nd);
r_d = radius_bs * sqrt(rand(1, nd));
x_d = r_d .* cos(theta_d);
y_d = r_d .* sin(theta_d);
% D2D接收用户随机分布在各自上行D2D用户周围
x_d2d = zeros(1, nd);
y_d2d = zeros(1, nd);
for i = 1:nd
theta = 2*pi*rand;
r = radius_d2d * sqrt(rand);
x_d2d(i) = x_d(i) + r * cos(theta);
y_d2d(i) = y_d(i) + r * sin(theta);
end
% 合并蜂窝用户和D2D上行用户的x和y坐标向量
x_up = [x_c, x_d];
y_up = [y_c, y_d];
% 接收端位置（将基站坐标作为蜂窝用户的接收端坐标）
x_r = [bs_x * ones(1, nc), x_d2d];
y_r = [bs_y * ones(1, nc), y_d2d];
SINR = compute_SNR(N, K, nc, a, P, P_gs, x_up, y_up, x_r, y_r, plec, pled, plcc, plcd, ssc, ssd)
%使用cvx解决凸优化问题
cvx_begin
variables a(N,K) P(K)
minimize(-sum(log2(1 + SINR)))
subject to
%约束1：a中元素全为0到1的整数
0 <= a <= 1
a == round(a) %所有元素为整数
%约束2：a中元素列向量和为1
sum(a) == ones(1, K)
%约束3：a的每行元素和小于2，且每行元素前nc个和小于1
for i = 1:N
sum(a(i,:)) <= 2
sum(a(i,1:nc)) <= 1
end
%约束4：:上行用户发射功率小于最大发射功率
0 <= P <= p_max
%约束5：各链路信噪比大于最低要求
for j = 1:K
sum(SINR(:,j) >= SNR_min) == 1
end
cvx_end
%显示结果
disp('最优解');
disp(a);
disp(p);
disp('目标函数值');
disp(cvx_optval);
% 绘制基站和用户位置平面图
figure;
hold on;
plot(bs_x, bs_y, 'ro', 'MarkerSize', 10, 'MarkerFaceColor', 'r'); % 基站位置
plot(x_c, y_c, 'bo', 'MarkerSize', 8, 'MarkerFaceColor', 'b'); % 蜂窝网用户位置
plot(x_d, y_d, 'go', 'MarkerSize', 8, 'MarkerFaceColor', 'g'); % 上行D2D用户位置
plot(x_d2d, y_d2d, 'mo', 'MarkerSize', 6, 'MarkerFaceColor', 'm'); % D2D接收用户位置
% 添加标签和图例
title('基站与用户位置平面图');
xlabel('距离 (m)');
ylabel('距离 (m)');
legend('基站', '上行蜂窝用户', '上行D2D用户', 'D2D接收用户', 'Location', 'best');
grid on;
axis equal;
hold off;
%定义计算信噪比函数
function SNR = compute_SNR(N, K, nc, a, P, P_gs, x_up, y_up, x_r, y_r, plec, pled, plcc, plcd, ssc, ssd)
SNR(N,K) = zeros(N,K);
g(N,K) = zeros(K,K);
for j = 1:nc
for p = 1:K
g(j,p) = ssc + plcc + plec*log10(0.001*sqrt((x_up(j) - x_r(p))^2+(y_up(j)-y_r(p))^2));
end
end
for j = nc + 1:K
for p = 1:K
g(j,p) = ssd + plcd +pled*log10(0.001*sqrt((x_up(j) - x_r(p))^2+(y_up(j)-y_r(p))^2));
end
end
for i = 1:N
for p = 1:K
SNR(i,p) = a(i,p)*((P(p) + g(p,p)) - (sum(a(i,:).*P.*g(:,p)) - a(i,p)*(P(p) + g(p,p)) + P_gs));
end
end
end
```

Sorry about that.It’s my first time using this web,and I don’t quite understand how to use ‘Preformatted text icon’,and hope that my last reply works.

You still need to explain more clearly what you are trying to do, and what errors you are encountering. But before doing that, please pay attention to the entirety of my previous post.

```
cvx_begin
variables a(N,K) P(K)
minimize(-sum(log2(1 + compute_SNR(N, K, nc, a, P, P_gs, x_up, y_up, x_r, y_r, plec, pled, plcc, plcd, ssc, ssd))))
subject to
%约束1：a中元素全为0到1的整数
0 <= a <= 1
a == round(a) %所有元素为整数
%约束2：a中元素列向量和为1
sum(a) == ones(1, K)
%约束3：a的每行元素和小于2，且每行元素前nc个和小于1
for i = 1:N
sum(a(i,:)) <= 2
sum(a(i,1:nc)) <= 1
end
%约束4：:上行用户发射功率小于最大发射功率
0 <= P <= p_max
%约束5：各链路信噪比大于最低要求
SNR = compute_SNR(N, K, nc, a, P, P_gs, x_up, y_up, x_r, y_r, plec, pled, plcc, plcd, ssc, ssd)
for j = 1:K
sum(SNR(:,j) >= SNR_min) == 1
end
cvx_end
```

In code above，i meant to call the function `compute_SNR`

in `minimize`

,and in `subjetct to`

i called `compute_SNR`

again as `SNR = compute_SNR(N, K, nc, a, P, P_gs, x_up, y_up, x_r, y_r, plec, pled, plcc, plcd, ssc, ssd)`

for another constraint condition:

```
for j = 1:K
sum(SNR(:,j) >= SNR_min) == 1
end
```

I defined the function `compute_SNR`

as below:

```
function SNR = compute_SNR(N, K, nc, a, P, P_gs, x_up, y_up, x_r, y_r, plec, pled, plcc, plcd, ssc, ssd)
SNR = zeros(N, K); % 初始化 SNR 矩阵
g = zeros(K, K); % 初始化 g 矩阵
% 计算 g 矩阵
for j = 1:nc
for p = 1:K
g(j,p) = ssc + plcc + plec*log10(0.001*sqrt((x_up(j) - x_r(p))^2+(y_up(j)-y_r(p))^2));
end
end
for j = nc + 1:K
for p = 1:K
g(j,p) = ssd + plcd + pled*log10(0.001*sqrt((x_up(j) - x_r(p))^2+(y_up(j)-y_r(p))^2));
end
end
% 计算 SNR 矩阵
for i = 1:N
for p = 1:K
SNR(i,p) = a(i,p)*((P(p) + g(p,p)) - (sum(a(i,:).*P.*g(:,p)') - a(i,p)*(P(p) + g(p,p)) + P_gs));
end
end
end
```

I expected this function will return the N × K matrix SNR,but here comes another problem:in code

`SNR(i,p) = a(i,p)*((P(p) + g(p,p)) - (sum(a(i,:).*P.*g(:,p)') - a(i,p)*(P(p) + g(p,p)) + P_gs));`

I got an error alert `Wrongly use .* ,Array dimensions must match for binary array op.`

These should be all my quetions.Since I’m not a native English speaker, i had try my best to express it clearly.And sincerely thank you for your replying.

Perhaps your code has many errors. Here are two of them. At least fix these before running it again.

`a == round(a)`

is not allowed in CVX for a variable `a`

. Using `round(a)`

without the equality constraint is not allowed.

`SNR = zeros(N, K);`

should instead be `expression SNR(N, K)`

, which will automatically be initialized to all zeros. See the discussion of expression holders at The Basics — CVX Users' Guide .

if you insert statement consisting of the object (item) in question, CVX will tell you what kind of object it is and its dimensions. You can also use `whos`

.