# Invalid quadratic form(s): not a square

Hi, everyone.I have problem to solve the following question.

``````cvx_begin
variable p(N)
expression y
y = 1;
for i = 1 : 50
y = y*( 1 + h_ss(i)^2*p(i)/(var_n + J(i) + M(i)) );
end
y = df*log2(y);
maximize(y)
subject to
for i = 1:N
A(i)*p(i) <= Ith;
p(i) >= 0;
end
cvx_end
``````

error

``````Error using cvx/times (line 262)
Disciplined convex programming error:
Invalid quadratic form(s): not a square.
Error in cvx/mtimes (line 36)
z = feval( oper, x, y );
Error in main (line 74)
y = y*( 1 + h_ss(i)^2*p(i)/(var_n + J(i) + M(i)) );
``````

Actually, this is convex, and CVX can solve it, but you can’t build it in the way you have. CVX requires every single subexpression you build to follow the rules. It is doing precisely the right thing rejecting the products that you are building in that for loop, because they are neither convex nor concave.

But the logarithm of that product, which is what you’re really going for, is concave, so you can indeed maximize it with CVX/

The challenge is just to get it into CVX in a disciplined manner. Here is what I would do:

``````cvx_begin
variable p(N)
expression y(N)
for i = 1 : 50
y(i) = 1 + h_ss(i)^2*p(i)/(var_n + J(i) + M(i));
end
maximize(geo_mean(y))
``````

or even better:

``````cvx_begin
variable p(N)
expression y(N)
y = 1 + h_ss .* p ./ ( var_n + J + M );
maximize(geo_mean(y))
``````

Notice that I’m using `geo_mean(y)` as my objective instead of `log_prod(y)` or `sum_log(y)`, which would be closer to what you asked for. This should give you the same value of `y`, but by avoiding the logarithm, you’re avoiding the successive approximation warning that CVX offers when using the logarithm.

hi,
I have the same error but do not know how to solve it.
cvx_begin quiet
variables a(Kf,U,N,M) s(Kf,U,N,M) g(Kf,U,N,M) x(Kf,U) q(Kf,U,N,M)
variables eta(Kf,1)

``````%objective function
maximize (sum(eta));

subject to
for k=1:Kf
for u=1:U
sum(sum( a(k,u,:,:).*log(1+gi(k,u,:,:)) + (g(k,u,:,:)-gi(k,u,:,:))./(1+gi(k,u,:,:)) )) / log(2) >= x(k,u) ;
end
for n=1:N
for m=1:M
for u=1:U

(g(k,u,n,m)^2)*l(k,u,n,m)/2 - s(k,u,n,m)*h_new(k,u,n,m) + ((a(k,u,n,m)*q(k,u,n,m))^2)/(2*l(k,u,n,m))<=0;%((a(k,u,n,m)*q(k,u,n,m))^2)/(2*l(k,u,n,m)) <= 0;
end
end
end

end
cvx_end
``````

I will be grateful if you can help me to debug this error . In addition, how to avoid the Log approximation warning?

The term `((a(k,u,n,m)*q(k,u,n,m))^2)/(2*l(k,u,n,m))` in your second loop of constraints violates CVX’s rules, and is a product of CVX variables `a` and `q`, which is a violation (and is neither convex nor concave), even before the squaring.

I will declare this to be non-convex unless you can show that it actually is convex.

I don’t see why you would get any Log approximation warning in your problem, because the only logarithms do not involve CVX variables.

excuse me
I just started using CVX, and when I try to run the following Program, it shows the error ‘not a square’ in the ’ 4 * A * C == -1’,but I can’t find the reason,could you give me a guide?

cvx_begin

``````    variables A B C D E F
expression   P(N)
for n=1:N
P(n)=A*(x(n)^2)+B*x(n)*y(n)+C*(y(n)^2)+D*x(n)+E*y(n)+F;
end

minimize( norm(P,2))
subject to
B == 0

4 * A * C == -1

(E / (- 2 * C ))< MinY

(D/(-2*A)) <= Xlr(i,2)

(D/(-2*A)) >= Xlr(i,1)

cvx_end
``````

error

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

z = feval( oper, x, y );

4 * A * C == -1

excuse me
I just started using CVX, and when I try to run the following Program, it shows the error ‘not a square’ in the ’ 4 * A * C == -1’,but I can’t find the reason,could you give me a guide?

cvx_begin

``````    variables A B C D E F
expression   P(N)
for n=1:N
P(n)=A*(x(n)^2)+B*x(n)*y(n)+C*(y(n)^2)+D*x(n)+E*y(n)+F;
end

minimize( norm(P,2))
subject to
B == 0

4 * A * C == -1

(E / (- 2 * C ))< MinY

(D/(-2*A)) <= Xlr(i,2)

(D/(-2*A)) >= Xlr(i,1)

cvx_end
``````

error

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

z = feval( oper, x, y );

4 * A * C == -1

Your model is nonconvex. Hence, as formulated there no way you can solve it with CVX.

thank you for your help and it benifit me a lot

Hi,

I have started using CVX, and such a great tool! I am writing a journal paper and found a way to solve an optimization problem using water-filling method, and I was interested to solve the problem with CVX as well and cite in my paper, but could not perfectly get the same results. Following is my MATLAB code to solve a very simple problem, which we can simply find the answer. I believe If we can solve this error, I can solve my main MATLAB optimization problem for the paper, which is very similar to this example with same variables.

I appreciate if you can address why CVX gives: “Invalid quadratic form(s): not a square” error.

Please note that I had to use Mosek solver in order to define the binary variable (alp). Please also note when we change the times (.*) operator with plus (+) in the objective, the CVX can solve the problem.

## MATLAB code:

clc
clear all
close all

nSubcarriers=10;
totalPower=100;

cvx_solver Mosek

cvx_begin

``````variable P(1,nSubcarriers)
variable alp(1,nSubcarriers) binary
expression objective(1,nSubcarriers)
objective=alp.*P
maximize(sum(objective))
subject to
P>=0
P <= 10
sum(P) == totalPower
``````

## cvx_end

CVX does not allow products of CVX variables. Hence the somewhat cryptic error message.

However, `objective` can be linearized by using big M, resulting in this case in a MILP, and solved with Mosek in CVX.

Thanks Mark. Unfortunately I am not familiar with big M, more advise or links regarding this will be appreciated.
And you mean this problem with two variables multiplication can be solved with big M and Mosek solver?

Search for Big M at https://or.stackexchange.com/