 We have worked out the number of ways to distribute n-balls among any number of boxes, with a1 balls in the first box, a2 in the second, and so on. This number is gamma equals n factorial over a1 factorial times a2 factorial, etc. Now, we want to apply this result to count the atomic states of an ideal monatomic gas. Assume the atoms are confined to a box with side lengths Lx, Ly, and Lz. An atom can have any position within the box specified by coordinates x, y, and z. In addition to position, an atomic state is specified by three components of momentum, px, py, and pz. We want to count how many atoms have a particular position and a particular momentum. For this purpose, we employ the concept of the system's so-called phase space. We form a two-dimensional coordinate system with coordinates x and px. Then, we overlay a rectangular grid with spacing delta x and delta px. We denote the, quote, area of a single rectangle by h. This is not a standard area, but has units of momentum times length, which we call action. Kilograms meters squared per second, which is equivalent to joule seconds. We form similar systems for the y and z coordinates. This allows us to specify the dynamical state of an atom by plotting a point on each of these three plots. Each point shows the value of one spatial coordinate and the value of the associated momentum. So at a given time, each atom is in a single six-dimensional cell of elementary volume h cubed and with kinetic energy px squared plus py squared plus pz squared over 2m. If the particle moves freely with constant momentum, these three points will move at a constant rate to the right or left, depending on the algebraic sign and magnitude of the corresponding component of momentum. If the particle suffers a collision, then one or more momentum values will undergo a sudden change. Let's look at a two-dimensional simulation of 16 balls in a box. Initially, they are uniformly spread out and have random velocities. One of the balls is colored red and its position in phase space is shown in the middle and right panels. When the red ball suffers a collision, one or both of its momentum values suddenly change. In between collisions, the phase space points drift to the right or left, depending on the corresponding momentum value. If we plot the phase space trajectories for all 16 balls, the plots get quite busy. Nonetheless, at any instant, these plots represent the complete dynamical state of the system. Now, let's consider how, in principle, we could collect phase space statistics. Suppose we track a single atom, the red ball in our simulation. We are interested in the probability it will fall into the highlighted phase space cell. Assume this is the ith cell in our bookkeeping scheme. We take snapshots of phase space at time intervals that are long enough that we can consider the atoms to have been randomly reshuffled. And we keep track of the number of times the tracked atom falls in the ith cell. There's a hit. For these nine snapshots, the ith cell was occupied one time. Therefore, the occupation frequency of the ith cell was fi equals 1 over 9. As we increase the number of snapshots, fi will change. But as the number of snapshots grows arbitrarily large, fi should approach a constant value. This will represent the probability that an atom would be found in the ith cell in a randomly selected snapshot. In theory, of course, for each snapshot, we could track all n atoms and keep statistics for every phase space cell. In any case, the need to collect statistics leads us to consider the idea of a statistical ensemble. We want to gather enough statistics that the resulting occupation frequencies are good estimates of the corresponding occupation probabilities. We can take r snapshots of our n gas atoms at different times with r as large as needed to get good statistics. Alternately, we can imagine taking a single snapshot of r independent gas samples each with the same n, v, and t values. In any case, if cell i is occupied ai times, then the occupation frequency is fi equals ai over rn. rn, because each of the r snapshots or samples has n atoms. Now we apply our balls and boxes theory to find the number of ways to get specific values a1, a2, etc. Gamma equals rn factorial over a1 factorial times a2 factorial and so on for every phase space cell. Since f1 equals a1 over rn, we can replace a1 by f1, rn, a2 by f2, rn, etc. Now we need to dig into quite a bit of math. At the end, this will lead to two important equations that will be central to the rest of our analysis. We will work with the natural logarithm of gamma. Reason one, entropy is proportional to this. Reason two, logs convert quotients to differences and products to sums which makes analysis much easier. We have log gamma equals log rn factorial minus sum over i log fi rn factorial. We take r large enough that for every log of a factorial in our expression, log a factorial equals a log a minus a is a good approximation. For reference, for a at least 90, this gives an error of less than 1%. With this approximation, log gamma equals rn log rn minus rn minus sum over i fi rn log fi rn minus fi rn. The last term is minus minus, which is plus, sum over i fi rn. This equals rn times sum over i fi, but this last sum is just 1. 100% of the time, and Adam is in one of the cells. So the original sum is just rn. This plus rn cancels the minus rn in our expression. This leaves us with log gamma equals rn log rn minus sum over i fi rn log fi rn. Here's that expression again. Let's work on the sum. We can factor out rn. The log can be written as log fi plus log rn. This breaks our sum into two terms, rn times sum over i fi log fi plus rn log rn sum over i fi. Again, the last sum is 1, so the second term is simply rn log rn. This last term cancels the first term in the top expression. Finally, we are left with log gamma equals minus rn sum over i fi log fi. We want to find the fi values that maximize this expression. This will be the most probable macrostate of the system. From our previous analysis, we expect that the system will not deviate significantly from this most probable state, and that the log of gamma for these optimal fi values will define the entropy of this system. We can drop the constant factor rn and use the fact that maximizing an expression is equivalent to minimizing its negative. So we seek to minimize sum over i fi log fi. We need to do this subject to two constraints. First, the sum of occupation frequencies over all cells has to be 1. So sum over i fi must be 1. Second, the average energy of an atom needs to be 3 halves kt, and the average energy is sum over i fi epsilon i. The probability an atom is in the ith cell times the energy of the ith cell. We do this using the method of Lagrange multipliers. We form an augmented function, which is the original function plus a new term for each constraint. Each of these terms is an unknown multiplier times the constraint expressed as a quantity that must be 0. Then, an unconstrained minimization of this function with respect to the fi values and the two multipliers solves our original problem. Our augmented function is sum over i fi log fi plus alpha times sum over i fi minus 1 plus beta times sum over i fi epsilon i minus 3 halves kt. To see why this method works, call our augmented function big f. For this to be a minimum with respect to alpha, f must not change if we change alpha by an infinitesimal amount. Otherwise, we could follow this change to a smaller value of f. Formally, we write the partial derivative of f with respect to alpha equals 0. But this derivative is simply the constraint sum over i fi minus 1 equals 0. Likewise, for the beta term. So, minimizing f with respect to alpha and beta enforces our constraints. With the constraints enforced, the alpha and beta terms are 0. So, the minimum of our augmented function with respect to fi is simply the minimum of our original function sum over i fi log fi subject to the constraints.