Dear all please have a look at constraint to buy sell power from to grid but I want either buy or sell at a time how to manage this constraint
cvx_clear
%input data
alpha1 = 2; beta1 = 3;
alpha2 = 4; beta2 = 5;
alpha3 = 6; beta3 = 7;
D = [5 3 7 2 12 20 6 9 5 7 11 12 11 10 5 6 7 4 9 7 9 10 7 5];
u = [1 1 2 2 3 33 4 4 5 5 2 100 1 7 7 5 5 3 3 0 0 1 1 8];
T = 24; d = 3;
cvx_begin
variables p(T,d) pg_buy(T,1) pg_sell(T,1);

minimize(sum(alpha1*p(:,1).^2 + beta1*p(:,1) + alpha2*p(:,2).^2 + beta2*p(:,2) + alpha3*p(:,3).^2 + beta3*p(:,3) +...
u'.*pg_buy - u'.*pg_sell));
subject to
sum(p,2)+ pg_buy == D' + pg_sell;
0 <= p(:,1) <= 4;
0 <= p(:,2) <= 6;
0 <= p(:,3) <= 9;
0 <= pg_buy <= 100; %constraint to buy power from grid
0 <= pg_sell <= 100; % constraint to sell power to grid
norms(p(2:T, : ) - p(1:T-1,:),Inf,1) <= [3 5 8];
cvx_end

This is not a forum for general modeling assistance. If you don’t yet know how to write your mathematical model using standard mathematical notation (on paper, say, or in LaTeX), then you’re not ready to apply CVX to it.

sir micheal mathematically I know but I don’t know to apply that constraint in CVX, I dont want buying and selling power at the same time.
mathematically this constraint is like that.
1pg_buy() + pg_sell(t) <= 1

In the future, you’d be better served asking for help on this kind of question at https://www.or-exchange.org// . There are many integer programming and Big M whizzes there.

Use a Big M approach. Add the following to your program.

As you can see, if b(t) ==1, pg_sell(t) must be <= 0. hence equal to 0. If b(t) ==0, pg_buy(t) must be <= 0, hence equal to 0. Therefore, at least one of pg_buy(t) and pg_sell(t) must be 0, and if either of them is non-zero, they are still allowed to go up to 100.

thanks Mark sir but yesterday I solved this problem in same way by taking help from your old post regarding big M. I am really thankful to you for your reply.