# How do i write a if loop in the cvx?

The variable are` x1bc`,` x2bc` and `xa` only,and the other are known vector or scalar

``````%transmitted power in the first phase
P1_A=(gamma_BC*x1bc*N0)/g1_ACc;
P1_B=(xa*gamma_A*N0)/(g_BAw);
P1_C=(xa*gamma_A*N0)/(g_CAw);

%harvested power in the first phase
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);

%transmitted power in the second phase
P2_A=(gamma_BC*x2bc*N0)/g2_ACc;
P2_B=Pda;
P2_C=Pda;

%harvested power in the second phase
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);

B1_A = B0_A-P1_A+H1_A;%The remaining power after first phase ends
B2_A = B1_A-P2_A+H2_A;%The remaining power after second phase ends

%Constraint
B0_A >= P1_A;
B1_A >= P2_A;

stop_charge>=B1_A;
stop_charge>=B2_A;
``````

Now,Assume that `B0_A= stop_charge=450`.i have a trouble,sometimes that H1_A>P1_A,and it will cause that stop_charge<B1_A ,so the cvx will show infeasible to me.we call that this situation is that the battery is overcharged,and that i have already add a constraint that `stop_charge>=B1_A`;

So i want to write a code to avoid the overcharged happen,so i write that

``````if H1_A > B0_A-P1_A
rH1_A=B0_A-P1_A
end
B1_A = B0_A-P1_A+rH1_A;
``````

However,cvx told me that

``````The following error occurred converting from cvxcnst to logical:
Disciplined convex programming error:
Constraints may not appear in if/then statements.
``````

How do i write the if -loop in other way?

You are trying to implement a logical constraint. You will have to introduce binary variables (using CVX’s MIDCP capability) and do some type of Big M modeling to handle them.

Search this forum for Big M and you should see some posts with links and guidance.