# ==============================================================================
# LECTURE 3: Forms of Energy and Calculating Work
# Script: Maxwell-Boltzmann Demo
# Author: Edward Maginn, CBE 20260
# Description:
# This notebook lets you interactively explore the Maxwell–Boltzmann speed distribution
# and connect it to molecular kinetic energy.
# Learning goals
# - See how the distribution **shifts and broadens** as temperature increases
# - Compare how **molecular mass** changes the typical speeds
# - Verify numerically that
# $[\left\langle \tfrac{1}{2} m v^2 \right\rangle = \tfrac{3}{2} k_B T]$
# Tip: Run cells top-to-bottom once. Then use the sliders/dropdowns to explore.
# ==============================================================================
import numpy as np
import math
import matplotlib.pyplot as plt
# Interactive widgets (works in JupyterLab/Notebook; in some environments you may need:
# pip install ipywidgets
# jupyter nbextension enable --py widgetsnbextension
import ipywidgets as widgets
from IPython.display import display, Markdown
# Physical constants
kB = 1.380649e-23 # Boltzmann constant, J/K
NA = 6.02214076e23 # Avogadro's number, 1/mol
R = 8.314462618 # Gas constant, J/(mol K)
# A small library of molecules (molar mass in g/mol)
species_db = {
"Helium (He)": 4.0026,
"Hydrogen (H2)": 2.01588,
"Nitrogen (N2)": 28.0134,
"Oxygen (O2)": 31.9988,
"Carbon dioxide (CO2)": 44.0095,
"Water vapor (H2O)": 18.01528,
"Argon (Ar)": 39.948,
}
# Functions to calculate
def molar_mass_to_molecule_mass(M_g_per_mol: float) -> float:
"""Convert molar mass (g/mol) to mass per molecule (kg)."""
return (M_g_per_mol * 1e-3) / NA
def f_MB(v: np.ndarray, m: float, T: float) -> np.ndarray:
"""Maxwell–Boltzmann speed distribution f(v) for speeds v (m/s)."""
pref = 4 * math.pi * (m / (2 * math.pi * kB * T))**(1.5)
return pref * v**2 * np.exp(-(m * v**2) / (2 * kB * T))
def speeds_characteristic(m: float, T: float):
"""Most probable, mean, and RMS speeds."""
v_mp = math.sqrt(2 * kB * T / m)
v_mean = math.sqrt(8 * kB * T / (math.pi * m))
v_rms = math.sqrt(3 * kB * T / m)
return v_mp, v_mean, v_rms
display(Markdown("✅ **Setup complete.** Use the interactive cell below."))✅ Setup complete. Use the interactive cell below.
