# Error in defining a constraint

Greetings,
I am trying to define a constrain which involves the following multiplication x’*x. However, it give me error message.
Can anyone help me with this issue ?

Please show the entire constraint, along with all CVX variable declarations and the values of any MATLAB variables which may appear.

If `x` is declared as a CVX scalar variable, i.e., `variable x`, then `x*x` is allowed in a constraint or objective function if used in a convex way. For example, `x*x <= 2` is allowed, but `x*x == 2` and `x*x >= 2`, both of which are non-convex, are not allowed

1 Like

The below picture shows my objective func and constrains. X is a matrics of size (2xN) yi is a (2x1) scalar vector.
I defined the objective function without a problem. But I faced issue with defining the constrain hij.

You should be writing (16) as `[eye(2) X;X' Z] == semidefinite(N+2)` (or `[eye(2) X;X' Z] >= 0` if using sdp mode). You should NOT be using the equivalent by Schur Complement `Z - X'*X == semidefinite(N)` (or `Z - X'*X >= 0` if using semidefinite mode).

You should not have `Z == X'*X` or `Z = X'*X` anywhere in the code. Constraint (16) is a convex semidefinite relaxation of `Z == X'*X`, and per the last sentence in the posted image, I guess you have to hope that the optimal solution of the relaxation formulation happens to satisfy `Z = X'X` (i.e., `Z` is rank one) at the optimal returned `X`.

1 Like

This is my formulation

lam=10.^[PR/5*n];
y=AncLoc’;
eN=eye(N)’; % node to node
eM=eye(M)’; % anchor to node

cvx_begin
cvx_solver sedumi
variables x(2,N) p(N) a(N) hN(N,N) hM(M,N) Z(N,N) h(M+N,N)
expressions f

for i=1:N
for j=1:M+N
if i+5==j
continue
end
f= f+[lam(1,j,i)*h(j,i)-a(i)]^2;
end
end

minimize(f)

% for Anchors
for i=1:M
for j=1:N
hM(i,j)==[y(:,i);-eN(:,j)]’[eye(2) x;x’ Z][y(:,i);-eN(:,j)]
end
end
% for Nodes
for i=1:N
for j=1:N
hN(i,j)==[[0;0];eN(:,i)-eN(:,j)]’[eye(2) x;x’ Z][[0;0];eN(:,i)-eN(:,j)];
end
end

h==[hM;hN];

[eye(2) x;x’ Z] == semidefinite(N+2)
cvx_end

after I run the code it provide this error
Status: Error
Optimal value (cvx_optval): NaN

Error using sparse
Index into matrix must be positive.

Error in cvx_sedumi>solve (line 144)
reord = sparse( …

Error in cvx_solve (line 399)
[ x, status, tprec, iters, y ] = shim.solve( At, b, c, cones, params );

Error in cvx_finish

Error in cvx_end (line 11)
evalin( ‘caller’, ‘cvx_finish’ );

Error in deleteme (line 64)
cvx_end

That error message looks like it is due to an installation issue and/or CVX or SeDuMi bug. Try a different solver, such as SDPT3, or Mosek, if you have it available

However, maybe the solver has finished “solving” the problem and determined that is is infeasible?. Please show all solver and CVX output.

1 Like

The same error occure for SDPT3 solver. UnfortunatelyI don’t have Mosek solver

## Calling SDPT3 4.0: 275 variables, 126 equality constraints For improved efficiency, SDPT3 is solving the dual problem.

Status: Error
Optimal value (cvx_optval): NaN

Array indices must be positive integers or logical values.

Error in cvx_sdpt3>solve (line 161)
Avec{end+1,1} = At(ti,:);

Error in cvx_solve (line 399)
[ x, status, tprec, iters, y ] = shim.solve( At, b, c, cones, params );

Error in cvx_finish

Error in cvx_end (line 11)
evalin( ‘caller’, ‘cvx_finish’ );

Error in SDP_1 (line 66)
cvx_end

If you provide a reproducible problem, complete with all input data, perhaps someone can help you. if you post something, use the Prepormatted text icon, copy and paste it into a new MATLAB session, and verify that the output matches what you have shown.

1 Like

I appologies for not providing the complete code. The below is the complete code

``````clear
clc
% Variables
t=0:0.0001:0.01;
n=2; % attenuation factor
o=2; % Vatiance
N=7; %number of Nodes
M=5; % Number of Anchors

%% Transmitter
Sig=sin(2000*t)'+5+randn(1,N);% signals
pt=10*log10(Sig*1e-3)+30;
%% Reciever
AncLoc=[0 0;0 20;20 0;20 20;10 10]; % Anchor Locations
NodLoc=[1+randi(18,N,2)]; % create random location between 1-19
D= pdist2(NodLoc,[AncLoc;NodLoc],'euclidean'); % euclidean between node and anchors
PR=zeros(1,N+M,N);
for j=1:1:500
for i=1:1:N % mean of the recieved power at one iteration
pr(:,:,i)= [pt(:,i)-10*n*log10(D(i,:))]+randn(max(size(t)),M+N)*o; % recieved power on nodes and anchor from sensor i
PR(:,:,i)=[mean(pr(:,:,i),1)+PR(:,:,i)]/2; % avaraging the Recieved Power
PR(PR==Inf)=0;
end
end

%% CVX start
lam=10.^[PR/5*n];
y=AncLoc';
eN=eye(N); % node to node
eM=eye(M); % anch to node

cvx_begin
cvx_solver SDPT3
variables x(2,N) p(N) a(N)  hN(N,N) hM(M,N) Z(N,N) h(M+N,N)
expressions f

% for Anchors
for i=1:M
for j=1:N
hM(i,j)==[y(:,i);-eN(:,j)].'*[eye(2) x;x' Z]*[y(:,i);-eN(:,j)]
end
end
% for Nodes
for i=1:N
for j=1:N
hN(i,j)==[[0;0];eN(:,i)-eN(:,j)].'*[eye(2) x;x' Z]*[[0;0];eN(:,i)-eN(:,j)];
end
end
h==[hM;hN];

for i=1:N
for j=1:M+N
if i+5==j
continue
end
f= f+[lam(1,j,i)*h(j,i)-a(i)]^2;
end
end

minimize(f)

[eye(2) x;x' Z] == semidefinite(N+2)
cvx_end``````

Your MATLAB session might be corrupted. Try running this is a new MATLAB session. If that doesn’t succeed, try reinstalling CVX.

I actually duplicated your error when I ran it in an old session of MATLAB. But it ran successfully under 3 different solvers when I ran it in a new MATLAB session. I don’t know what kind of “corruption” is occurring, and whether it is due to a CVX bug.

1 Like