Why will i get cvx_optval =NaN when want to maxmize objected function?

Why will i get cvx_optval =NaN when want to maxmize objected function?

Sometime i will get cvx_optval =-inf,but i know it just means that infeasible,but why will get cvx_optval =NaN rarely ?

tic
rw=1000;%8000
%rho
BD=[0 3 6 9 12]%bdb
N0dBc=[0 -3 -5 -7 -9]%nnn
for nnn=1%:5
    for bdb=1%1:5
        R_D=0.1;
        for ti=1:rw
            batteryA=450;
            batteryB=450;
            batteryC=450;
            stop_charge=450;
            B0_A=batteryA;
            B0_B=batteryB;
            B0_C=batteryC;
            H_AB =H_AB_store(1+(ti-1)*Br:Br*ti,1:At)
            H_AC =H_AC_store(1+(ti-1)*Cr:Cr*ti,1:At)
            H_BA =H_BA_store(1+(ti-1)*Ar:Ar*ti,1:Bt)
            H_CA =H_CA_store(1+(ti-1)*Ar:Ar*ti,1:Ct)
            H_A =H_A_store(1+(ti-1)*Ar:Ar*ti,1:At)
            H_B =H_B_store(1+(ti-1)*Br:Br*ti,1:Bt)
            H_C =H_C_store(1+(ti-1)*Cr:Cr*ti,1:Ct)
            
            dB=BD(bdb)
            %dB=3;
            gamma=10.^(dB./10);
            gamma_BC=gamma;
            gamma_A=gamma;
            N0dB=N0dBc(nnn);
            N0=10.^(N0dB./10);
            %design f_B
            f_B=finding_fB(H_BA,H_B,Bt)
            
            %design f_C
            f_C=finding_fC(H_CA,H_C,Ct)
            %design f_A & best_a
            [best_a,f_A,cn]=finding_fA_besta(gamma,N0,At,H_A,H_BA,H_AB,H_AC)
            [best_a2,f_A2]=finding_fA2_besta(gamma,N0,At,H_BA,H_AB,H_AC)
            [wBo,wCo]=finding_combiner(f_B,f_C,H_BA,H_CA);
            
            %cvx_begin
            g_BA=real(f_B'*H_BA'*H_BA*f_B);
            g_CA=real(f_C'*H_CA'*H_CA*f_C);
            g_BAw=real(f_B'*H_BA'*wCo*wCo'*H_BA*f_B);%B
            g_CAw=real(f_C'*H_CA'*wBo*wBo'*H_CA*f_C);%C
            
            g1_AC=real(f_A'*H_AC'*H_AC*f_A);
            g1_AB=real(f_A'*H_AB'*H_AB*f_A);
            g1_ACc=(real(f_A'*H_AC'*H_AC*f_A))^2;
            g1_ABc=(real(f_A'*H_AB'*H_AB*f_A))^2;
            g2_AC=real(f_A2'*H_AC'*H_AC*f_A2);
            g2_AB=real(f_A2'*H_AB'*H_AB*f_A2);
            g2_ACc=(real(f_A2'*H_AC'*H_AC*f_A2))^2;
            g2_ABc=(real(f_A2'*H_AB'*H_AB*f_A2))^2;
            g_A=real(f_A2'*H_A'*H_A*f_A2);
            
            cvx_begin
            variable xa
            variable x1bc
            variable x2bc
            variable Pda
            
            if g1_ACc < g1_ABc
                P1_A=(gamma_BC*x1bc*N0)/g1_ACc;
                P1_B=(xa*gamma_A*N0)/(g_BAw);
                P1_C=(xa*gamma_A*N0)/(g_CAw);
                H1_A=R_D*N0*gamma_A*(xa-1)*(   (real(g_BA)/real(g_BAw)) +  (real(g_CA)/real(g_CAw))    )
                H1_B=R_D*gamma_BC*N0*(g1_AB/g1_ACc)*( (g1_ABc*x1bc-g1_ACc)/g1_ABc );
                H1_C=R_D*N0*gamma_BC*(x1bc-1)*(g1_AC/g1_ACc);
                
            else
                P1_A=(gamma_BC*x1bc*N0)/g1_ABc;
                P1_B=(xa*gamma_A*N0)/(g_BAw);
                P1_C=(xa*gamma_A*N0)/(g_CAw);
                H1_A=R_D*N0*gamma_A*(xa-1)*(   (real(g_BA)/real(g_BAw)) +  (real(g_CA)/real(g_CAw))    )
                H1_B=R_D*N0*gamma_BC*(x1bc-1)*(g1_AB/g1_ABc);
                H1_C=R_D*gamma_BC*N0*(g1_AC/g1_ABc)*((g1_ACc*x1bc-g1_ABc)/g1_ACc);
                
            end
            
            if g2_ACc < g2_ABc
                P2_A=(gamma_BC*x2bc*N0)/g2_ACc;
                P2_B=Pda;
                P2_C=Pda;
                H2_A =R_D* ((P2_B*g_BA) + (P2_C*g_CA) + (P2_A*g_A));
                H2_B=R_D*gamma_BC*N0*(g2_AB/g2_ACc)*( (g2_ABc*x2bc-g2_ACc)/g2_ABc );
                H2_C=R_D*N0*gamma_BC*(x2bc-1)*(g2_AC/g2_ACc);
                
                
            else %g2_AC > g2_AB
                P2_A=(gamma_BC*x2bc*N0)/g2_ABc;
                P2_B=Pda;
                P2_C=Pda;
                H2_A = R_D*((P2_B*g_BA) + (P2_C*g_CA) + (P2_A*g_A) );
                H2_B=R_D*N0*gamma_BC*(x2bc-1)*(g2_AB/g2_ABc);
                H2_C=R_D*gamma_BC*N0*(g2_AC/g2_ABc)*((g2_ACc*x2bc-g2_ABc)/g2_ACc);
                
            end
            
            
            %A
            B1_A = B0_A-P1_A+H1_A;
            B2_A = B1_A-P2_A+H2_A;
            
            %B
            B1_B=B0_B-P1_B+H1_B;
            B2_B=B1_B-P2_B+H2_B;
            
            %C
            B1_C=B0_C-P1_C+H1_C;
            B2_C=B1_C-P2_C+H2_C;
            
            BL=B2_A+B2_B+B2_C;
            
            maximize BL
            
            subject to
            xa >=   1
            x1bc >=   1
            x2bc >=    1
            Pda > 0
            
            B0_A >= P1_A;
            B1_A >= P2_A;
            B2_A >= P1_A;
            stop_charge>=B1_A;
            stop_charge>=B2_A;
            
            B0_B >= P1_B;
            B1_B >= P2_B;
            B2_B >= P1_B;
            stop_charge>=B1_B;
            stop_charge>=B2_B;
            
            B0_C >= P1_C;
            B1_C >= P2_C;
            B2_C >= P1_C;
            stop_charge>=B1_C;
            stop_charge>=B2_C;
            
            %P2_A<P1_A;
            cvx_end
            resul_store(ti)=cvx_optval;
        end
        resul_store_noinf=~isinf(resul_store) ;
        resul_store_nonan =~isnan(resul_store) ;
        yesnumber=sum(resul_store_noinf & resul_store_nonan);
        out_prob=(rw-yesnumber)/rw;
    end
end

resul_store_nonan =

Columns 1 through 16

 1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1

Columns 17 through 32

 1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1

Columns 33 through 48

 1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1

Columns 49 through 64

 1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1

Columns 65 through 80

 1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1

Columns 81 through 96

 1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1

Columns 97 through 112

 1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1

Columns 113 through 128

 1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1

Columns 129 through 144

 1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1

Columns 145 through 160

 1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1

Columns 161 through 176

 1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1

Columns 177 through 192

 1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1

Columns 193 through 208

 1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1

Columns 209 through 224

 1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1

Columns 225 through 240

 1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1

Columns 241 through 256

 1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1

Columns 257 through 272

 1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1

Columns 273 through 288

 1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1

Columns 289 through 304

 1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1

Columns 305 through 320

 1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1

Columns 321 through 336

 1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1

Columns 337 through 352

 1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1

Columns 353 through 368

 1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1

Columns 369 through 384

 1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1

Columns 385 through 400

 1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1

Columns 401 through 416

 1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1

Columns 417 through 432

 1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1

Columns 433 through 448

 1     1     1     1     0     1     1     1     1     1     1     1     1     1     1     1

Columns 449 through 464

 1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1

Columns 465 through 480

 1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1

Columns 481 through 496

 1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1

Columns 497 through 512

 1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1

Columns 513 through 528

 1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1

Columns 529 through 544

 1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1

Columns 545 through 560

 1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1

Columns 561 through 576

 1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1

Columns 577 through 592

 1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1

Columns 593 through 608

 1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1

Columns 609 through 624

 1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1

Columns 625 through 640

 1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1

Columns 641 through 656

 1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1

Columns 657 through 672

 1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1

Columns 673 through 688

 1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1

Columns 689 through 704

 1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1

Columns 705 through 720

 1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1

Columns 721 through 736

 1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1

Columns 737 through 752

 1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1

Columns 753 through 768

 1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1

Columns 769 through 784

 1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1

Columns 785 through 800

 1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1

Columns 801 through 816

 1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1

Columns 817 through 832

 1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1

Columns 833 through 848

 1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1

Columns 849 through 864

 1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1

Columns 865 through 880

 1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1

Columns 881 through 896

 1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1

Columns 897 through 912

 1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1

Columns 913 through 928

 1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1

Columns 929 through 944

 1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1

Columns 945 through 960

 1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1

Columns 961 through 976

 1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1

Columns 977 through 992

 1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1

Columns 993 through 1000

 1     1     1     1     1     1     1     1

As you can see ,there is only one 0 in the column 437 of the resul_store_nonan,it means that there is only one time that cvx_optval=NaN

Show us an example with all the output.

I have edit the problem,and add my full code with the output ,by the way,
Ar=8
At=11
Br=2
Bt=5
Cr=2
Ct=5

You haven’t even provided all the input data, for instance, H_AB_store.

Please re-run your program, but this time, show us all the solver and CVX output for the instance in which cvx_optbval = NaN.

You can also try specifying a different solver and see whether the same thing happens.