Ideal Gas and the Maxwell-Boltzmann Distribution
After reading this you can predict the speed distribution of a two-dimensional gas from its temperature, check the pressure against the ideal-gas law, and explain why the histogram settles into the same smooth curve no matter how you start it.
What the simulation actually does
The box holds a few hundred hard discs. Each disc flies in a straight line until it hits a wall or another disc, then bounces off elastically. Elastic means no energy is lost: the total kinetic energy of the gas stays fixed, and momentum is conserved in every collision. That is the entire physics. There is no attraction between molecules, no spin, no internal structure. This is the simplest model that still behaves like a gas.
Here is the hook. Start every disc at exactly the same speed, say 300 in the simulation's units, all pointing in random directions. The speed histogram is a single spike. Let the collisions run for a few hundred impacts and the spike spreads out. A handful of discs end up moving at twice the starting speed, a handful barely move, and most sit in a broad middle. The histogram stops changing shape and matches a specific curve, the Maxwell-Boltzmann distribution. Nothing forces this shape. It emerges from random elastic collisions alone.
When this model helps and when it lies
Use the ideal-gas model when the molecules are far apart compared to their own size and interact only briefly. Air at room temperature and normal pressure fits well: the ideal-gas law PV = N k_B T predicts its pressure to within about 0.1 percent. Helium and neon fit even better because they barely attract each other.
The model breaks down in three situations. Near condensation, where molecules stick together into a liquid, the missing attraction matters and real pressure drops below the ideal prediction. At very high density, where the discs' own area is a large fraction of the box, the finite size matters and real pressure rises above the ideal prediction. And it is two-dimensional here, so the numeric prefactors differ from three-dimensional gases. If you want to see attraction and phase changes, the Molecular Dynamics: Phases of Matter simulation adds a Lennard-Jones force and cools a gas down into a crystal.
This is a 2D gas, so each molecule has two translational degrees of freedom, not three. That changes the speed distribution's exact form and the relation between temperature and mean square speed. The formulas below are the correct 2D versions, so do not compare them term for term with a 3D chemistry textbook.
The distribution and where it comes from
In two dimensions the probability that a molecule has speed v follows the Maxwell-Boltzmann speed distribution:
Here m is the molecule mass, v is speed, T is temperature, and k_B is Boltzmann's constant. The factor e^{-m v^2 / 2 k_B T} says fast molecules are exponentially rare because they carry more energy. The factor v in front says slow molecules are rare too, because there are few ways to move slowly in two dimensions. The product peaks somewhere in the middle.
The peak sits at the most probable speed:
The mean speed is slightly larger, and the root-mean-square speed larger still. In 2D:
Temperature enters only through the combination k_B T / m. Double the temperature and every speed scales up by \sqrt{2} \approx 1.414. That is the same as the footnote on the tool page: temperature is average kinetic energy, and heating the gas literally speeds the molecules up.
A worked example using the demo settings
The demo button starts every molecule at the same speed. Work in the simulation's natural units where m = 1 and the starting speed is 300. Because all molecules start at speed 300, the initial mean square speed is \langle v^2 \rangle = 300^2 = 90000.
From a single spike to the smooth curve
- Fix the temperature from the starting energy. In 2D, \langle v^2 \rangle = 2 k_B T / m, so k_B T / m = \langle v^2 \rangle / 2 = 45000. Collisions conserve total energy, so this value never changes.
- Predict the most probable speed: v_p = \sqrt{45000} \approx 212.
- Predict the mean speed: \langle v \rangle = \sqrt{\pi \cdot 45000 / 2} = \sqrt{70690} \approx 266.
- Predict the rms speed: v_{rms} = \sqrt{2 \cdot 45000} = \sqrt{90000} = 300. This equals the starting speed exactly, as it must, because energy is conserved.
- Run the simulation. The histogram spreads from the spike at 300 and settles with its peak near 212 and its mean near 266, matching the overlaid curve.
Notice the ordering: v_p \lt \langle v \rangle \lt v_{rms}, or 212 < 266 < 300. The distribution is skewed toward high speeds, so the average sits above the peak.
Pressure and the ideal-gas law
The simulation measures pressure by counting momentum delivered to the walls. Every time a disc of mass m hits a wall and bounces back, it hands the wall a momentum kick of 2 m v_x, where v_x is the speed component toward the wall. Sum those kicks over a time window and divide by the window length and the wall length. That is force per unit length, the 2D version of pressure.
Average over many impacts and this mechanical count reproduces the ideal-gas law exactly:
Here P is pressure, V is the box area, N is the number of molecules, and T is temperature. The law is not assumed by the code. It falls out of adding up individual bounces. That is worth sitting with: a macroscopic gas law appears from nothing but discs and walls.
To test the law, change one variable and hold the rest fixed. Double N at fixed V and T, and the measured pressure should double. Halve the box area at fixed N and T, and pressure should double again. The measured value fluctuates, so average over a few seconds before reading it.
| Change | Factor applied | Predicted pressure change |
|---|---|---|
| More molecules N | ×2 | ×2 |
| Hotter gas T | ×2 | ×2 |
| Larger box V | ×2 | ×0.5 |
| Smaller box V | ×0.5 | ×2 |
Reading the histogram and the pressure readout
The live histogram bins molecule speeds and draws the theoretical curve on top. Two things tell you the gas has reached equilibrium. First, the histogram's shape stops drifting: bars jiggle by a few percent from random sampling but the outline holds. Second, the outline matches the smooth curve, not the starting spike.
Expect noise. With 200 molecules and 20 bins, each bin holds about 10 molecules on average, so a bin can easily be off by \sqrt{10} \approx 3.2 molecules, roughly 30 percent. The tallest bins are steadier because they hold more counts. Do not read meaning into a single tall bar. The pressure readout fluctuates for the same reason: it is an average of discrete impacts, and the fewer impacts per window, the noisier the number.
Common mistakes
The most probable speed, the mean speed, and the rms speed are three different numbers. For the demo they are 212, 266, and 300. If you compare the histogram peak to a rms formula, or the average to the peak formula, everything looks broken when it is fine.
A second mistake is reading the pressure or histogram before equilibrium. Right after you press start, or right after you change the temperature, the numbers are still relaxing. Wait until the histogram shape holds steady.
A third mistake is over-packing the box. If the discs cover a large fraction of the area, their own size stops being negligible and the measured pressure runs high against PV = N k_B T. That is a real effect, the same one that makes dense gases deviate from ideal behavior, not a bug. Keep the packing loose to see clean ideal-gas numbers.
Related simulations
The Maxwell-Boltzmann shape here comes from a system relaxing to equilibrium. Several other simulations on this site show emergence from simple local rules. The Ising Model Ferromagnet shows a different equilibrium, where spins order or disorder as temperature crosses a critical point. Percolation shows a sharp tipping point in a random grid. For self-organizing chemistry, see the Reaction-Diffusion Patterns and the Belousov-Zhabotinsky Reaction. For growth from wandering particles, try the Crystal Growth Simulator and the Flame Simulator.
Frequently asked questions
Why do the speeds spread out if collisions conserve energy?
Total energy is fixed, but collisions move energy between molecules. A fast disc hitting a slow one can hand over most of its speed. Over many collisions, energy shuffles into the single arrangement that has the most ways of happening, which is the Maxwell-Boltzmann distribution. The total stays constant; only the sharing changes.
Why does the histogram look different from my chemistry textbook?
Most textbooks show the three-dimensional distribution, which has a v^2 factor out front. This simulation is two-dimensional, so it has a single v factor. The 2D peak sits at \sqrt{k_B T / m}, not the 3D value \sqrt{2 k_B T / m}.
What temperature does the simulation use?
Temperature is set by the average kinetic energy, through \tfrac{1}{2} m \langle v^2 \rangle = k_B T in 2D. In the tool's units you effectively set it through the starting speed. There is no need for real Kelvin values; the physics depends only on the ratio k_B T / m.
Why does the measured pressure keep fluctuating?
Pressure is counted from individual wall impacts over a short window. With a finite number of impacts, the count wobbles by roughly 1/\sqrt{n} where n is impacts per window. More molecules and longer averaging windows give a steadier reading.
Does the starting arrangement matter?
Not for the final histogram. Start all molecules at one speed, or give them random speeds, or bunch them in a corner. After enough collisions the speed distribution relaxes to the same Maxwell-Boltzmann curve set by the total energy. That independence from initial conditions is the meaning of thermal equilibrium.