# Maxwell-Boltzmann distribution for the simulation of a perfect gas

• greg_rack
In summary: Isn't that a high number?It is a high number, but it's not too high to apply the law of large numbers.
greg_rack
Gold Member
I'm currently programming lots of physics simulators in DHTML, and my next step would be simulating a perfect gas behavior at a molecular point of view, bouncing inside of an HTML canvas.
For this reason, I came across "Maxwell-Boltzmann's law of distribution"(for speed, which I only studied last year without going deep into the function at all) to have the right molecules speed distribution depending on temperature(which on the simulator will be changing thanks to a slider, to see the consequent speeds variation), molecular mass and number of total molecules(which will be fixed instead).
I surfed a lot on the internet in these past few days trying to come up with a solution, but what I found are only loads of different equations and informations, that only ended up confusing me a lot more than they should.
Jumping to conclusions, I would be extremely grateful if you guys could explain me what this law's all about, and how to effectively simulate a perfect gas behavior.
Greg.

The Maxwell velocity distribution practically says that for a system of gaseous substance in thermal equilibrium, there's some most likely speed ##v_p## that you can expect a randomly picked gas molecule to have, and speeds greater than that become exponentially less likely. E.g. finding a gas molecule with speed greater than ##3v_p## is already quite improbable, and you almost never run across one with speed greater than ##10v_p##. The ##v_p## gets larger with increasing temperature.

The Maxwell-Boltzmann distribution is proved by probability arguments (law of large numbers and so on) and doesn't assume much about the forces that act between different gas molecules. The problem with an ideal gas simulation is that the ideal gas particles can't collide (by definition), and therefore the system won't approach the equilibrium state where the speed distribution is the Maxwell-Boltzmann distribution. You have to allow for a small chance of momentum-conserving collision between nearby gas molecules before you can show that the system approaches the M-B distribution. The number of individual molecules in such a simulation also has to be quite large before you can compare a histogram of particle velocities to the Maxwell-Boltzmann prediction.

Proving the equilibrium speed distribution from probability theory is possibly too difficult at high-school level, some first year students in college may already be able to understand it completely.

greg_rack
hilbert2 said:
The Maxwell velocity distribution practically says that for a system of gaseous substance in thermal equilibrium, there's some most likely speed ##v_p## that you can expect a randomly picked gas molecule to have, and speeds greater than that become exponentially less likely. E.g. finding a gas molecule with speed greater than ##3v_p## is already quite improbable, and you almost never run across one with speed greater than ##10v_p##. The ##v_p## gets larger with increasing temperature.

The Maxwell-Boltzmann distribution is proved by probability arguments (law of large numbers and so on) and doesn't assume much about the forces that act between different gas molecules. The problem with an ideal gas simulation is that the ideal gas particles can't collide (by definition), and therefore the system won't approach the equilibrium state where the speed distribution is the Maxwell-Boltzmann distribution. You have to allow for a small chance of momentum-conserving collision between nearby gas molecules before you can show that the system approaches the M-B distribution. The number of individual molecules in such a simulation also has to be quite large before you can compare a histogram of particle velocities to the Maxwell-Boltzmann prediction.

Proving the equilibrium speed distribution from probability theory is possibly too difficult at high-school level, some first year students in college may already be able to understand it completely.

Damn, that's exactly what I was afraid of... but what if I don't consider the gas an ideal one?
This way I would let the balls collide elastically with each other and with canvas walls being able to reach the M-B distribution with a good(isn't it?) approximation, also by taking the "molecules" number in the gas to approximately 10.000 units(is that a high enough value to apply the "law of large numbers"?).
With these assumptions, would I now be able to succesfully simulate a gas behavior which follows the M-B distribution, just by using the right function P(v)?

greg_rack said:
Damn, that's exactly what I was afraid of... but what if I don't consider the gas an ideal one?
This way I would let the balls collide elastically with each other and with canvas walls being able to reach the M-B distribution with a good(isn't it?) approximation, also by taking the "molecules" number in the gas to approximately 10.000 units(is that a high enough value to apply the "law of large numbers"?).
With these assumptions, would I now be able to succesfully simulate a gas behavior which follows the M-B distribution, just by using the right function P(v)?

That number of gas particles should be enough for comparison with the Maxwell-Boltzmann theory.

hilbert2 said:
That number of gas particles should be enough for comparison with the Maxwell-Boltzmann theory.
And how could I effectively model this situation? Which function should I use to obtain the number of molecules with a certain velocity? I'm feeling dumb asking it, but these days I couldn't manage to find a suitable solution since online there is a lot of confusion for this topic, which is also often explained with a very "advanced" and absolutely non-practical approach.

greg_rack said:
And how could I effectively model this situation? Which function should I use to obtain the number of molecules with a certain velocity? I'm feeling dumb asking it, but these days I couldn't manage to find a suitable solution since online there is a lot of confusion for this topic, which is also often explained with a very "advanced" and absolutely non-practical approach.

Calculate the lengths of the velocity vectors of each molecule, divide the velocity axis to some number of 'bins' and find how many velocity vector lengths belong to each bin. The probability of a molecule having exactly the velocity of 300 m/s, or any other particular exact value, is zero. The likelihood of a velocity to be between some two different values can be nonzero.

https://en.wikipedia.org/wiki/Histogram

greg_rack
hilbert2 said:
Calculate the lengths of the velocity vectors of each molecule, divide the velocity axis to some number of 'bins' and find how many velocity vector lengths belong to each bin. The probability of a molecule having exactly the velocity of 300 m/s, or any other particular exact value, is zero. The likelihood of a velocity to be between some two different values can be nonzero.

https://en.wikipedia.org/wiki/Histogram
I think I'm finally starting to get there, thanks a lot for the advice!

hilbert2
I did exactly this for a Python class that I took years ago, here are some results. The simulation I wrote involved circular balls bouncing around inside of a spherical cavity, the initial positions and velocities were evenly distributed and eventually thermalized by allowing the spheres to collide for some amount of time. The results below were obtained with around 100 balls averaged over a large number of runs. Let me know if you need any help writing something like this.

#### Attachments

• velocity_distribution.png
15.9 KB · Views: 172
• x-velocity.png
14.3 KB · Views: 146
• average.png
10 KB · Views: 141
greg_rack
I am not sure how much statistics you have had but here goes. Do you know how to generate random numbers from a Normal distribution? If you are working in Cartesian coordinates in three dimensions:
i.e. x-y-z; the idea would be to generate vx by (speed) * a normal random variable; vy by (speed) * a normal random variable; and vz by (speed) * a normal random variable. You may need to adjust speed to the "molecules" or "atoms" (particles) in your simulation, so that it looks pretty. You do this for each particle.

The upshot is if you draw the x,y, and z components from a normal distribution, the distribution in speed square root( vx2 + vy2 + vz2 ) will automatically be Maxwell-Boltzmann distributed.

You can use the slider to increase or decrease the speed. Timing may be an issue. You probably will not be able to simulate thousands of particles every msec. You will have to play around, but the large scale long time behavior should be noticeable.

HomogenousCow said:
I did exactly this for a Python class that I took years ago, here are some results. The simulation I wrote involved circular balls bouncing around inside of a spherical cavity, the initial positions and velocities were evenly distributed and eventually thermalized by allowing the spheres to collide for some amount of time. The results below were obtained with around 100 balls averaged over a large number of runs. Let me know if you need any help writing something like this.
It would be amazing to ask you something more "in detail", I texted you privately.

mpresic3 said:
I am not sure how much statistics you have had but here goes. Do you know how to generate random numbers from a Normal distribution? If you are working in Cartesian coordinates in three dimensions:
i.e. x-y-z; the idea would be to generate vx by (speed) * a normal random variable; vy by (speed) * a normal random variable; and vz by (speed) * a normal random variable. You may need to adjust speed to the "molecules" or "atoms" (particles) in your simulation, so that it looks pretty. You do this for each particle.

The upshot is if you draw the x,y, and z components from a normal distribution, the distribution in speed square root( vx2 + vy2 + vz2 ) will automatically be Maxwell-Boltzmann distributed.

You can use the slider to increase or decrease the speed. Timing may be an issue. You probably will not be able to simulate thousands of particles every msec. You will have to play around, but the large scale long time behavior should be noticeable.
So you are telling me that there's no need to "force" the M-B speeds distribution? Just by randomly spawning speed components on a cartesian system they will follow the distribution?

Yes that is correct for a ideal gas. The root-sum-square (i.e. speed) for three components vx, vy, vz will automatically be Maxwell Boltzmann if each of the components are normal distributed. Each component of the molecules of air in the room you're in has a normal distribution. The speed of the molecules will be Maxwell Boltzmann.

I feel this is not often emphasized in physics classes. I found this out rather late in my education (after graduate school), when I was taking a course in stochastic signal processing.

mpresic3 said:
Yes that is correct for a ideal gas. The root-sum-square (i.e. speed) for three components vx, vy, vz will automatically be Maxwell Boltzmann if each of the components are normal distributed. Each component of the molecules of air in the room you're in has a normal distribution. The speed of the molecules will be Maxwell Boltzmann.

I feel this is not often emphasized in physics classes. I found this out rather late in my education (after graduate school), when I was taking a course in stochastic signal processing.
But are you sure that JavaScript Math.random() function returns normally distributed random values?? I don't think that's the case...

I cannot say, because I never used that function. For the most part, if you want to generate random numbers on the fly and all you have is a random number generator for a uniform distribution, you can generate normal distributed random variables by averaging (say 15) uniform random variables for each normal random variable generated. I realize there are more sophisticated and accurate techniques, but this should be OK in a pinch.

mpresic3 said:
I cannot say, because I never used that function. For the most part, if you want to generate random numbers on the fly and all you have is a random number generator for a uniform distribution, you can generate normal distributed random variables by averaging (say 15) uniform random variables for each normal random variable generated. I realize there are more sophisticated and accurate techniques, but this should be OK in a pinch.
Wow, I'm discovering plenty of new things, that's really interesting! But how is that possible? How come that root-sum-square of two(since I'm working on an x-y system) normally distributed random variables is by itself M-B distributed?

I forgot to add if you average to get normal random numbers you may have to recenter the normal distribution. for example if you generate 15 random numbers from uniform on [0, 1], you will never get a value less than zero. The distribution will be (close to) normal but the average will be (around) 7.5. It is better to use uniform on [-1 , 1] if you can.

My copy of Papoulis, (something like) "Probability, Statistics, Random Variables..." , is in an office 10 miles away, that I am not allowed to visit due to COVID, but I found a source assigned as a problem. In "Applied Linear Optimal Control" page 7 by Arthur Bryson. After reviewing this source, I find I was wrong, it is the sum of the square of the (3 (not 2)) velocity components (not the square root of this sum ) that will be Maxwell Boltzmann distributed. Nevertheless the gas molecules (speeds) are distributed in this manner. See page 7 of Bryson where they discuss this

You can probably find proofs in many books on probability and statistics, besides Papoulis. Many of these texts like (Feller) are really difficult though

I do (now) see that if you are working in a 2-D coordinate system, and you are going to have problems simulating a 3-D system of particles.
You see from reading page 7 of the book by Bryson. If you generate normal distributed random variables for (two) the x-y components. The vx2+vy2 will not be Maxwell Boltzmann distributed. Instead it will be Rayleigh distributed.

As I see it the best you can do (maybe) would be go and do this anyway. The simulation of the 3-D particles with the Rayleigh distribution will demonstrate the "shadow" of all the particles in a room, projected on a 2-D plane. Unless you can display in 3-D, this is likely the best you can do.
Maxwell-Boltzmann is inherently a 3-D distribution.

mpresic3 said:
I forgot to add if you average to get normal random numbers you may have to recenter the normal distribution. for example if you generate 15 random numbers from uniform on [0, 1], you will never get a value less than zero. The distribution will be (close to) normal but the average will be (around) 7.5. It is better to use uniform on [-1 , 1] if you can.

My copy of Papoulis, (something like) "Probability, Statistics, Random Variables..." , is in an office 10 miles away, that I am not allowed to visit due to COVID, but I found a source assigned as a problem. In "Applied Linear Optimal Control" page 7 by Arthur Bryson. After reviewing this source, I find I was wrong, it is the sum of the square of the (3 (not 2)) velocity components (not the square root of this sum ) that will be Maxwell Boltzmann distributed. Nevertheless the gas molecules (speeds) are distributed in this manner. See page 7 of Bryson where they discuss this

You can probably find proofs in many books on probability and statistics, besides Papoulis. Many of these texts like (Feller) are really difficult though

I do (now) see that if you are working in a 2-D coordinate system, and you are going to have problems simulating a 3-D system of particles.
You see from reading page 7 of the book by Bryson. If you generate normal distributed random variables for (two) the x-y components. The vx2+vy2 will not be Maxwell Boltzmann distributed. Instead it will be Rayleigh distributed.

As I see it the best you can do (maybe) would be go and do this anyway. The simulation of the 3-D particles with the Rayleigh distribution will demonstrate the "shadow" of all the particles in a room, projected on a 2-D plane. Unless you can display in 3-D, this is likely the best you can do.
Maxwell-Boltzmann is inherently a 3-D distribution.
Wow, so many new things... thanks a lot! Now I'll think about this a little more "in practice", hoping I'll come up with something done.
Heartfelt thanks again!

mpresic3, greg_rack and Stephen Tashi

## 1. What is the Maxwell-Boltzmann distribution?

The Maxwell-Boltzmann distribution is a probability distribution that describes the speeds of particles in a perfect gas at a given temperature. It is used in simulations to predict the behavior of a gas based on the kinetic theory of gases.

## 2. How is the Maxwell-Boltzmann distribution calculated?

The Maxwell-Boltzmann distribution is calculated using the formula f(v) = 4π(𝑚/2π𝑘𝑇)^3/2 * v^2 * e^(-𝑚v^2/2𝑘𝑇), where m is the mass of the particle, v is the velocity, k is the Boltzmann constant, and T is the temperature in Kelvin.

## 3. What is the significance of the Maxwell-Boltzmann distribution in gas simulations?

The Maxwell-Boltzmann distribution is significant because it allows us to predict the behavior of a gas based on the kinetic theory of gases. It helps us understand the speed and energy distribution of gas particles at a given temperature, and can be used to study various thermodynamic properties of gases.

## 4. How does temperature affect the Maxwell-Boltzmann distribution?

Temperature has a direct effect on the shape and spread of the Maxwell-Boltzmann distribution curve. As temperature increases, the curve shifts to the right and becomes wider, indicating a larger spread of particle speeds. This is because higher temperatures result in higher average speeds of gas particles.

## 5. Can the Maxwell-Boltzmann distribution be applied to real gases?

The Maxwell-Boltzmann distribution is an idealized model that assumes a perfect gas with no intermolecular forces. While it may not be a perfect representation of real gases, it can still provide valuable insights and predictions about the behavior of gases in certain conditions. In some cases, adjustments can be made to account for deviations from the ideal gas model.

• Classical Physics
Replies
1
Views
926
• Electromagnetism
Replies
4
Views
3K
• Electromagnetism
Replies
38
Views
3K
• Classical Physics
Replies
8
Views
698
• Electromagnetism
Replies
4
Views
2K
• Electromagnetism
Replies
3
Views
1K
• Mechanics
Replies
3
Views
667
• Electromagnetism
Replies
2
Views
1K
• Electromagnetism
Replies
4
Views
3K
• Mechanics
Replies
10
Views
2K