Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding avg and mps speed formulae for ideal gases #10229

Merged
merged 29 commits into from
Oct 14, 2023
Merged
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
1427fe3
avg and mps speed formulae added
Baron105 Oct 10, 2023
0608c40
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 10, 2023
7457b25
avg and mps speed formulae added
Baron105 Oct 10, 2023
b1ef5a3
fixed_spacing
Baron105 Oct 10, 2023
c957bd7
.
Baron105 Oct 10, 2023
07502c8
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 10, 2023
ead4f47
spacing
Baron105 Oct 10, 2023
c18b39d
.
Baron105 Oct 10, 2023
cb006c3
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 10, 2023
ab04734
ws
Baron105 Oct 10, 2023
69487f3
Merge branch 'master' of https://github.com/Baron105/Python
Baron105 Oct 10, 2023
6a741dc
added amicable numbers
Baron105 Oct 10, 2023
d13c09d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 10, 2023
18df9ba
Merge branch 'TheAlgorithms:master' into master
Baron105 Oct 10, 2023
63734b9
added amicable numbers
Baron105 Oct 10, 2023
aa5f176
descriptive
Baron105 Oct 10, 2023
9779c1a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 10, 2023
aa4d4e8
spacing
Baron105 Oct 10, 2023
7bb663f
Merge branch 'master' of https://github.com/Baron105/Python
Baron105 Oct 10, 2023
52c2cb8
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 10, 2023
643958b
removed
Baron105 Oct 10, 2023
65f5851
Merge branch 'master' of https://github.com/Baron105/Python
Baron105 Oct 10, 2023
7f14ab9
Merge branch 'TheAlgorithms:master' into master
Baron105 Oct 12, 2023
c05ed7c
changed name of file and added code improvements
Baron105 Oct 12, 2023
886fb5d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 12, 2023
97b89cd
issues fixed due to pi
Baron105 Oct 12, 2023
a1c783a
Merge branch 'master' of https://github.com/Baron105/Python
Baron105 Oct 12, 2023
1c2b7b2
requested changes added
Baron105 Oct 12, 2023
5aa65b8
Merge branch 'TheAlgorithms:master' into master
Baron105 Oct 13, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 111 additions & 0 deletions physics/speeds_of_gas_molecules.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
"""
The root-mean-square, average and most probable speeds of gas molecules are
derived from the Maxwell-Boltzmann distribution. The Maxwell-Boltzmann
distribution is a probability distribution that describes the distribution of
speeds of particles in an ideal gas.

The distribution is given by the following equation:

-------------------------------------------------
| f(v) = (M/2πRT)^(3/2) * 4πv^2 * e^(-Mv^2/2RT) |
-------------------------------------------------

where:
f(v) is the fraction of molecules with a speed v
M is the molar mass of the gas in kg/mol
R is the gas constant
T is the absolute temperature

More information about the Maxwell-Boltzmann distribution can be found here:
https://en.wikipedia.org/wiki/Maxwell%E2%80%93Boltzmann_distribution

The average speed can be calculated by integrating the Maxwell-Boltzmann distribution
from 0 to infinity and dividing by the total number of molecules. The result is:

---------------------
| vavg = √(8RT/πM) |
---------------------

The most probable speed is the speed at which the Maxwell-Boltzmann distribution
is at its maximum. This can be found by differentiating the Maxwell-Boltzmann
distribution with respect to v and setting the result equal to zero. The result is:

---------------------
| vmp = √(2RT/M) |
---------------------

The root-mean-square speed is another measure of the average speed
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No function for the RMS speed?

of the molecules in a gas. It is calculated by taking the square root
of the average of the squares of the speeds of the molecules. The result is:

---------------------
| vrms = √(3RT/M) |
---------------------

Here we have defined functions to calculate the average and
most probable speeds of molecules in a gas given the
temperature and molar mass of the gas.
"""

# import the constants R and pi from the scipy.constants library
from scipy.constants import R, pi


def avg_speed_of_molecule(temperature: float, molar_mass: float) -> float:
"""
Takes the temperature (in K) and molar mass (in kg/mol) of a gas
and returns the average speed of a molecule in the gas (in m/s).

Examples:
>>> avg_speed_of_molecule(273, 0.028) # nitrogen at 273 K
454.3488755020387
>>> avg_speed_of_molecule(300, 0.032) # oxygen at 300 K
445.52572733919885
>>> avg_speed_of_molecule(-273, 0.028) # invalid temperature
Traceback (most recent call last):
...
Exception: Absolute temperature cannot be less than 0 K
>>> avg_speed_of_molecule(273, 0) # invalid molar mass
Traceback (most recent call last):
...
Exception: Molar mass should be greater than 0 kg/mol
"""

if temperature < 0:
raise Exception("Absolute temperature cannot be less than 0 K")
if molar_mass <= 0:
raise Exception("Molar mass should be greater than 0 kg/mol")
return (8 * R * temperature / (pi * molar_mass)) ** 0.5


def mps_speed_of_molecule(temperature: float, molar_mass: float) -> float:
"""
Takes the temperature (in K) and molar mass (in kg/mol) of a gas
and returns the most probable speed of a molecule in the gas (in m/s).

Examples:
>>> mps_speed_of_molecule(273, 0.028) # nitrogen at 273 K
402.65620701908966
>>> mps_speed_of_molecule(300, 0.032) # oxygen at 300 K
394.836895549922
>>> mps_speed_of_molecule(-273, 0.028) # invalid temperature
Traceback (most recent call last):
...
Exception: Absolute temperature cannot be less than 0 K
>>> mps_speed_of_molecule(273, 0) # invalid molar mass
Traceback (most recent call last):
...
Exception: Molar mass should be greater than 0 kg/mol
"""

if temperature < 0:
raise Exception("Absolute temperature cannot be less than 0 K")
if molar_mass <= 0:
raise Exception("Molar mass should be greater than 0 kg/mol")
return (2 * R * temperature / molar_mass) ** 0.5


if __name__ == "__main__":
import doctest

doctest.testmod()