Skip to content

Commit 0b2c9fb

Browse files
Adding avg and mps speed formulae for ideal gases (#10229)
* avg and mps speed formulae added * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * avg and mps speed formulae added * fixed_spacing * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * spacing * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * ws * added amicable numbers * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * added amicable numbers * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * spacing * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * removed * changed name of file and added code improvements * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * issues fixed due to pi * requested changes added --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 9fb0cd2 commit 0b2c9fb

File tree

1 file changed

+111
-0
lines changed

1 file changed

+111
-0
lines changed

physics/speeds_of_gas_molecules.py

+111
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
"""
2+
The root-mean-square, average and most probable speeds of gas molecules are
3+
derived from the Maxwell-Boltzmann distribution. The Maxwell-Boltzmann
4+
distribution is a probability distribution that describes the distribution of
5+
speeds of particles in an ideal gas.
6+
7+
The distribution is given by the following equation:
8+
9+
-------------------------------------------------
10+
| f(v) = (M/2πRT)^(3/2) * 4πv^2 * e^(-Mv^2/2RT) |
11+
-------------------------------------------------
12+
13+
where:
14+
f(v) is the fraction of molecules with a speed v
15+
M is the molar mass of the gas in kg/mol
16+
R is the gas constant
17+
T is the absolute temperature
18+
19+
More information about the Maxwell-Boltzmann distribution can be found here:
20+
https://en.wikipedia.org/wiki/Maxwell%E2%80%93Boltzmann_distribution
21+
22+
The average speed can be calculated by integrating the Maxwell-Boltzmann distribution
23+
from 0 to infinity and dividing by the total number of molecules. The result is:
24+
25+
---------------------
26+
| vavg = √(8RT/πM) |
27+
---------------------
28+
29+
The most probable speed is the speed at which the Maxwell-Boltzmann distribution
30+
is at its maximum. This can be found by differentiating the Maxwell-Boltzmann
31+
distribution with respect to v and setting the result equal to zero. The result is:
32+
33+
---------------------
34+
| vmp = √(2RT/M) |
35+
---------------------
36+
37+
The root-mean-square speed is another measure of the average speed
38+
of the molecules in a gas. It is calculated by taking the square root
39+
of the average of the squares of the speeds of the molecules. The result is:
40+
41+
---------------------
42+
| vrms = √(3RT/M) |
43+
---------------------
44+
45+
Here we have defined functions to calculate the average and
46+
most probable speeds of molecules in a gas given the
47+
temperature and molar mass of the gas.
48+
"""
49+
50+
# import the constants R and pi from the scipy.constants library
51+
from scipy.constants import R, pi
52+
53+
54+
def avg_speed_of_molecule(temperature: float, molar_mass: float) -> float:
55+
"""
56+
Takes the temperature (in K) and molar mass (in kg/mol) of a gas
57+
and returns the average speed of a molecule in the gas (in m/s).
58+
59+
Examples:
60+
>>> avg_speed_of_molecule(273, 0.028) # nitrogen at 273 K
61+
454.3488755020387
62+
>>> avg_speed_of_molecule(300, 0.032) # oxygen at 300 K
63+
445.52572733919885
64+
>>> avg_speed_of_molecule(-273, 0.028) # invalid temperature
65+
Traceback (most recent call last):
66+
...
67+
Exception: Absolute temperature cannot be less than 0 K
68+
>>> avg_speed_of_molecule(273, 0) # invalid molar mass
69+
Traceback (most recent call last):
70+
...
71+
Exception: Molar mass should be greater than 0 kg/mol
72+
"""
73+
74+
if temperature < 0:
75+
raise Exception("Absolute temperature cannot be less than 0 K")
76+
if molar_mass <= 0:
77+
raise Exception("Molar mass should be greater than 0 kg/mol")
78+
return (8 * R * temperature / (pi * molar_mass)) ** 0.5
79+
80+
81+
def mps_speed_of_molecule(temperature: float, molar_mass: float) -> float:
82+
"""
83+
Takes the temperature (in K) and molar mass (in kg/mol) of a gas
84+
and returns the most probable speed of a molecule in the gas (in m/s).
85+
86+
Examples:
87+
>>> mps_speed_of_molecule(273, 0.028) # nitrogen at 273 K
88+
402.65620701908966
89+
>>> mps_speed_of_molecule(300, 0.032) # oxygen at 300 K
90+
394.836895549922
91+
>>> mps_speed_of_molecule(-273, 0.028) # invalid temperature
92+
Traceback (most recent call last):
93+
...
94+
Exception: Absolute temperature cannot be less than 0 K
95+
>>> mps_speed_of_molecule(273, 0) # invalid molar mass
96+
Traceback (most recent call last):
97+
...
98+
Exception: Molar mass should be greater than 0 kg/mol
99+
"""
100+
101+
if temperature < 0:
102+
raise Exception("Absolute temperature cannot be less than 0 K")
103+
if molar_mass <= 0:
104+
raise Exception("Molar mass should be greater than 0 kg/mol")
105+
return (2 * R * temperature / molar_mass) ** 0.5
106+
107+
108+
if __name__ == "__main__":
109+
import doctest
110+
111+
doctest.testmod()

0 commit comments

Comments
 (0)