Skip to content

Instantly share code, notes, and snippets.

@PM2Ring
Created January 19, 2023 10:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PM2Ring/78d1d9c906bc3c52146cac0edfc88716 to your computer and use it in GitHub Desktop.
Save PM2Ring/78d1d9c906bc3c52146cac0edfc88716 to your computer and use it in GitHub Desktop.
Plot a Maxwell Boltzmann distribution graph using Sage
""" Maxwell–Boltzmann distribution
See https://en.m.wikipedia.org/wiki/Maxwell%E2%80%93Boltzmann_distribution
Written by PM 2Ring 2021.06.20
"""
# Gas constant g⋅m^2⋅s^−2⋅K^−1⋅mol^−1
R = 8314.46261815324
@interact
def _(T=('temp (°C)', 20), mass=('mass (g/mol)', 29),
auto_update=False):
T += 273.15
vp2 = 2 * T * R / mass
vp = sqrt(vp2)
a = 4*pi / (pi * vp2)^1.5
vm = 2 * vp / sqrt(pi)
vr = vp * sqrt(3/2)
print(f"Mode speed {n(vp)} m/s")
print(f"Mean speed {n(vm)} m/s")
print(f"RMS speed {n(vr)} m/s")
def f(v):
v2 = v^2
return a * v2 * exp(-v2 / vp2)
P = points([(v, f(v)) for v in (vp, vm, vr)], color="red")
P += plot(f, (0, pi * vm), gridlines="major", frame=True,
title=f"Maxwell–Boltzmann distribution, {T=:.2f} K, {mass=:.2f} g/mol",
axes_labels=["Speed (m/s", "Probability"], axes_labels_size=1.15)
P.show( )
@PM2Ring
Copy link
Author

PM2Ring commented Jan 19, 2023

Live demo on SageMathCell

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment