A blog of Python-related topics and code.

Calculating the heat capacity from the partition sum

It is a standard result of statistical thermodynamics that the molar heat capacity at constant volume, $C_{V,\mathrm{m}}$, is related to the molecular partition sum, $q$, through the relation $$ C_{V,\mathrm{m}} = R\beta^2\left[\frac{\ddot{q}}{q} - \left( \frac{\dot{q}}{q}\right)^2\right], $$ where $\dot{q}$ and $\ddot{q}$ represent the first and second derivative of $q$ with respect to the "thermodynamic beta", $\beta = \frac{1}{k_\mathrm{B}T}$:

Interactive particle-in-a-box wavefunctions in Jupyter

A very quick plot of particle-in-a-box wavefunctions with interactive control over the box length, $L$ – this kind of thing is very easy with Jupyter and ipywidgets' interact method: Just define a function to plot the wavefunction for a provided argument $L$ and pass it to interact along with a range and stepsize of values of $L$ and a slider will appear above the plot. Event callbacks and range-checking are all handled for you.

The arcsine law

Suppose a fair coin is tossed $N$ times and the number of "heads", $n_\mathrm{heads}$, and "tails", $n_\mathrm{tails}$, recorded after each toss. Over the course of the trial, how many times is the number of heads recorded greater than the number of tails? Naive intuition might lead to the expectation that as $N$ increases the number of times "heads" is in the lead increases in proportion.

Atomic term symbols

The problem of predicting the term symbols for states derived from a $nl^r$ atomic configuration (without using group theory!) provides a good use of Python's collections and itertools libraries.

A customizable Ebbinghaus illusion

The Ebbinghaus illusion is a famous visual effect based on the perception of the relative size. Two circles of the same size are surrounded in one case by obviously larger circles and in the other by obviously smaller circles. The former is perceived as being smaller than the latter, as illustrated below.