How to fix "Cannot perform the operation: {real affine} .* {convex}"?

%% 参数初始化
clear; clc;

I = 2;      % 无人机数量
T = 5;      % 时间步长
J = 3;      % RSU数量
M = 2;      % 车辆数量
eta_max = 10;   % 最大资源分配
p_max = 100;    % 最大功率
Gamma_L = 1;    % 低优先级效用系数
Gamma_H = 5;    % 高优先级惩罚系数
c = 0.1;        % 衰减系数
S = ones(I, T); % 任务大小
D = ones(I, T); % 数据量
tau = ones(I, T); % 阈值
x = rand(I, T);  % 无人机固定坐标
y = rand(I, T);
max_iter = 10;   % Dinkelbach最大迭代
lambda = 0;      % 初始lambda
M_big = 1e3;     % 大M法常数

B_mj = 10e6;       % 带宽 (Hz)
v = randi([0,1], I, T);      % 任务优先级 (0:低, 1:高)
delta = randi([1,3], I, 1);  % 无人机本地计算资源 (CPU GHz)
sigma = 0.0009999999999999985; % 噪声功率 (W)

H = 50;         % 无人机悬停高度 (m)
% 通信参数(随机生成位置和信道增益)
% 无人机位置 (固定)
x_uav = randi([0,100], I, 1);
y_uav = randi([0,100], I, 1);
% RSU位置 (固定)
x_rsu = randi([0,100], J, 1);
y_rsu = randi([0,100], J, 1);
% 车辆位置 (随机移动)
x_veh = randi([0,100], M, T);
y_veh = randi([0,100], M, T);

P = 0.5;% 视距概率
r_ij = 1;%无人机i和RSUj之间的传输功率
r_im = 1;%无人机i和车辆m之间的传输功率
g_mj = 1;%车辆m和RSU j之间的信道增益

% 初始化变量存储
theta_prev = rand(I, T);
eta_prev = rand(I, J, T);
p_prev = rand(M, I, T);

% Dinkelbach
for iter = 1:max_iter
    cvx_begin
        variables theta(I, T)  eta(I, J, T) p_mi(M, I, T)
        variables T_i(I, T) z(I, T)
        
        % 目标函数分段处理
        u = 0;
        for i = 1:I
            for t = 1:T
                % 定义T_i(t)的约束
                T_l_i(i,t) = (1 - theta(i,t)) * S(i,t) * D(i,t) / delta(i);
                % C = theta(i,t) * D(i,t) / (B_mj * rel_entr(1,sigma/(sigma+p_mi(i,t) * g_mj)));
                C = theta(i,t) * D(i,t) *inv_pos(B_mj * z(i,t));
                T_RSU_i(i,t) = P*theta(i,t)*D(i,t)/r_ij + (1-P) * (theta(i,t)*D(i,t)/r_im + C) + theta(i,t)*S(i,t)*D(i,t) - lambda*eta(i,j,t);
                
                T_i(i,t) = max(T_l_i(i,t),T_RSU_i(i,t));
                
                % Big-M
                T_i(i,t) <= tau(i,t) + M_big*(1 - z(i,t));
                T_i(i,t) >= tau(i,t) - M_big*z(i,t);
                
                % u_i(t)
                u1 = v(i,t)*log(1 + tau(i,t) - T_i(i,t))/log(2) + Gamma_L;
                u2 = -v(i,t)*Gamma_H + (1 - v(i,t))*Gamma_L*exp(-c*(T_i(i,t)-tau(i,t)));
                u = u + z(i,t)*u1 + (1 - z(i,t))*u2;
            end
        end
        maximize(u)
        subject to
        % C1-C11
        v >= 0; v <= 1;
        theta >= 0; theta <= 1;
        eta >= 0; eta <= eta_max;
        for j = 1:J
            for t = 1:T
                sum(eta(:, j, t)) <= eta_max;
            end
        end
        p >= 0; p <= p_max;
        for m = 1:M
            for t = 1:T
                sum(p(m, :, t)) <= p_max;
            end
        end
    cvx_end
    
    % lambda
    if strcmp(cvx_status, 'Solved')
        lambda_new = sum(theta(:) .* S(:) .* D(:)) / sum(eta(:));
        if abs(lambda_new - lambda) < 1e-3
            break;
        end
        lambda = lambda_new;
        theta_prev = theta;
        eta_prev = eta;
        p_prev = p;
    else
        error('CVX求解失败');
    end
end

This is the code generated by GPT based on my model, and I am trying to improve it. However,
I cannot run the code and the window said that :
"Error using .*
Disciplined convex programming error:
Cannot perform the operation: {real affine} .* {convex}

Error in * (line 36)
z = feval( oper, x, y );

Error in test_taskoffloading0421 (line 62)
C = theta(i,t) * D(i,t) *inv_pos(B_mj * z(i,t));"
I am sure my model is convex. How could I fix it?(Please ignore other confusing parts in the code.)

Please show us your convexity proof. When doing so, please follow the guidelines at Why isn't CVX accepting my model? READ THIS FIRST!

C is used in T_RSU, which is used in T_i, which s a non-affine expression which is used in two inequalities going in opposite directions. So until shown otherwise, I will assume the problem is non-convex.

Also, GPT does not seem to have digested expression holders (declaration) Assignment and expression holders in CVX Users’ Guide


I am sorry for the delay, here is my proof.

You have not implemented the approximations and relaxations which were need to make the model convex.

You need to understand exactly what the convex optimization problem to be implemented is, and then (try to) produce the corresponding CVX code.

I don’t believe any current AI tool is going to be able to do that for you.

Thank you.
Because this is the first time I code with CVX, so I try to produce approximate code with AI and improve it.
Perhaps it’s an extravagant request, but could you suggest some ideas to make my model convex?

This is not a tutorial site. People who post on this forum are expected to already know a lot about convex optimization, and to have thoroughly read the CVX Users’ Guide.

You need to carefully read and understand the paper you got this from. Maybe you need to study https://web.stanford.edu/~boyd/cvxbook/bv_cvxbook.pdf in order to learn more about convex optimization.

It might be a good idea to start with simpler models to implement in CVX before you try more complicated models.

OK, thank you again for your guidance.