cvx_begin
sum=0;sum_pj=0;
for n=1:1:N
variable Pj
A(n)=-anbn/log(2)/(bnPj_k+1)/(bnPj_k+an+1);
R(n)=A(n)Pj(n)+log2(1-cninv_pos(dnPj(n)+1));
sum=sum+R(n);
sum_pj=sum_pj+Pj(n);
end
maximize sum
subject to
0<=sum_pj<=NPj_average;
0<=Pj(n)<=4Pj_average;

Have you proven this is a convex optimization problem? The 2nd term of the objective function summand can be implemented per the last row of the table in section “5.2.10 Other simple sets” of https://docs.mosek.com/modeling-cookbook/expo.html#modeling-with-the-exponential-cone . I haven’t verified the curvature one way or another for the first term of the objective function summand. Have you?

Instead of declaring the same scalar variable Pj N each time through a for loop, you need to declare it once as a vector variable Pj(N)