-
-
Notifications
You must be signed in to change notification settings - Fork 46.5k
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
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 0608c40
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 7457b25
avg and mps speed formulae added
Baron105 b1ef5a3
fixed_spacing
Baron105 c957bd7
.
Baron105 07502c8
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] ead4f47
spacing
Baron105 c18b39d
.
Baron105 cb006c3
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] ab04734
ws
Baron105 69487f3
Merge branch 'master' of https://github.com/Baron105/Python
Baron105 6a741dc
added amicable numbers
Baron105 d13c09d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 18df9ba
Merge branch 'TheAlgorithms:master' into master
Baron105 63734b9
added amicable numbers
Baron105 aa5f176
descriptive
Baron105 9779c1a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] aa4d4e8
spacing
Baron105 7bb663f
Merge branch 'master' of https://github.com/Baron105/Python
Baron105 52c2cb8
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 643958b
removed
Baron105 65f5851
Merge branch 'master' of https://github.com/Baron105/Python
Baron105 7f14ab9
Merge branch 'TheAlgorithms:master' into master
Baron105 c05ed7c
changed name of file and added code improvements
Baron105 886fb5d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 97b89cd
issues fixed due to pi
Baron105 a1c783a
Merge branch 'master' of https://github.com/Baron105/Python
Baron105 1c2b7b2
requested changes added
Baron105 5aa65b8
Merge branch 'TheAlgorithms:master' into master
Baron105 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
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() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?