Values of Expression Does not Change the Objective

Hello
I am having trouble understanding the use of expression
This is what I had to do:
variable pg(10)
variable qg(10)
expression delta_p(10)
expression delta_q(10)
minimize (sum(Cs.*pg.pg) + 10000 sum(delta_p) + 1000 * sum(delta_q))

subject to
sum(pg) >= sum(p_demand)
for k = 1:10
  delta_p(k) = (pg(k) - p_demand(k) + ........)^2
  delta_q(k) = (qg(k) - q_demand(k) + ........)^2
end
sum(delta_p) <=0.05; 
sum(delta_q) <=0.05; 

So the problem solves, (I have not added/shown all the constraints) - but what I am seeing is, the cvx_optval only is sum(Cs.*pg.*pg), whatever value delta_p or delta_q gets does not change the optimal value. The weights also do not make any difference to the problem solution.
NB: I have to use expressions as the ... parts can not be written in a vector/matrix format.

Am I Doing something wrong?

The expression needs to be assigned before it is used. When delta_p and delta_q appear in the objective value, they have not yet been assigned, so they have the default (initial) value of zero in the objective function, i.e.,the same as the last two terms in the objective function not being there at all.

This is different than variables, for which it doesn’t make any difference whether constraints involving them appear before or after the objective.

So, then what should be the solution? If I define delta_p and delta_q to be variables, then the equations in the for loop, throws this error:

Disciplined convex programming error:
Invalid constraint: {real affine} == {convex}

In simple, I am calculating some square terms, and then summing those terms, I was hoping to hold those values in an expression, and then sum them up in the objective function…

Place the expression assignments in the code before the objective.

Alternatively, use sum_square without using expressions.