Please see my problem below: the problem has 51 variables when I assume the variable of number 51 (u(51))is given I get a solution.
When I assume the variable number 51 is not given and try to solve for all 51 variables, I get the following message “Disciplined convex programming error: Cannot perform the operation: {real affine} .* {convex}”

I guess it is b/c in db equation the Cap which is u(51) is multiplied by the other variables (u)

N=51
L = 0.5*tril(ones(50))
cvx_begin
variables u(N)
Cap=u(51)
E0=Cap/2
db = 1000*Cap*max(1/eta*u(1:50),eta*u(1:50));
Es = E0+Cap*L*u(1:50);
N_PV=100
minimize sum(Load+db'-N_PV*PV)/(2*1000)*rate+rate_max*max((Load+db'
N_PV*PV)/1000)
u(1:50) <= 0.4;
u(1:50) >= -1;
u(51)>=0
u(51)<=100
Es <= Cap;
Es >= 0;
cvx_end

Thank you. when I assume the variable number 51 is given the cvx will solve and I have seen some papers solving the problem assuming convex optimization. But when I assume the variable number 51 is not given, the problem will have local minimum which I believe that means it is not convex. I was wondering if there is a way to convert it to convex in that case. Also I have another question if you can help me. How can I use if statement inside the convex optimization?

The problem is not convex unless one of the “varaiables” being multiplied is actually fixed, rather than being an optimization variable.

As for if statements, if a CVX variable or expression would appear in the condition being evaluated for the if statement, that is not allowed in CVX, and you would need to handle that by Big M logical constraint modeling See https://or.stackexchange.com/search?q=Big+M+logical+constraints using CVX’s MIDCP capability.

If u is a CVX variable or expression, you need to apply Big M per the links at the link. if u is a numerically populated MATLAB variable, you can enter this using if else.

Thank you. Can I have your final word on this?
I added the following:
variables u(N)
variable b binary
M=10^12;% large number
rate=9b+1
u<=h+mb
u>=h-(1-b)M
but it say Cannot perform the operation: {convex} . {real affine}.
I guess my problem can not be solved here (cvx) since u and b are multiplied by each other: u is cvx optimization variable and b is the binary variable.

You should not choose M = 10^12. That may result in things working out incorrectly due to solver integrality tolerance. Instead, M should be chosen based on lower and upper bounds on 'u`.