Derivation of ensemble distribution 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$.
 A: For a system in the grand-canonical ensemble, the symbols $E$ and $N$ are not fixed values of the energy and particle number respectively.  They are specified values for the ensemble averages of these quantities.  Explicitly:
\begin{align}
  E = \langle \hat H\rangle, \qquad N = \langle \hat N\rangle,
\end{align}
where angled brackets denote ensemble averages, and $\hat H$ and $\hat N$ are the hamiltonian and number operator of the system respectively.  Even more explicitly, in the density matrix formalism, the ensemble average of an observable $\hat O$ given a state $\hat \rho$ of the system is defined as
\begin{align}
  \langle \hat O\rangle = \mathrm{tr}(\hat \rho\hat O).
\end{align}
In the thermodynamic limit, namely in the limit of a large number of constituent subsystems, the distribution of energy and particle number will generically be extremely sharply peaked around their ensemble averages, and in that case, there essentially is a fixed amount of energy or a fixed number of particles in the system, so this often leads to abuse of notation and language, but the abuse isn't so bad since the distributions are indeed extremely sharp.
A: E is the total energy. The energy $E_i$ is the energy of the $i$-th ensemble. The quantity $p_i E_i$ is like $\langle E_i\rangle$, which is the average energy of the $i$-th ensemble. The same sort of argument is true for the particle number. The total energy E across all the sub-systems has to be fixed and some finite-value. The same is true for the particle number and hence, the constraints for $E$ and $N$. 
