# The same problem:Invalid quadratic form(s): not a square

Here is my core code,i don’t know what’s wrong with these code?Is my constraints wrong?please help me .Thanks!

``````G_pre=0.1;
a=60;
b=90;
P1=SelectP( a,b,1 );
while(1)
cvx_begin sdp quiet
variables W_1(N*N,N*N)  P1
maximize  real(trace(B_2_2*W_1))
subject to
trace(Q_2_2*W_1)==1;
%     real(trace(R1*W_1))>=0;
%     real(trace(R2*W_1))>=0;
P1*real(trace(b_1*W_1))-P2*real(trace(b_1*W_1))*t1-real(trace(c_1+D*Pr^(-1)))*t1>=0;
P1*real(trace(b_2*W_1))-P2*real(trace(b_2*W_1))*t1-real(trace(c_2+D*Pr^(-1)))*t1>=0;
P2=Ps-P1;
cvx_end
cvx_optval
W_1
a
b

if cvx_optval>G_pre
a=P1;
P1=SelectP( a,b,1);
G_pre=cvx_optval;
else
b=P1;
P1=SelectP( a,b,2);
G_pre=cvx_optval;
end
if abs(b-a)<0.1
break;
end
end
``````

Here is my faults:

Disciplined convex programming error:
Invalid quadratic form(s): not a square.

P1real(trace(b_1W_1))-P2real(trace(b_1W_1))t1-real(trace(c_1+DPr^(-1)))*t1>=0;

You should not declare P1 as a (CVX) variable. You got that error because you did declare it a variable.

Also, you don’t define P2 before first use (at least in the code you showed us). Perhaps you want `P2=Ps-P1;`to be earlier in the program? I’ll let you figure out where it goes.

Thanks!
But I want to do in this way: Given P1,Iwant to figure out this problem by 1-D search over P1.I think the P1 is always changing,so I let P1 variable. By given P1(such P1 is from[a,b]),and I can solve the sdp problem.I can show you all my code,it is very simple.
clc;
clear;
cvx_setup
%=================================
%SystemSetup
%=================================
%numbers of antennas
N=4;
%numbers of users
K=2;
sigma = 1.0;% set noise 1.
dr=1;%源到中继的距离
d1=1;%用户1的距离
d2=0.5;%用户2的距离（近）

coe_dr=sqrt(dr^(-2));% 系数
coe_d1=sqrt(d1^(-2));
coe_d2=sqrt(d2^(-2));
r_2 = [];
% for Pr_dB=10:5:30
% Pr=10^(0.1Pr_dB);
Ps=100;%w不是dB
Pr=100;%w不是dB
%Pr=20;%according to fig to set power of relay.(dB)
% P1=0.8
Ps; %when 0 interation the value of p1.(dB)
P1=0.6*Ps;
P2=Ps-P1; %when 0 interation the value of p2.(dB)
CH_iter_max=10;%信道的数目
bug_iter=0;%解不出来的迭代次数(个数)
r2_sum=0;%r2速率的总和
% for CH_iter=1:CH_iter_max %换信道
n=16;% the dimension of f

% h=coe_dr*(randn(N,1)+1irandn(N,1));
% g1=coe_d1
(randn(N,1)+1irandn(N,1));
% g2=coe_d2
(randn(N,1)+1i*randn(N,1));

h=coe_dr*(rand(N,1)+1irand(N,1));
g1=coe_d1
(rand(N,1)+1irand(N,1));
g2=coe_d2
(rand(N,1)+1i*rand(N,1));

F=randn(N,N);
I1=eye(N);%setup unit matrix N dimension.
I2=eye(N^2);
f=vec(F);
W=f*f’;

B_1_1=P1kron((conj(h)h.’),g1g1’);%(13)k=1,j=1
B_2_1=P1
kron((conj(h)h.’),g2g2’);%(13)k=1,j=2
B_2_2=P2*kron((conj(h)h.’),g2g2’);%(13)k=2,j=2
b_1=kron((conj(h)h.’),g1g1’);%mu_1没有P1,后面需要
b_2=kron((conj(h)h.’),g2g2’);%mu_2

C_1_1=P2kron(conj(h)h.’,g1g1’)+kron(sigmasigmaI1,g1g1’);%(14)k=1,j=1
C_2_1=P2kron(conj(h)h.’,g2g2’)+kron(sigmasigmaI1,g2g2’);%(14)k=1,j=2
C_2_2=kron(sigmasigmaI1,g2g2’);%(14)k=2,j=2
c_1=kron(sigma
sigmaI1,g1g1’);%C_1_1后面的一部分
c_2=kron(sigmasigmaI1,g2*g2’);

D=Pskron(conj(h)h.’,I1)+sigmasigmaI2;%(16)

r1=0.6;
t1=2^(2r1)-1;%（7）
eta=1/trace(Q_2_2
W);
W_1_1=W*eta;

R1=B_1_1-t1Q_1_1;
R2=B_2_1-t1
Q_2_1;

%=================================
%Initialize
%=================================

%=================================
%Repeat
%=================================
%==================================
%obtain f(m) by solving (34)iteratively with {Pk(m-1)}
%==================================
G_pre=0.1;
a=60;
b=90;
P1=SelectP( a,b,1 );
while(1)
cvx_begin sdp quiet
variables W_1(NN,NN) P1
maximize real(trace(B_2_2W_1))
subject to
trace(Q_2_2
W_1)==1;
% real(trace(R1W_1))>=0;
% real(trace(R2
W_1))>=0;
P1real(trace(b_1W_1))-P2real(trace(b_1W_1))t1-real(trace(c_1+DPr^(-1)))t1>=0;
P1
real(trace(b_2W_1))-P2real(trace(b_2*W_1))t1-real(trace(c_2+DPr^(-1)))*t1>=0;
P2=Ps-P1;
cvx_end
cvx_optval
W_1
a
b

``````if cvx_optval>G_pre
a=P1;
P1=SelectP( a,b,1);
G_pre=cvx_optval;
else
b=P1;
P1=SelectP( a,b,2);
G_pre=cvx_optval;
end
if abs(b-a)<0.1
break;
end
``````

end

This is my modified code,still have the same problem
clc;
clear;
cvx_setup
%=================================
%SystemSetup
%=================================
%numbers of antennas
N=4;
%numbers of users
K=2;
sigma = 1.0;% set noise 1.
dr=1;%源到中继的距离
d1=1;%用户1的距离
d2=0.5;%用户2的距离（近）

coe_dr=sqrt(dr^(-2));% 系数
coe_d1=sqrt(d1^(-2));
coe_d2=sqrt(d2^(-2));
r_2 = [];
% for Pr_dB=10:5:30
% Pr=10^(0.1Pr_dB);
Ps=100;%w不是dB
Pr=100;%w不是dB
%Pr=20;%according to fig to set power of relay.(dB)
% P1=0.8
Ps; %when 0 interation the value of p1.(dB)
% P1=0.6*Ps;
% P2=Ps-P1; %when 0 interation the value of p2.(dB)
CH_iter_max=10;%信道的数目
bug_iter=0;%解不出来的迭代次数(个数)
r2_sum=0;%r2速率的总和
% for CH_iter=1:CH_iter_max %换信道
n=16;% the dimension of f

% h=coe_dr*(randn(N,1)+1irandn(N,1));
% g1=coe_d1
(randn(N,1)+1irandn(N,1));
% g2=coe_d2
(randn(N,1)+1i*randn(N,1));

h=coe_dr*(rand(N,1)+1irand(N,1));
g1=coe_d1
(rand(N,1)+1irand(N,1));
g2=coe_d2
(rand(N,1)+1i*rand(N,1));

F=randn(N,N);
I1=eye(N);%setup unit matrix N dimension.
I2=eye(N^2);
f=vec(F);
W=f*f’;

G_pre=0.1;
a=60;
b=90;
P1=SelectP( a,b,1 );
P2=Ps-P1;

B_1_1=P1kron((conj(h)h.’),g1g1’);%(13)k=1,j=1
B_2_1=P1
kron((conj(h)h.’),g2g2’);%(13)k=1,j=2
B_2_2=P2*kron((conj(h)h.’),g2g2’);%(13)k=2,j=2
b_1=kron((conj(h)h.’),g1g1’);%mu_1没有P1,后面需要
b_2=kron((conj(h)h.’),g2g2’);%mu_2

C_1_1=P2kron(conj(h)h.’,g1g1’)+kron(sigmasigmaI1,g1g1’);%(14)k=1,j=1
C_2_1=P2kron(conj(h)h.’,g2g2’)+kron(sigmasigmaI1,g2g2’);%(14)k=1,j=2
C_2_2=kron(sigmasigmaI1,g2g2’);%(14)k=2,j=2
c_1=kron(sigma
sigmaI1,g1g1’);%C_1_1后面的一部分
c_2=kron(sigmasigmaI1,g2*g2’);

D=Pskron(conj(h)h.’,I1)+sigmasigmaI2;%(16)

r1=0.6;
t1=2^(2r1)-1;%（7）
% eta=1/trace(Q_2_2
W);
% W_1_1=W*eta;

R1=B_1_1-t1Q_1_1;
R2=B_2_1-t1
Q_2_1;

%=================================
%Initialize
%=================================

%=================================
%Repeat
%=================================
%==================================
%obtain f(m) by solving (34)iteratively with {Pk(m-1)}
% %==================================
% G_pre=0.1;
% a=60;
% b=90;
% P1=SelectP( a,b,1 );
while(1)
cvx_begin sdp quiet
variables W(NN,NN) P11
maximize real(P2trace(b_2W)/(trace((c_2+DPr^(-1)))))
subject to
% trace(Q_2_2
W)==1;
% real(trace(R1W))>=0;
% real(trace(R2
W))>=0;
P11real(trace(b_1W))-P2real(trace(b_1W))t1-real(trace(c_1+DPr^(-1)))t1>=0;
P11
real(trace(b_2W))-P2real(trace(b_2*W))t1-real(trace(c_2+DPr^(-1)))*t1>=0;
P2=Ps-P11;
cvx_end
cvx_optval
W
a
b

``````if cvx_optval>G_pre
a=P11;
P11=SelectP( a,b,1);
G_pre=cvx_optval;
else
b=P11;
P11=SelectP( a,b,2);
G_pre=cvx_optval;
end
if abs(b-a)<0.1
break;
end
``````

end

I told you not to declare P! as a CVX variable. If P1 is a CVX variable, the program is not convex, and is not accepted by CVX.

P1 is a MATLAB variable. You are controlling the value of P1 outside of CVX’s control. So every time you run CVX, P1 will be a constant value as far as CVX is concerned. The whole point of controlling P1 outside CVX, and keeping it fixed for a given CVX instantiation, is to avoid having a non-convex problem which CVX will not accept.

There is a big difference between a CVX variable and a MATLAB variable. A MATLAB variable is considered by CVX to be a constant. That is what you need P1 to be. You can still change P1 all you want, so long as you don’t declare it to be a CVX variable.

Thank you very much!!You help me a lot,I just started with cvx, so some basic questions and grammar are not well understood, Thank you so much! I hope I can communicate with you in the future!There is no problem in this cvx, but the solution is Inf, the status is unbounded, I will continue to figure out the problem, thank you very much again!!!