# Invalid constraint: {complex affine} >= {convex},how do i rewrite the code of formula,which has two variables

(蕭仰恩) #1

I am going to write the code about this formula,but it has some problems,hoping someone can teach me how to modify it.My problem code will begin the %========my problem of code begin here===========

``````%OP4
%declare
K=4;
N=4;
L=5;%distance between RX & TX
nois_var_hk_2pow=0.1*(L^(-2.5))
nois_var_ak_2pow=[10^-7 10^-7 10^-7 10^-7 ];
nois_var_dk_2pow=[10^-5 10^-5 10^-5 10^-5 ];
bar_r=[10 10 10 10];
P_T=10000;
h_1=sqrt(nois_var_hk_2pow/2)*(randn(N,1)+1i*randn(N,1));
h_2=sqrt(nois_var_hk_2pow/2)*(randn(N,1)+1i*randn(N,1));
h_3=sqrt(nois_var_hk_2pow/2)*(randn(N,1)+1i*randn(N,1));
h_4=sqrt(nois_var_hk_2pow/2)*(randn(N,1)+1i*randn(N,1));
h_kk=cat(2,h_1 ,h_2 ,h_3, h_4)
for n=1:4
h_k{n}=h_kk(1:4 , n);
n=n+1;
end

cvx_begin

variable FNNK(N,N,K) semidefinite;%c7
variable rho_k(1,1,K) semidefinite;
variable P

Fkk=cat(2,FNNK);

u=0
for o=1:4
Fk{o}=Fkk(1:4,o+3*u:4*o)
u=u+1;
end
%===============my problem of code begin here================
c5_left_hand = 0;
for k = 1:K
sumja = 0;
for j = 1:K
if j ~= k
sumja = sumja +  h_k{k}' * Fk{j} * h_k{k};
end
end
c5_left_hand = c5_left_hand - sumja+ (h_k{k}' * Fk{k} * h_k{k}*inv_pos(bar_r(1)))
end

c5_right_hand= nois_var_ak_2pow(1)+ ( nois_var_dk_2pow(1)*inv_pos(rho_k(k)) )

c5_left_hand >=  c5_right_hand
``````

And the window told me

``````Error using cvxprob/newcnstr (line 192)
Disciplined convex programming error:
Invalid constraint: {complex affine} >= {convex}

Error in  >=  (line 21)
b = newcnstr( evalin( 'caller', 'cvx_problem', '[]' ), x, y, '>=' );

Error in op4 (line 74)
c5_left_hand >=  c5_right_hand
``````

How do i rewrite the code of formula ?if i modify to real(c5_left_hand) >= real( c5_right_hand),the error will become

``````Error using  .*  (line 262)
Disciplined convex programming error:
Invalid quadratic form(s): not a square.

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

Error in op4 (line 89)
c10_left_hand = c10_left_hand + rho_k(k)*sumja10+nois_var_ak_2pow(1);``````

(Mark L. Stone) #2

Because `h_k` has complex cell elements, there are roundoiff level imaginary components in the summands comprising `c5_left_hand`. Using `real(c5_left_hand)` eliminates the imaginary component, thereby allowing
`real(c5_left_hand) >= c5_right_hand`
So you have already resolved that difficulty.

Error in op4 (line 89)
c10_left_hand = c10_left_hand + rho_k(k)*sumja10+nois_var_ak_2pow(1);

You haven;t shown us the code which comes before it. What is `sumja10`.? If it is a CVX expression, as is `sumja,` then you have a multiplication of two CVX variables, which is non-convex, and could produce the error message you received. I don’t see anything in the image you posted which looks like it would correspond to such code.

(蕭仰恩) #3

i just want to write a for loop for it,so i will set sumja=0 in the begining,just like

``````a=0
for b=1:10
a=a+b
end
``````

so the code means a=0 first,then a =0+1=1,then a=1+2=3,and so on

(Mark L. Stone) #4

What does that code have to do with your error message? Your ''b was apparently the “illegal” multiplication of two CVX expressions (or variables, not a non-CVX number.

(蕭仰恩) #5

you means i multiply two cvx variables so the window show me the error? I have seen this error and modification in your other answer ,so i have modified it before.the variable in this code are just `Fk{k}` and `rho_k`

And in the both c5_left_hand and c5_right_hand,there are no multiplication of two cvx variables

(蕭仰恩) #6

To be honest, i don’t understand this error meaning

(Mark L. Stone) #7

AS I wrote previously
`real(c5_left_hand) >= c5_right_hand`
is accepted by CVV and does not produce any error messages.

You need to show your complete code if you want someone to figure out what your remaining errors are.

(蕭仰恩) #8

modify as you said,the code is ok,but as I wrote previously ,if i write the as real(c5_left_hand) >= real(c5_right_hand),why there is a error like below,but real(c5_left_hand) >= c5_right_hand is ok.

``````Error using  .*  (line 262)
Disciplined convex programming error:
Invalid quadratic form(s): not a square.

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

Error in op4 (line 89)
c10_left_hand = c10_left_hand + rho_k(k)*sumja10+nois_var_ak_2pow(1);``````

(Mark L. Stone) #9

You STILL have not shown the preceding code. You have not shown us how `sumja10` is determined. If, as appears to be the case, `sumja10` is a CVX expression, there is an “illegal” multiplication of CVX variables or expressions, hence the error message.

(蕭仰恩) #10
``````hat_p_lb=-59.2081
%OP4
%declare
K=4;
N=4;
L=5;
nois_var_hk_2pow=0.1*(L^(-2.5))
nois_var_ak_2pow=[10^-7 10^-7 10^-7 10^-7 ];
nois_var_dk_2pow=[10^-5 10^-5 10^-5 10^-5 ];
bar_r=[10 10 10 10];
%P_hat=0.00001;
P_T=10000;
h_1=sqrt(nois_var_hk_2pow/2)*(randn(N,1)+1i*randn(N,1));
h_2=sqrt(nois_var_hk_2pow/2)*(randn(N,1)+1i*randn(N,1));
h_3=sqrt(nois_var_hk_2pow/2)*(randn(N,1)+1i*randn(N,1));
h_4=sqrt(nois_var_hk_2pow/2)*(randn(N,1)+1i*randn(N,1));
h_kk=cat(2,h_1 ,h_2 ,h_3, h_4)
for n=1:4
h_k{n}=h_kk(1:4 , n);
n=n+1;
end

cvx_begin

variable FNNK(N,N,K) semidefinite;%c7
variable rho_k(1,1,K) semidefinite;
variable P
%==========================
Fkk=cat(2,FNNK);

u=0
for o=1:4
Fk{o}=Fkk(1:4,o+3*u:4*o)
u=u+1;
end
tr_ace=0
for t=1:K
tr_ace=tr_ace+trace(Fk{t})
end
%====================================
%object function
minimize( tr_ace )
%====================================
%Constraint
subject to
%c3
rho_k<=1

%c5
%===================================================
c5_left_hand = 0;
for k = 1:K
sumja = 0;
for j = 1:K
if j ~= k
sumja = sumja +  h_k{k}' * Fk{j} * h_k{k};
end
end

c5_left_hand = c5_left_hand - sumja+ (h_k{k}' * Fk{k} * h_k{k}*inv_pos(bar_r(1)))

end

c5_right_hand= nois_var_ak_2pow(1)+ ( nois_var_dk_2pow(1)*inv_pos(rho_k(k)) )
real( c5_left_hand ) >=   c5_right_hand
%===================================================
%c10
c10_left_hand = 0;
for k = 1:K
sumja10 = 0;
for j = 1:K
if j ~= k
sumja10 = sumja10 +  h_k{k}' * Fk{j} * h_k{k};
end
end
c10_left_hand = c10_left_hand + sumja10+nois_var_ak_2pow(1);
end
for k=1:K
c10_right_hand=hat_p_lb*inv_pos(1-rho_k(k))
end
real(c10_left_hand)>= real( c10_right_hand)
cvx_end``````

(蕭仰恩) #11

i know your meaning now,i forget to paste the code about `sumja10`,here is the total code,and you can see the `sumja10` just like the `sumja`,but `sumja10` is in the constraint 10,`sumja` is in the constraint 5.Sorry for my mistake

now the problem about constraint 10 is

``````Error using cvxprob/newcnstr (line 192)
Disciplined convex programming error:
Invalid constraint: {real affine} >= {concave}

Error in  >=  (line 21)
b = newcnstr( evalin( 'caller', 'cvx_problem', '[]' ), x, y, '>=' );

Error in op4 (line 96)
real(c10_left_hand)>= real( c10_right_hand)``````

(Mark L. Stone) #12

You haven;t shown the code for `hat_p_lb`, therefore the problem us not reproducible. However, given the error message you received, which shows that `c10_right_hand` is concave, it must be that `hat_p_lb < 0` when the error message was produced,.because `inv_pos(1-rho_k(k))` is convex.

Also note that every time through the for loop from 1 to K, `c10_right_hand` is being overwrittten, so its value when used in the following constraint is `hat_p_lb*inv_pos(1-rho_k(K))` and the first K-1 times through the for loop have no effect on the optimization problem, i.e., are meaningless.

(蕭仰恩) #13

`hat_p_lb` is a negative value,so you mean that it has to be a positive value,or the error will be shown?
`rho_k(k)` is not all the same value,that’s why i still write it in the for k=1:K loop,i mean,the rho_k(1) is not equal to rho_k(2),etc. i said `rho_k(k)` ,not `rho_k(K)`

(Mark L. Stone) #14

A negative number times a convex expression is a concave expression, It is not allowed to have concave_expression <= anything or anything >= concave_expression. I.e.,such a constraint is non-convex.

The last time through the for loop, k has the value K, so that is the only one used, because
`c10_right_hand` is overwritten every time through the loop.

(蕭仰恩) #15

oh i know your meaning now,i think have to modify this code for a little bit

(蕭仰恩) #16

so if i want a negative number times something to be convex expression, that something has to be “concave expression”? can you should me how modify a convex expression to be concave expression?

(Mark L. Stone) #17

so if i want a negative number times something to be convex expression, that something has to be “concave expression”?

Yes.

Ar this point, you don;t need help in CVX, you need help in the mathematics of convex optimizatio. I suggest you read and solve a lot of the exercises in at least the first 5 chapters of “Convex Optimization”, by Boyd and Vandenberghe, which is freely downloadable at http://web.stanford.edu/~boyd/cvxbook/ . If you have difficulties getting through that book, https://math.stackexchange.com/ would be a more appropriate forum than this one to get assistance. You will never learn anything by taking problems you don’t understand from books and papers and trying to implement them in CVX.,