A blog of Python-related topics and code.

The Reutersvärd Triangle

The Reutersvärd Triangle is an example of an impossible object: a two-dimensional optical illusion perceived as a projection of a three-dimensional object that cannot exist. The Python code below creates an SVG image of Reutersvärd's impossible triangular arrangement of cubes, similar to the more famous Penrose Triangle.

The Maxwell Construction

The van der Waals equation of state for a gas is: $$ \left(p - \frac{a}{V^2}\right) \left(V - b\right) = RT $$ in terms of the pressure, $p$, molar volume, $V$ and temperature, $T$. $a$ and $b$ are constants which depend on the gas, so it is often useful to recast this equation into reduced form: $$ \left(p_\mathrm{r} + \frac{3}{V_\mathrm{r}^2}\right)\left(V_\mathrm{r}-\frac{1}{3}\right) = \frac{8}{3}T_\mathrm{r}, $$ or equivalently $$ p_\mathrm{r} = \frac{8T_\mathrm{r}}{3V_\mathrm{r}-1} - \frac{3}{V_\mathrm{r}^2}, $$ where the reduced variables $p_\mathrm{r} = p/p_\mathrm{c}$, $V_\mathrm{r} = V/V_\mathrm{c}$ and $T_\mathrm{r} = T / T_\mathrm{c}$ in terms of the critical pressure, volume and temperature: $$ p_\mathrm{c} = \frac{a}{27b^2}, \;\; V_\mathrm{c} = 3b, \;\; k_\mathrm{B}T_\mathrm{c} = \frac{8a}{27b}. $$ Whilst the van der Waals equation does a better job than the ideal gas law of describing the properties of a real gas it suffers from an artefact for $T_\mathrm{r} < 1$ (that is, temperatures $T < T_\mathrm{c}$), as shown in the plot below.

Inset plots in Matplotlib

Using AxesGrid

Matplotlib has included the AxesGrid toolkit since v0.99. One of the useful things this allows you to do is include "inset" figures which are often used to show greater detail of a region of the enclosing plot, as in this example (the graph is of the variation of the heat capacity of tantalum with temperature).

Linear and non-linear fitting of the Theis equation

The Theis equation

This post is also available as a Jupyter Notebook.

The separatrix for a simple pendulum

The Hamiltonian of a simple pendulum of mass $m$ and length $l$ may be written $$ \textstyle H = T + V = \frac{1}{2}ml^2\dot{\theta}^2 + mgl(1-\cos\theta) $$ Or, dividing by the moment of inertia, $I=ml^2$, $$ \textstyle H' = \frac{1}{2}\dot{\theta}^2 + \frac{g}{l}(1-\cos\theta) $$ Contours of constant $H$ in the phase space $(\theta, \dot{\theta})$ are plotted below, with the contour corresponding to the separatrix highlighted. This is the value of $H=2g/l$ corresponding to the boundary between two distinct kinds of motion: for $H$ less than this value, the pendulum swings back and forth (closed curves in phase space); for $H$ greater than this, the pendulum turns in continuous circles.