Spin the Lorenz attractor
NumPy integrates the classic chaotic system; an HTML cell renders the butterfly on a canvas you can drag to rotate. Every frame draws client-side from one baked JSON string.
Spin the Lorenz attractor
In 1963 Edward Lorenz boiled convection down to three little equations and
found chaos: trajectories that never settle and never repeat, tracing the shape
now known as the butterfly. Python integrates the system below with a
fourth-order Runge-Kutta scheme and publishes the curve as JSON; the HTML cell
renders it in 3D on a <canvas>. Drag the plot to rotate it yourself, or let
it spin.
1import json2import numpy as np34sigma, rho, beta = 10.0, 28.0, 8.0 / 3.056def deriv(p):7 x, y, z = p8 return np.array([sigma * (y - x), x * (rho - z) - y, x * y - beta * z])910dt = 0.00511steps = 1200012p = np.array([1.0, 1.0, 1.05])13pts = np.empty((steps, 3))14for i in range(steps):15 k1 = deriv(p)16 k2 = deriv(p + 0.5 * dt * k1)17 k3 = deriv(p + 0.5 * dt * k2)18 k4 = deriv(p + dt * k3)19 p = p + (dt / 6.0) * (k1 + 2 * k2 + 2 * k3 + k4)20 pts[i] = p2122# Drop the transient approach, thin the rest, trim to plotting precision.23curve = pts[400::4]24lorenz_points = json.dumps(np.round(curve, 3).tolist(), separators=(",", ":"))2526f"{len(curve):,} points along a single trajectory; the curve below never crosses itself."
'2,900 points along a single trajectory; the curve below never crosses itself.'
1<div style="font-family:var(--verso-prose-font-family,sans-serif);">2 <div style="display:flex;align-items:center;gap:8px;margin-bottom:8px;">3 <button id="lz-spin" type="button" style="padding:3px 12px;border:1px solid var(--verso-border-default,#bfbfbf);border-radius:999px;background:transparent;color:inherit;font:inherit;font-size:12.5px;cursor:pointer;">Pause spin</button>4 <button id="lz-reset" type="button" style="padding:3px 12px;border:1px solid var(--verso-border-default,#bfbfbf);border-radius:999px;background:transparent;color:inherit;font:inherit;font-size:12.5px;cursor:pointer;">Reset view</button>5 <span style="margin-left:auto;font-size:12.5px;color:var(--verso-fg-muted,#777);">drag to rotate</span>6 </div>7 <canvas id="lz-canvas" width="820" height="560"8 style="width:100%;max-width:820px;display:block;border-radius:8px;cursor:grab;touch-action:none;"></canvas>9</div>10<script>11(function () {12 var pts = @lorenz_points;13 var canvas = document.getElementById('lz-canvas');14 var ctx = canvas.getContext('2d');15 var n = pts.length;1617 var c = [0, 0, 0];18 for (var i = 0; i < n; i++)19 for (var k = 0; k < 3; k++) c[k] += pts[i][k] / n;20 var maxR = 0;21 for (var i = 0; i < n; i++) {22 var dx = pts[i][0] - c[0], dy = pts[i][1] - c[1], dz = pts[i][2] - c[2];23 var r = Math.sqrt(dx * dx + dy * dy + dz * dz);24 if (r > maxR) maxR = r;25 }26 // Fit for every possible rotation, perspective included, so nothing clips.27 // The perspective distance is in world units and must comfortably exceed28 // maxR, or points swinging near the camera would blow up.29 var scale = (Math.min(canvas.width, canvas.height) / 2 - 16) / (maxR * 1.06);30 var PERSP = maxR * 6;3132 var HOME = { yaw: 0.9, pitch: 0.35 };33 var yaw = HOME.yaw, pitch = HOME.pitch;34 var spinning = true, dragging = false, dirty = true;35 var lastX = 0, lastY = 0;3637 function draw() {38 ctx.fillStyle = '#0b0e15';39 ctx.fillRect(0, 0, canvas.width, canvas.height);40 var cy = Math.cos(yaw), sy = Math.sin(yaw);41 var cp = Math.cos(pitch), sp = Math.sin(pitch);42 var ox = canvas.width / 2, oy = canvas.height / 2;43 ctx.lineCap = 'round';44 var prev = null;45 for (var i = 0; i < n; i++) {46 var X = pts[i][0] - c[0], Y = pts[i][1] - c[1], Z = pts[i][2] - c[2];47 var x1 = cy * X - sy * Y, y1 = sy * X + cy * Y;48 var v = cp * Z - sp * y1;49 var d = sp * Z + cp * y1;50 var k = PERSP / (PERSP - d);51 var px = ox + x1 * scale * k;52 var py = oy - v * scale * k;53 if (prev) {54 var dn = Math.max(0, Math.min(1, d / (2 * maxR) + 0.5));55 ctx.strokeStyle = 'hsla(' + Math.round(200 + 140 * i / n) + ', 85%, '56 + Math.round(36 + 30 * dn) + '%, 0.8)';57 ctx.lineWidth = 0.6 + 1.2 * dn;58 ctx.beginPath();59 ctx.moveTo(prev[0], prev[1]);60 ctx.lineTo(px, py);61 ctx.stroke();62 }63 prev = [px, py];64 }65 }6667 canvas.addEventListener('pointerdown', function (e) {68 dragging = true; lastX = e.clientX; lastY = e.clientY;69 canvas.setPointerCapture(e.pointerId);70 canvas.style.cursor = 'grabbing';71 e.preventDefault();72 });73 canvas.addEventListener('pointermove', function (e) {74 if (!dragging) return;75 yaw += (e.clientX - lastX) * 0.008;76 pitch += (e.clientY - lastY) * 0.008;77 pitch = Math.max(-1.5, Math.min(1.5, pitch));78 lastX = e.clientX; lastY = e.clientY;79 dirty = true;80 });81 canvas.addEventListener('pointerup', function () {82 dragging = false;83 canvas.style.cursor = 'grab';84 });8586 var spinBtn = document.getElementById('lz-spin');87 spinBtn.addEventListener('click', function () {88 spinning = !spinning;89 spinBtn.textContent = spinning ? 'Pause spin' : 'Spin';90 });91 document.getElementById('lz-reset').addEventListener('click', function () {92 yaw = HOME.yaw; pitch = HOME.pitch; dirty = true;93 });9495 function loop() {96 if (!canvas.isConnected) return;97 if (spinning && !dragging) { yaw += 0.004; dirty = true; }98 if (dirty) { draw(); dirty = false; }99 requestAnimationFrame(loop);100 }101 draw();102 requestAnimationFrame(loop);103})();104</script>
Sensitive dependence, literally
Nudge the starting point by one part in a million and the trajectory ends up
somewhere completely different a few seconds later; that is the butterfly
effect, and this butterfly is its portrait. Try rho = 14.0 for a system that
spirals politely into a fixed point, or rho = 99.65 for chaos in a different
wardrobe.
Note what the HTML cell did not do: it never ran any Python. It picked
@lorenz_points out of the variable store, the same way a SQL result or an
HTTP response travels between cells, and every frame you see is drawn
client-side from that one baked JSON string.
Run it yourself
Nothing on this page is executable; the outputs were baked in by a real run. Download the notebook and it runs anywhere Verso does: VS Code, the browser, or the terminal.
dotnet tool install --global Verso.Cliverso serve lorenz-attractor.versoWhat this shows
NumPy integrates the classic chaotic system; an HTML cell renders the butterfly on a canvas you can drag to rotate. Every frame draws client-side from one baked JSON string.
Every language in this notebook reads and writes the same variable store, so a value produced in one cell is simply there in the next, whatever the language.