CVX gives error on sdpvar

I get the following error while using CVX for solving an optimization problem.
Unrecognized function or variable ‘sdpvar’.

how can I solve this?

sdpvar is used to declare optimization variables in YALMIP, not CVX.

even if I declare the variables with variable command, I get error

Please show a complete MATLAB session and output exhibiting the error.

Have you installed CVX (2.2)? What is the output of cvx_version ?

output of cvx_version:

CVX: Software for Disciplined Convex Programming ©2014 CVX Research
Version 2.2, Build 1148 (62bfcca) Tue Jan 28 00:51:35 2020

cvx_begin sdp
cvx_solver mosek;
variable x(n)
minimize(x(3)+(x(5)-Z(1,j)+Lambda(1,j))^2 + (x(6)-Z(1,j+1)+Lambda(1,j+1))^2 ... 
         + (x(7) - Z(2,j) + Lambda(2,j))^2 + (x(8) - Z(2,j+1) + Lambda(2,j+1))^2)
subject to
B(:,k:k+1) * [x(1);x(2)] >= (0.1) * alpha1;
Bp2(:,k:k+1) * [x(1);x(2)] >= (0.1) * alpha1;
(Bp2(:,k:k+1) - B(:,k:k+1)) * [x(1);x(2)] - x(3) * ones(N,1)...
       - x(5) * ww(:,i) - x(6) * wy(:,i) - x(7) * yw(:,i) - x(8) * yy(:,i) <= 0;
x(4) <= 0.95;
x(3) >= -5;
cvx_end
X(1:2,j:j+1) = [x(5)  x(6) ; x(7)  x(8)];
Q(:,i) = [x(1);x(2)];
eta(i) = x(3);
k = k+2;
j = j+2;

end

% Global condition verification
k = 1;
X11 = zeros(ell,ell); X12 = zeros(ell,ell);
X21 = zeros(ell,ell); X22 = zeros(ell,ell);
for i = 1:ell
X11(i,i) = X(1,k);
X12(i,i) = X(1,k+1);
X21(i,i) = X(2,k);
X22(i,i) = X(2,k+1);
k = k+2;
end
Xx = [X11 X12 ; X21 X22];
V = [M ; eye(ell)]'Xx[M ; eye(ell)];
if max(eig(V)) <= 0
s = 2
break
end
% Global optimization problem
%cvx_begin sdp

Z = sdpvar(2,2*ell);

% Add LMIs
Z11 = zeros(ell,ell); Z12 = zeros(ell,ell);
Z21 = zeros(ell,ell); Z22 = zeros(ell,ell);
k = 1;
for i = 1:ell
Z11(i,i) = Z(1,k);
Z12(i,i) = Z(1,k+1);
Z21(i,i) = Z(2,k);
Z22(i,i) = Z(2,k+1);
k = k+2;
end
Zz = [Z11 Z12 ; Z21 Z22];

LMI = [[M ; eye(ell)]'Zz[M ; eye(ell)] <= -1e-2*eye(ell)];

optimize(LMI,norm(X(1:2,1:2)-Z1+Lambda(1:2,1:2),‘fro’)^2+norm(X(1:2,3:4)-Z2+Lambda(1:2,3:4),‘fro’)^2 …
+norm(X(1:2,5:6)-Z3+Lambda(1:2,5:6),‘fro’)^2+norm(X(1:2,7:8)-Z4+Lambda(1:2,7:8),‘fro’)^2 …
+norm(X(1:2,9:10)-Z5+Lambda(1:2,9:10),‘fro’)^2 …
+norm(X(1:2,11:12)-Z6+Lambda(1:2,11:12),‘fro’)^2+norm(X(1:2,13:14)-Z7+Lambda(1:2,13:14),‘fro’)^2 …
+norm(X(1:2,15:16)-Z8+Lambda(1:2,15:16),‘fro’)^2+norm(X(1:2,17:18)-Z9+Lambda(1:2,17:18),‘fro’)^2 …
+norm(X(1:2,19:20)-Z10+Lambda(1:2,19:20),‘fro’)^2 …
+norm(X(1:2,21:22)-Z11+Lambda(1:2,21:22),‘fro’)^2+norm(X(1:2,23:24)-Z12+Lambda(1:2,23:24),‘fro’)^2 …
+norm(X(1:2,25:26)-Z13+Lambda(1:2,25:26),‘fro’)^2+norm(X(1:2,27:28)-Z14+Lambda(1:2,27:28),‘fro’)^2 …
+norm(X(1:2,29:30)-Z15+Lambda(1:2,29:30),‘fro’)^2 …
+norm(X(1:2,31:32)-Z16+Lambda(1:2,31:32),‘fro’)^2+norm(X(1:2,33:34)-Z17+Lambda(1:2,33:34),‘fro’)^2 …
+norm(X(1:2,35:36)-Z18+Lambda(1:2,35:36),‘fro’)^2+norm(X(1:2,37:38)-Z19+Lambda(1:2,37:38),‘fro’)^2 …
+norm(X(1:2,39:40)-Z20+Lambda(1:2,39:40),‘fro’)^2);
check(LMI)

Z1 = value(Z1);
Z2 = value(Z2);
Z3 = value(Z3);
Z4 = value(Z4);
Z5 = value(Z5);
Z6 = value(Z6);
Z7 = value(Z7);
Z8 = value(Z8);
Z9 = value(Z9);
Z10 = value(Z10);
Z11 = value(Z11);
Z12 = value(Z12);
Z13 = value(Z13);
Z14 = value(Z14);
Z15 = value(Z15);
Z16 = value(Z16);
Z17 = value(Z17);
Z18 = value(Z18);
Z19 = value(Z19);
Z20 = value(Z20);

error:
Unrecognized function or variable ‘sdpvar’ from command “Z = sdpvar(2,2*ell);”

and when I declare variable Z(2,2*ell), again I get the error Unrecognized function or variable ‘variable’ .

Are you using YALMIP or CVX? The first thing you need to do is decide whether you are using YALMIP or CVX. Don’t intermix them, You haven’t actually shown an error associated with your CVX program.

If you are using YALMIP, seek help at https://groups.google.com/g/yalmip , not at the CVX forum.