Error: Cannot perform the operation: {real affine} ./ {real affine}

Hello everyone,

I am using CVX for solving a convex optimization problem. The convexity of the problem mentioned in the paper. The objective function is concave and the nonlinear constraint is convex. The code I have written is as follows:
function [Rate,p,B,p_F,B_F] = CVXforRate(F,H,G,q,w,Phi,d,Beta,c,a)

global N_R N_T K N B B_F Delta N0 B_max P_max P_c0 P_cn T0 b_F mu_F mu P_F T T_E P_c

P0=0.5e-3; %mW, can change
P_c = N*P_cn+P_c0+P_E;

%Optimization problem to solve is as follows:
% max  log(Beta-d*g2(P_max-p,B_max-B))+log(g1(p,B)) 
%  p,B

% s.t. 0 <= p <= P_max,  0 <= B <= B_max
% d/(B_max-B)*log(1+(P_max-p)*|h_F|^2/(B_max-B)*N_0)<= Beta

variables x(n)    %x=[x1, x2]=[p,B]
maximize ((log(Beta-d*g2))+log(g1));   %Objective function is a concave function
subject to 
0 <= x(1) && x(1) <= P_max;
0 <= x(2) && x(2) <= B_max;
rdivide(d,(B_max-x(2))*log(1+rdivide((P_max-x(1))*abs(F)^2,((B_max-x(2))/N0)))) <= Beta;



The errors are as follows:
Error using .* (line 173)
Disciplined convex programming error:
Cannot perform the operation: {real affine} ./ {real affine}

Error in ./ (line 19)
z = times( x, y, ‘./’ );

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

Error in / (line 15)
z = mtimes( x, y, ‘rdivide’ );

Error in CVXforRate (line 22)

Error in main (line 109)
[Rate,p,B,p_F,B_F] = CVXforRate(F,H,G,q,w,Phi,d,Beta,c,a)

I read the DCP ruleset and also common questions. My question is also among common question. However the paper I am simulating proved that objective function is concave.

Could you please help me with this error?


1 Like

can be rewrrtten as

CVX will let you enter the log of that, because the argument is concave.

More generally, x*log(1+y/x) = -rel_entr(x,x+y), the RHS of which CVX will accept if x and y are any combination of affine or constant.

The g2 term looks rather a mess. If there really is 1/ ... at the beginning, I think you’re in trouble, because I think g2 is neither convex nor concave, nor islog(Beta-d*g2). If there isn’t the 1/..., I think x*log(1+x/y) = -rel_entr(x,x+y) could be used with the correct choice of x and y. And then the log of that, all presuming the signs of the terms are what they need to be. So the g2 thing is rather a mess now, and i don’t even really know what is is supposed to be.

Anyhow, I suggest you carefully read the CVX Users’ Guide. Among other things, you will not see provision for such things as
0 <= x(1) && x(1) <= P_max;
Instead, that could be entered as
0 <= x(1) <= P_max;

I haven’t look at the rdivide line, but I presume that needs to be fixed.

I don’t know how much other stuff is wrong with your program, but this will hopefully be enough to get you started toward fixing it.

Edit: Corrected typo to More generally, x*log(1+y/x) = -rel_entr(x,x+y)

Edit: See below. I misread the scope of the parentheses for g2

1 Like

Dear Mark,

Many thanks for your help.
Yes, g2 is in the form of 1/ … .
Instead of the objective function that I explained in the previous message and is over two variables ((x(1)),(x(2))), I want to use the new one over four variables (x(1), x(2),x(3),x(4)) as follows:

log(β − d*g2(x(3) , x(4) )) + log(g1(x(1), x(2))).

Then, I have written the g1 and g2 function using x*log(1+x/y) = -rel_entr(x,x+y) rule as follows:
variables x(n) %x=[x1, x2 x3 x4]=[p,B,p_F,B_F]



I have written g2 using -inv_pos. However, it does not work (Error using cvx/inv_pos
Too many input arguments.).
The paper proved that the function g2(x(3), x(4)) =1/ x(4)* log(1+a x(3)/x(4)) is jointly convex in (x(3) , x(4)).
Also, it mentioned that (Beta-d*g2) used in the first sentence of objective function is jointly concave with respect to x(3) and x(4). Do you have any suggestions for writing g2 in an appropriate way?

I really appreciate your time.
All the best,
Samane Bidabadi

1 Like

I just corrected a typo in my previous post. The corrected version is
More generally, x*log(1+y/x) = -rel_entr(x,x+y)
However the refomulation I showed for g1 was correct.

According to my calculations, 1/x(4)*log(1+a*x(3)/x(4)) is neither convex nor concave in (x(3) , x(4)). For instance, letting a=x(3)=x(4)=1, the Hessian has one positive and one negative eigenvalue. So this looks like a showstopper. There is no way to formulate g2 for CVX, because it is neither convex nor concave.

i don’t know what the paper did or showed, but that expression as is is not convex.

Your attempts to use CVX functions in disallowed ways will not succeed. You can’t sneak in disallowed argument types and convert a non-convex expression into a convex expression.

1 Like

My previous post applies to the g2 as defined in your most recent post. But re-reading the original g2=(1/(B_max-x(2))/log(1+a*(P_max-x(1))/(B_max-x(2))));, I now see that I misread the scope of the parentheses, and it is not where I thought, or where it is in your most recent post.

Therefore, the solution I provided at applies to g2. I.e.,
g2 = inv_pos(-rel_entr(B_max-x(2),B_max-x(2)+a*(P_max-x(1))));

Then you should be able to use log(Beta-d*g2) because the argument of log is concave.

I siggest you check all my work to make sure there are no typos.

That should be enough for enter the whole problem in CVX.


Dear Mark,

Thanks for your response. I calculated the Hessian matrix of g2=1/(x(4)log(1+ax(3)/x(4)) and it’s determinant in (x(3),x(4))=(1,1) as well. The determinant is positive (1.9e12). The paper also calculated the hessian matrix and explained all the steps(

Matlab commands:
%g2=1/(x(4)log(1+ax(3)/x(4))=1/z(x(3),x(4)); (x(3),x(4))=(p_F, B_F);

Hess=(1/(B_F+ap_F)^2z(p_F,B_F)^3)[a^2B_Fz(p_F,B_F)+2a^2B_F^2 -a^2p_Fz(p_F,B_F)+2aB_F(B_F+ap_F)z_der_BF(p_F,B_F);…
z(p_F,B_F)+2a(B_F)(B_F+ap_F)z_der_BF(p_F,B_F) a^2p_F^2/B_Fz(p_F,B_F)+2(B_F+ap_F)^2(z_der_BF(p_F,B_F))^2];

function z=z(p_F,B_F)
function z_der_BF=z_der_BF(p_F,B_F)


Any assistance you can provide would be greatly appreciated.
All the best,

Thank you Mark. I have just read your new post. Yes, you are right. I will check your work and also my explanation to make sure there are no typos.

Thank you so much