According to the Wikipedia page on Lagrange multipliers under the section - Example 3: Entropy, it is written that:

$$f(p_1,p_2,\ldots,p_n) = -\sum_{j=1}^n p_j\log_2 p_j$$

For this to be a probability distribution the sum of the probabilities

$p_i$ at each point $x_i$ must equal 1, so our constraint is:

$$g(p_1,p_2,\ldots,p_n)=\sum_{j=1}^n p_j = 1\tag{1}$$

We use Lagrange multipliers to find the point of maximum entropy, $\vec{p}^{\,*}$ across all discrete probability distributions $\vec{p}$ on $$\{x_1,x_2, \ldots, x_n\}$$ We require that:

$$\left.\frac{\partial}{\partial \vec{p}}\Big(f+\lambda (g-1)\Big)\right|_{\vec{p}=\vec{p}^{\,*}}=0\tag{2}$$

My question is simple, why is $1$ is subtracted from the constraint $(1)$, in equation $(2)$?

Put another way, I think eqn. $(2)$ should be $$\left.\frac{\partial}{\partial \vec{p}}\Big(f+\lambda g\Big)\right|_{\vec{p}=\vec{p}^{\,*}}=0,$$

However, I know that equation $(2)$ is correct as I have a similar problem regarding entropy also. In the following expression, (eqn. $(5)$), I must maximize the entropy subject to the constraints $$\mathrm{Tr}\left[\hat \rho\right]=1\tag{3}$$ and the constraint that the expectation value of the Hamiltonian, $$\mathrm{Tr}\left[\hat \rho \hat H\right]\tag{4}$$ is $E$,

$$S'=-\mathrm{Tr}\left[\hat \rho \ln \hat \rho\right]+\lambda\left(\mathrm{Tr}\left[\hat \rho \hat H-E\right]\right)+\mu\left(\mathrm{Tr}\left[\hat \rho\right]-1\right)\tag{5}$$

I just cannot understand why $1$ is being subtracted in the third term and $E$ is subtracted in the second term. This just seems non-sensical as from eqn. $(3)$ $$\mathrm{Tr}\left[\hat \rho\right]-1=0$$ so it is like $0$ is being added in as a constraint which makes no sense whatsoever to me.

Can someone please explain the logic behind putting factors of zero into the Lagrangian?

Update in response to a comment:

Since this post is not being well received I will try to clarify further by showing a 'counter-example', then, hopefully, it will become more clear why I am getting confused:

On this page for the derivation of the Boltzmann distribution it is written (amongst other things) that,

To find the most-likely configuration, we maximize $\ln\Omega_n$ subject to constraints, $$\sum_j n_j=N,\quad \sum_j \epsilon_j n_j=U\tag{6}.$$ $\fbox{$\text{Where the statistical weight is defined as } $$\Omega_n=\frac{N!}{\prod_j n_j !}$}$

Using Lagrange's method of undetermined multipliers, $$\frac{\partial}{\partial n_j}\left[\ln \Omega_n-\alpha\sum_jn_j-\beta\sum_j\epsilon_jn_j\right]=0 \,\, \forall \, j\tag{7}$$

But according to @Connor Behan (in one of the comments below this question)

"$g-1$ is the constraint"

So, this logic implies that equation $(7)$ should actually be written as

$$\frac{\partial}{\partial n_j}\left[\ln \Omega_n-\alpha\color{red}{\left(\sum_jn_j-N\right)}-\beta\color{red}{\left(\sum_j\epsilon_jn_j-U\right)}\right]=0 \,\, \forall \, j\tag{8}$$

Which by virtue of the equations in $(6)$, the terms in the parentheses marked in red are zero. Now you may see what I meant when I asked "why do we put factors of zero in the Lagrangian"

To be pedantic, my reasoning for writing what I did in that last paragraph above is because I am simply subtracting $N$ and respectively $U$ from both sides of the respective equations in $(6)$. Hence my reason for suggesting the terms marked red in $(8)$ are zero.

So, either equation $(7)$ is correct or equation $(8)$ is correct. They can't both be correct so which one is it?

Just a side note, I know that the constant terms ($N$ and $U$) in eqn. $(8)$ will differentiate to zero anyway. But what I would like to know here is the reason for the inconsistency between equations $(7)$ and $(8)$.

  • 1
    $\begingroup$ $\partial / \partial \lambda$ should give the constraint. $\endgroup$ Mar 14, 2023 at 17:40
  • 4
    $\begingroup$ Nothing is being subtracted from the constrait... $g - 1$ is the constraint. $\endgroup$ Mar 14, 2023 at 19:17
  • 4
    $\begingroup$ en.wikipedia.org/wiki/Lagrange_multiplier#Single_constraint note that the contraint $g(x, y) = c$ will then enter as $\lambda (g - c)$ and setting the derivative wrt. $\lambda$ to 0 you recover the constraint that you want to enforce, however when taking derivative wrt. $x$ the $-\lambda c$ term drops out trivially, so sometimes people will neglect that $\endgroup$
    – Wihtedeka
    Mar 15, 2023 at 10:49
  • 2
    $\begingroup$ @ConnorBehan So you are saying that this derivation of the Boltzmann distribution is wrong as in eqn. $(7)$ they really do have (the equivalent of) $\lambda g$ in the Lagrangian (and not $\lambda(g-1)$)? $\endgroup$ Mar 15, 2023 at 12:22
  • 1
    $\begingroup$ @ConnorBehan Okay, so I am relying on finding consistency across literatures, but I have gone through the proof and I understand all of the proof (Boltzmann distribution derivation) except the part involving constraints. Answer the question please (in my last comment). $\endgroup$ Mar 15, 2023 at 22:58

4 Answers 4


I will expand a bit on why this Lagrangian is defined this way. If I want to maximize a function $f(p_1,\dots,p_n)$ over all its parameters I can set the gradient to zero. $$\nabla f(\vec p)=0$$ This will give me a single point $\vec p^* $ that maximizes $f$ (or minimizes, but let's assume maximize). Doing this naively for the entropy $f(\vec p)=-\sum_ip_i\log p_i$ gives $$p_i^*=\frac{1}e$$ The probability of every state is $1/e$, so this means that if I have many states, the probability will sum to be larger than 1! How can we enforce $\sum p_i=1$, while still maximizing the entropy? Lagrange developed a trick. You can always write a constraint as a function that you set to zero. For example, you can write $x^2+y^2=r^2$ as $R(x,y)=x^2+y^2-r^2$ and $R(x,y)=0$. The reason this is useful is that we can incorporate constraints automatically into our maximization procedure.

For example, we want to enforce the constraint $\sum p_i=1$. We can define a function $g(\vec p)=\sum_ip_i-1$ and construct our Lagrangian such that after maximizing we will have $g(\vec p)=0$. Our previous Lagrangian only depended on $\vec p$: $\mathcal L(\vec p)$. The trick is to add a dummy variable to our Lagrangian, for example $\lambda$: $\mathcal L(\vec p,\lambda)$. We then maximize with respect to $\vec p$, as well as to $\lambda$. By adding a clever term to our Lagrangian, we can enforce our constraints on the equations of motion. Like this:

$$\mathcal L(\vec p,\lambda)=-\sum_ip_i\log p_i+\lambda\left(\sum_ip_i-1\right)$$

The equations of motion then become \begin{align} \frac{\partial}{\partial p_i}\mathcal L=0&&\implies p_i^*=e^{\lambda^*-1}\tag {1}\\ \frac{\partial}{\partial \lambda}\mathcal L=0&&\implies \sum_i p_i^*=1\tag{2} \end{align} I used asterisks to make it extra clear that these are EOM. We can then plug (1) into (2) to get \begin{align} \sum_ip_i=\sum_ie^{\lambda-1}=ne^{\lambda-1}=1 \end{align} You can plug this back in (1) to finally get $p_i=\frac 1 n$.

We basically derived the microcanonical probability distribution. If you had used the additional constraint that $\sum_i\epsilon_ip_i=E$, you would get the canonical probability distribution, i.e. $p_i=\frac{1}{Z}e^{-\beta \epsilon_i}$. Note that in doing this we started out with $\beta$ as a "dummy" variable, which later turns out be very useful...


The discussion in the comments suggests that the following might help clarify the confusion.

Original problem The original maximization problem is this: Maximize $$\ln\Omega_n = - \sum_j n_i \ln \frac{n_j}{N}\tag{1}$$ subject to the constraints $$\sum_j n_j = N,\quad \sum_j E_j = U \tag{2}$$ The constraints impose a relationship between the $n_j$, therefore we cannot simply differentiate $\ln\Omega$ with respect to $n_j$ and set the result eqial to zero because $n_j$ are not independent.

Lagrange's reformulation Construct a new function in which the $n_j$ are independent. This function is $$ \mathcal L(\{n_j\}, \alpha, \beta) = \ln\Omega_n - \alpha\left(\sum_j n_j - N\right) - \beta\left(\sum_j E_j n_j - U\right) $$ This is a function of $n_j$, $\alpha$ and $\beta$, and all variables are independent. The solution then is $$\tag{3} \frac{\partial \mathcal L }{\partial n_j} = 0,\quad \frac{\partial \mathcal L }{\partial \alpha} = 0,\quad \frac{\partial \mathcal L }{\partial \beta} = 0 . $$ Since the $n_j$ are now independent, we can choose their values arbitrarily (in this problem they must be non negative numbers), therefore they are no longer required to obey the constraints.

Suppose we identify a set $(n^*_j, \alpha^*\neq 0, \beta^*\neq 0)$ that satisfies all equations in (3). Then $$\tag{4} \frac{\partial \mathcal L}{\partial \alpha^*} = 0 \Rightarrow \frac{\partial}{\partial\alpha^*}\alpha^*\left(\sum_j n^*_j - N\right) = 0 \Rightarrow \boxed{\sum_j n^*_j = N\vphantom{\sum^j}} $$ We conclude that the solution to (3) also satisfies all constraints.

In Sumamry Lagrange constructs a new function in which all variables are independent. This relaxes the requirement that the $n_j$ must obey the constraints. Even so, the solution that is obtained by minimizing this new function is guaranteed to satisfy these constraints.

  • $\begingroup$ Thank you very much for writing a separate answer. I am starting to understand what you mean now when you said in a comment below your first answer that you don't agree that $\sum_j n_j -N=0 \,\, \forall \, j$ as some of the $n_j$ may depend on other $n_j$. So what I should have said instead is that $\sum_j n_j -N=0$, which is true for some $j$ but not all $j$. You can't really fault this logic as all I have done is subtracted the constant number of particles, $N$ from both sides of the first eqn. in $(6)$ of my post. Do you agree with what I wrote in this comment? Many thanks. $\endgroup$ Mar 17, 2023 at 5:32
  • 1
    $\begingroup$ @N.Ginlabs I don't fault the logic, I am glad you pressed until your misconception was resolved. $\endgroup$
    – Themis
    Mar 17, 2023 at 11:27

The problem which motivates the method of Lagrange multipliers is constrained optimization. We have a function $f$ of several variables that we would like to optimize subject to a holonomic constraint between those variables, which is a constraint which can be put in the form $g(x,y,\ldots) = 0$ for some function $g$.

For example, if we have two variables $x,y$ and wish to maximize the function $f(x,y)= x+y$ subject to the constraint that $x$ and $y$ lie on the unit circle, then our constraint function is $g(x,y)=x^2+y^2-1$. When $g(x,y)=0$, then our constraint is satisfied. Notice, however, that $g(x,y)\neq 0$ in general; it is only for special values of the variables that the constraints are satisfied. This is ultimately the source of your troubles.

Now, the reason constrained optimization is generally difficult is that if we enforce the constraint, then $x$ and $y$ may no longer vary independently of one another. In the unconstrained case, we would be looking at the partial derivatives of $f$ and checking to see the places where they vanish - however, remember that $$\frac{\partial f}{\partial x} = \lim_{\epsilon\rightarrow 0} \frac{f(x+\epsilon,y)-f(x,y)}{\epsilon}$$

The problem is that if the point $(x,y)$ obeys the constraint, the point $(x+\epsilon,y)$ generally does not. Ordinary partial derivatives require $x$ and $y$ to vary independently in their definitions, and so they inherently disregard any constraints that we may wish to apply. It's far from obvious how we should fix this in the general case.

The method of Lagrange multipliers solves this problem in the following way. Rather than trying to optimize $f(x,y)$ subject to the constraint that $g(x,y)=0$, we instead optimize a new function $L(x,y,\lambda) = f(x,y)-\lambda g(x,y)$ subject to no constraints at all. That is, we find the partial derivatives of $L$ in the usual way (because we need not worry about any constraints) and set them to zero to find the points $(x^*,y^*,\lambda^*)$ which optimize $L$; from there, the points $(x^*,y^*)$ will optimize $f$ subject to the desired constraint.

There are a number of ways to see why this approach works - some geometric and graphical, and others analytic. I'll take the latter approach here. Observe that if $g(x,y)=0$, then we must have that $$\mathrm dg = \frac{\partial g}{\partial x} \mathrm dx + \frac{\partial g}{\partial y} \mathrm dy = 0$$ This reflects the fact that $x$ and $y$ may not change independently; if $x$ changes by an infinitesimal amount $\mathrm dx$, then $y$ must change by $$\mathrm dy = -\frac{\partial g/\partial x}{\partial g/\partial y} \mathrm dx$$ in order to keep the constraint satisfied.

If $f(x,y)$ is maximized or minimized subject to the constraint, then any infinitesimal change in $x$ and $y$ subject to the constraint will yield no first-order change in $f$. That is, to first order we have $$f\big(x+\mathrm dx,y+\mathrm dy) = f(x,y)$$ $$\implies \frac{\partial f}{\partial x} \mathrm dx - \frac{\partial f}{\partial y} \frac{\partial g/\partial x}{\partial g/\partial y} \mathrm dx = 0$$ $$\implies \frac{\partial f}{\partial x} \frac{\partial g}{\partial y} = \frac{\partial f}{\partial y} \frac{\partial g}{\partial x}$$

But now apply the Lagrange procedure. Taking the partial derivatives of $L$ and setting them to zero yields $$\frac{\partial L}{\partial x} = \frac{\partial f}{\partial x} + \lambda \frac{\partial g}{\partial x} = 0$$ $$\frac{\partial L}{\partial y} = \frac{\partial f}{\partial y} + \lambda \frac{\partial g}{\partial y} = 0$$ $$\frac{\partial L}{\partial \lambda} = g(x,y)=0$$

The third equation enforces the constraint. If we solve the first equation for $\lambda$ and then plug it in to the second, we find $$\lambda = -\frac{\partial f/\partial x}{\partial g/\partial x}$$ $$\implies \frac{\partial f}{\partial y} - \frac{\partial f/\partial x}{\partial g/\partial x} \frac{\partial g}{\partial y} = 0 \implies \frac{\partial f}{\partial y}\frac{\partial g}{\partial x} = \frac{\partial f}{\partial x} \frac{\partial g}{\partial y}$$

which is precisely the expression we found above.

Which by virtue of the equations in (6), the terms in the parentheses marked in red are zero. Now you may see what I meant when I asked "why do we put factors of zero in the Lagrangian"

The equations marked in red are analogous to the function $g(x,y)$ in my explanation. They are not zero in general - remember that we are maximizing and minimizing the Lagrangian subject to no constraints!

In other words, rather than optimize the entropy subject to the constraints in red, we define the Lagrangian and optimize it with no constraints at all. The expressions in red are generally nonzero, but when we follow the standard optimization procedure we will find that they will vanish at the end - but in the process, we will obtain specific values for the multipliers $\alpha$ and $\beta$, which we obviously can't do if we take our constraints to be satisfied from the outset (since the terms with $\alpha$ and $\beta$ will simply vanish immediately).

So, either equation (7) is correct or equation (8) is correct. They can't both be correct so which one is it?

Equation (8) is correct, equation (7) is not. If you take the partial derivative of (7) with respect to $\alpha$ and $\beta$ and set them to zero as per the Lagrange multiplier procedure, then you will find that the total particle number and total energy of the system are zero, which is obviously not correct. Doing the same with (8) yields the correct results.

With that being said, taking the derivatives of (7) and (8) with respect to the $n_i$'s will yield exactly the same results. Explicitly, one finds that $$\frac{\partial}{\partial n_i} \ln(\Omega) - \alpha - \beta \epsilon_i = 0 \implies n_i = \exp\big[-(\alpha + \beta \epsilon_i)n_i\big]$$ where we've used that $\frac{\partial}{\partial n_i}\ln(\Omega) = -\frac{\partial}{\partial n_i} \ln(n_i!) \approx - \ln(n_i)$. At this stage, we have no idea what $\alpha$ and $\beta$ are, so we must turn to our remaining constraint equations: $$\frac{\partial}{\partial \alpha} \left[\ln(\Omega) -\alpha\left(\sum_i n_i - N\right) - \beta\left(\sum_i n_i \epsilon_i - U\right) \right] = -\left(\sum_i n_i - N\right) = 0$$ $$\frac{\partial}{\partial \beta} \left[\ln(\Omega) -\alpha\left(\sum_i n_i - N\right) - \beta\left(\sum_i n_i \epsilon_i - U\right) \right] = -\left(\sum_i n_i\epsilon_i - U\right) = 0$$

These last two equations give the "normalization" conditions which implicitly determine $\alpha$ and $\beta$.

The Wikipedia article leaves out $N$ and $U$ for reasons I don't understand, which yields the wrong normalization equations but still gives the correct results for the $n_i$'s. If you take the latter and then simply apply the constraints by hand, you get the right results, but it's strictly speaking not in line with the formal Lagrange multiplier procedure outlined above.


The question is:

So, this logic implies that equation $\mathrm(7)$ should be written as $$\frac{\partial}{\partial n_j}\left[\ln \Omega_n-\alpha\color{red}{\left(\sum_jn_j-N\right)}-\beta\color{red}{\left(\sum_j\epsilon_jn_j-U\right)}\right]=0 \,\, \forall \, j\tag{8}$$ Which by virtue of the equations in $(6)$, the terms in the parentheses marked in red are zero. Now you may see what I meant when I asked "why do we put factors of zero in the Lagrangian"

We are not putting factors of zero in the Lagrangian because these factors are zero only for the special distribution that maximizes the entropy functional. Indeed, the whole point is to identify the special distribution that makes those factors zero while maximizing entropy.

From a practical standpoint, $N$ and $U$ will drop out during maximization so we "could" define a "simplified" Lagrangian $$ \mathcal L' = \ln \Omega_n -\alpha \sum_jn_j -\beta\sum_j\epsilon_jn_j $$ that is maximized by the same distribution, but I don't recommend it.

  • 2
    $\begingroup$ "We are not putting factors of zero in the Lagrangian [....]". From eqn. $(6)$ in my post does it seem that $\sum_j n_j-N \ne 0$? I'm sorry, I really want to understand what you are saying in your answer but unless $\alpha(\sum_j n_j-N)\ne 0$ then we really are putting factors of zero into the Lagrangian. Thank you for taking the time to write an answer. $\endgroup$ Mar 15, 2023 at 22:46
  • 2
    $\begingroup$ @N.Ginlabs The Lagrangian doesn't yet know that $\sum_jn_j-N$ should be zero. Minimizing a Lagrangian gives you equations of motion (EOM). The Lagrangian is a compressed (compact) way of writing your EOM. It doesn't make sense to put the EOM back into your Lagrangian. $\endgroup$ Mar 15, 2023 at 23:51
  • 3
    $\begingroup$ @N.Ginlabs A stupid example: $\mathcal L(a,b)=\tfrac 1 2 a^2+b^2$. The EOM of this Lagrangian are $a=0$ and $b=0$. You are not supposed to plug those EOM back into the Lagrangian, otherwise you get $\mathcal L=0$. The Lagrangian is just a function. $\endgroup$ Mar 15, 2023 at 23:54
  • 2
    $\begingroup$ @AccidentalTaylorExpansion Okay, lets just move aside the Lagrangian part for the moment and focus on the first of the equations in $(6)$, namely, $\sum_j n_j=N$. Do you agree with me when I write that $\sum_j \left(n_j \right)-N=0$? Thanks for your comment. $\endgroup$ Mar 15, 2023 at 23:55
  • 1
    $\begingroup$ @Themis Yes, so the Lagrangian is a function of $n_j$, but putting the Lagrangian to one side for a moment, we still have that $\sum_j \left(n_j \right)-N=0 \,\, \forall \, j$, do you agree with this? I am not talking about putting that equation in the Lagrangian, for now I would just like confirmation that (the first of eqn. $(6)$) is indeed zero (when rearranged). Once I have an answer to this I can move on. But I have asked this very basic question several times to different users and no one answers. $\endgroup$ Mar 16, 2023 at 17:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.