{
 "nbformat": 4,
 "nbformat_minor": 5,
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python",
   "version": "3"
  }
 },
 "cells": [
  {
   "cell_type": "code",
   "id": "cell-0",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": "# --- house style (the Press palette: accent/navy/gold/parchment) ---\nimport matplotlib as mpl\nACCENT, NAVY, GOLD, PARCH = '#7a1f1f', '#1f3a5f', '#b8860b', '#f7f2e7'\nmpl.rcParams.update({'figure.facecolor': PARCH, 'axes.facecolor': '#fffdf6',\n                     'axes.edgecolor': '#c9bfa3', 'font.family': 'serif'})\n\nimport numpy as np\nimport matplotlib.pyplot as plt\n\n# --- The monitor opus: a subdivision rule over [0,1] -----------------------\nRULES = {'shock':          lambda x: 1 + 9*np.exp(-((x-0.5)**2)/0.002),\n         'boundary layer': lambda x: 1/(0.02 + x),\n         'two bumps':      lambda x: 1 + 4*np.exp(-((x-0.25)**2)/0.004)\n                                        + 6*np.exp(-((x-0.75)**2)/0.002)}\nrho = RULES['boundary layer']                        # <- choose the monitor\n\nxx  = np.linspace(0, 1, 20001)\ncdf = np.cumsum(rho(xx)); cdf /= cdf[-1]             # the running tesseraction F(x)\nN   = 24                                             # tessera budget\nedges  = np.interp(np.linspace(0, 1, N+1), cdf, xx)  # x_i = F^-1(i/N): Thm 13.1\nshares = np.diff(np.interp(edges, xx, cdf))\nprint(f\"worst deviation from 1/N = {np.abs(shares - 1/N).max():.2e}  equi-distributed\")\n\n# --- Thm 13.3 closed form for the wall rule, checked against the numerics --\ndelta, L = 0.02, 1.0\nexact = delta*((1 + L/delta)**(np.arange(N+1)/N) - 1)\nprint(f\"closed-form vs numeric edges, max gap: {np.abs(exact - edges).max():.2e}  (Thm 13.3)\")\n\nfig, ax = plt.subplots(figsize=(9, 3.6))\nax.plot(xx, rho(xx), color=NAVY, lw=2, label='monitor rho(x)')\nfor e in edges:\n    ax.axvline(e, color=ACCENT, lw=0.9)\nmid = 0.5*(edges[:-1] + edges[1:])\nax.plot(mid, rho(mid), 'o', color=GOLD, ms=4)\nax.set(xlabel='x', title=f'{N} cells, each holding share 1/N - narrow where rho rages (Thm 13.1)')\nax.legend(); plt.tight_layout(); plt.show()\n"
  }
 ]
}