I heard that you can derive the canonical ensemble by maximizing
$L = \sum_i p_ilog( p_i ) + \alpha (\sum_i p_iE_i-E)$
or for the grand-canonical ensemble
$L = \sum_i p_ilog( p_i ) + \alpha (\sum_i p_iE_i-E) +\beta (\sum_i p_iN_i-N) ,$
where $\alpha,\beta$ are Lagrange multipliers. I mean, I see that the first term is just the entropy, but I don't understand the conditions here. THe point in the canonical ensemble should be that the energy is not constant, so why do we want to have $(\sum_i p_iE_i-E)=0$ and what does this $E$ represent?-Similarly, for the particle number in the grand canonical ensemble, it is not clear to me, why we want to have $(\sum_i p_iN_i-N)=0$.