How to solve Unable to perform assignment because value of type 'cvx' is not convertible to 'double'

I have a problem here .Please help me,thank you very much.

Code:

global K N noise B R0 h p1 h0 P_max  t E v_max H1 H2 T
K=6;
N=40;
noise=1e-14;
B=1;
R0=0.4;
h=1e-9;
p1=400;
h0=2.2e-5;  
P_max=1;
t=0.2;
T=8;
E=18;
v_max=50;
H1=100;
H2=500;

a=ones(K,N)*(1/6);
p2=ones(1,N)*0.5;
g=ones(1,N)*0.2;

cvx_begin
    %cvx_quiet true
    variable q(3,N)
    variable r(K,N)
    expression d2(K,N)
    expression gamma2(K,N)
    expression R2(K,N)
    expression w2(3,N,K)
    expression x1(1,6)
    expression y1(1,6)
    expression z1(1,K)
    expression k
    x1=[-2200,-2400,-1000,0,2000,1500];
    y1=[-2100,1800,-500,-1000,-1500,2000];
    w2=zeros(3,N,K);
    z1=zeros(1,K);
    for k=1:K
        for n=1:N
            w2(:,n,k)=[x1(k),y1(k),z1(k)]';
        end
    end          
    d2=zeros(K,N);
    for k=1:K
       d2(k,:)=sum_square(q-w2(:,:,k));
end
gamma2=h0*ones(K,1)*p2.*r./(ones(K,N)*noise);  
R2=B*ones(K,1)*(1-g).*a.*(log(1+gamma2)./log(2));

maximize ((1/N)*sum(R2(:))) 
subject to  
    (1/N)*sum(B*ones(K,1)*(1-g).*a.*(log(1+gamma2)./log(2)),2) >= R0;   
    q(:,1) == q(:,N);
    H1<=q(3,:)<=H2;
    for n=1:N-1
       sum_square(q1(:,n+1)-q1(:,n)) <= (v_max*t)^2;
    end
    r<=1./d2;

cvx_end

output
Unable to perform assignment because value of type ‘cvx’ is not convertible to ‘double’.

error traj (Line 47)
d2(k,:)=sum_square(q-w2(:,:,k));

reason:
Misusing double
Cannot convert from cvx to double.

You declared d2 as an expression holder (array), but then set d2=zeros(K,N);, which supersedes the earlier declaration, and makes d2 a double precision array. As a consequence d2(k,:)=sum_square(q-w2(:,:,k)); produces the error message.

Tou should remove d2=zeros(K,N);, from your program. All elements of CVX expression holders (arrays) are initialized to zero, so need not be explicitly set thereto.

http://cvxr.com/cvx/doc/basics.html#assignment-and-expression-holders