Defining a Variable in CVX

Hello All,
I have a variable which is called S to define but when I tried to define it, it gives me “NaN”
Could you please kindly show me it?
t
s

I tried like that

cvx_begin

variable S(d,d,m,m)
for a=1:d

 for b =1:d

  for x=1:m

   for y =1:m

    if a ~= b

     S(a,b,x,y) == sum(T(a,b,x,y)); %T is coming from my other code and it is exactly equal the formula and it is true
     J=S(a,b,x,y).*W; .%this is my objective function
    end

   end

  end

 end

end

Please show us exactly what the error or other information or indication is that the variable is NaN.

S is Nan:
nans

(The T is coming from other code and is is ok and S should be the sum of T which has the different value for a and b )

Please show us the context. Was this after cvx_end? Did you look at the solver and CVX output? Perhaps the problem was infeasible, or the solver experienced some other error (failed)?

This was the my codes G is also 0 it is not true I should have a value for G. Yeah you are right status is infeasible but how can I solve that? Yesterday I asked the problem to forum with the eq. but no answer…
This is the result:
> Calling SDPT3 4.0: 32 variables, 1 equality constraints
> For improved efficiency, SDPT3 is solving the dual problem.
> ------------------------------------------------------------
>
> num. of constraints = 1
> dim. of sdp var = 16, num. of sdp blk = 2
>
> *******************************************************************
> SDPT3: Infeasible path-following algorithms
> *******************************************************************
> version predcorr gam expon scale_data
> HKM 1 0.000 1 0
> it pstep dstep pinfeas dinfeas gap prim-obj dual-obj cputime
> -------------------------------------------------------------------
> 0|0.000|0.000|4.0e+01|1.7e+01|1.6e+03|-4.000000e+01 0.000000e+00| 0:0:01|
>
> chol 1
>
> 1
> 1|0.997|0.944|1.1e-01|1.0e+00|5.2e+01|-3.284941e+01 -4.694775e+00| 0:0:02| chol 1 1
> 2|1.000|0.211|3.6e-15|8.3e-01|7.7e+01|-4.872019e+02 -5.629049e+00| 0:0:02| chol 1 1
> 3|1.000|0.021|1.5e-11|8.1e-01|2.2e+03|-8.619111e+05 -5.997421e+00| 0:0:02| chol 1 1
> 4|0.065|0.002|1.2e-07|8.1e-01|2.4e+06|-1.067261e+10 -7.134851e+00| 0:0:02| chol 1 1
> stop: primal infeas has deteriorated too much, 1.9e-04
> 5|0.000|0.000|1.2e-07|8.1e-01|2.4e+06|-1.067261e+10 -7.134851e+00| 0:0:02|
>
> prim_inf,dual_inf,relgap = 1.19e-07, 8.11e-01, 2.29e-04
> sqlp stop: dual problem is suspected of being infeasible
> -------------------------------------------------------------------
> number of iterations = 5
> residual of dual infeasibility
> certificate X = 9.37e-11
> reldist to infeas. <= 2.34e-11
> Total CPU time (secs) = 2.11
> CPU time per iteration = 0.42
> termination code = 2
> DIMACS: 1.2e-07 0.0e+00 1.3e+00 0.0e+00 -1.0e+00 2.3e-04
> -------------------------------------------------------------------
>
> ------------------------------------------------------------
> Status: Infeasible
> Optimal value (cvx_optval): +Inf

function [G,W,S] = TD_P_inv_Dual(P_exp,B,A_ax,B_by)

        d = size(P_exp,1); 

        m = size(P_exp,3); 

        cvx_begin

        variable S(d,d,m,m) 

        W_hat=zeros(d^2,d^2);

        for a=1:d

        for b=1:d

        for x=1:m

        for y=1:m

        W_hat=W_hat+B(a,b,x,y).*(kron(A_ax(:,:,x,a),B_by(:,:,y,b))); %this is the Bell operator

        end

        end

        end

        end

        W=sum(sum(sum(sum(B.*P_exp))));

        J=cvx(0);

        for a=1:d

        for b =1:d

        for x=1:m

        for y =1:m

        if a ~= b

        S(a,b,x,y) == sum(B(a,b,x,y));

        J=S(a,b,x,y).*W;

        end

        end

        end

        end

        end

        J=real(J)

        minimize(J)

        subject to

        for e=1:d

        (J-kron(squeeze(A_ax(:,:,1,e)),eye(d)))==semidefinite(d^2,d^2);

        end

        cvx_end

        G=double(S(a,b,x,y).*W);

Other than syntax differences, this advice applies to CVX as well.

actually my problem is that: I could not define S correctly I think I have an algorithmic problem.
Do you think that S is defind correctly?? Because when I use the T everything is normal. But the thing is that in T we sometimes have same a and b values so I used if a ~= b but didnt work for me

I have no idea what you’re trying to do, so I don’t know what is correct. You should start from a simple, easy to understand problem, and as you get that to work correctly, gradually build up complexity toward the problem you really want to solve.

I have no idea what you’re trying to do, so I don’t know what is correct. You should start from a simple, easy to understand problem, and as you get that to work correctly, gradually build up complexity toward the problem you really want to solve.

actually as I told before, I am trying to define s as a variable in cvx and I think this is a sipmle problem:
t
s

I can’t even read that.

Anyway, it seems you should be declaring the T as variables, and using assignments ( = ), not equality constraints ( == ), to build up S from T.

Anyway, it seems you should be declaring the T as variables, and using assignments ( = ), not equality constraints ( == ), to build up S from T .

Of course I tried it first.

    for a=1:d
     for b =1:d
         for x=1:m
             for y =1:m
                 if a ~= b
                    S(a,b,x,y) = sum(B(a,b,x,y));
                    %W2=sum(W*S(a,b,x,y));
                    J=S(a,b,x,y).*W;
                   % W=sum(W*S(a,b,x,y));
                   %W=W*S(a,b,x,y)
                 end
            end
        end
    end
end

But when I tried it I have this error:

Error using [cvx_end](matlab:matlab.internal.language.introspective.errorDocCallback(‘cvx_end’, ‘/MATLAB Drive/cvx-a64/cvx/commands/cvx_end.m’, 70)) ([line 70](matlab: opentoline(’/MATLAB Drive/cvx-a64/cvx/commands/cvx_end.m’,70,0)))

The following cvx variable(s) have been cleared or overwritten:

S

This is often an indication that an equality constraint was

written with one equals ‘=’ instead of two ‘==’. The model

must be rewritten before cvx can proceed.

Error in [TD_P_inv_Dual](matlab:matlab.internal.language.introspective.errorDocCallback(‘TD_P_inv_Dual’, ‘/MATLAB Drive/cvx-a64/QETLAB-0.9/TD_P_inv_Dual.m’, 68)) ([line 68](matlab: opentoline(’/MATLAB Drive/cvx-a64/QETLAB-0.9/TD_P_inv_Dual.m’,68,0)))

cvx_end

Don’t declare S as a variable. Do follow the guidance in http://cvxr.com/cvx/doc/basics.html#assignment-and-expression-holders .

1 Like

it worked thanks :slight_smile: