Showcase · examples/damped_driven_resonance.lms
Driven damped resonance
Nonlinear Dynamics: Driven damped harmonic oscillator steady-state magnification M(ω/ω0), resonance peak backbone locus, half-power bandwidth and quadrature phase lag φ(ω/ω0)
Loading the WebAssembly host…
What runs
The page fetches examples/damped_driven_resonance.bin, 6,034 bytes of lmcode bytecode
(sha256 7fe82b99d322…), and hands it to the same lmplot.wasm every example uses.
The bytecode calls the host's Plot functions (plot.cmd, plot.data, plot.num,
plot.png …); gnuplot draws into an in-memory file system and the page shows the files the program wrote.
In SVG mode plot.png("x.png", w, h) writes x.svg with gnuplot's svg terminal; PNG mode uses lmplot's
own rasterizer and fonts, the same bytes as the native binary.
The program was compiled ahead of time from the source on the right with lmscript; the lmscript lines around the
_lmcode_ block (data generation, ${...} values, run_lmcode) ran at compile time.
# Driven damped harmonic oscillator: steady-state amplitude magnification M(r)
# and phase lag phi(r) across quality factors Q = 1.5, 3, 6, 12, plus backbone
# resonance locus and half-power (-3 dB) bandwidth computed in lmscript.
# Writes images/damped_driven_resonance.png (900x560).
mkdir("images");
mkdir("build");
p = _lmcode_ cli="lmplot" install="github:lmctl-ai/lmplot@v0.3.1";
fn sqrt_pos(v: float) -> float {
let g = 1.0;
if (v > 0.000001) {
g = v * 0.5 + 0.5;
for (k in range(0, 10)) {
g = 0.5 * (g + v / g);
}
}
return g;
}
# Compute exact resonant peak coordinates (r_peak = sqrt(1 - 1/(2*Q^2)), M_peak)
let peaks = "";
for (idx in range(0, 4)) {
let q = 1.5;
if (idx == 1) { q = 3.0; }
if (idx == 2) { q = 6.0; }
if (idx == 3) { q = 10.0; }
let r_pk = sqrt_pos(1.0 - 1.0 / (2.0 * q * q));
let m_pk = q / sqrt_pos(1.0 - 1.0 / (4.0 * q * q));
peaks = peaks + str(r_pk) + " " + str(m_pk) + " " + str(q) + "\n";
}
plot.data("PEAKS", peaks);
plot.cmd("set terminal unknown");
plot.cmd("stats $PEAKS using 1:2 nooutput");
let max_mag = plot.num("STATS_max_y");
let q6_bw = 1.0 / 6.0;
plot.echo("Max magnification (Q=10): " + str(max_mag));
plot.echo("Q=6 half-power bandwidth: " + str(q6_bw) + " omega_0");
plot.png("images/damped_driven_resonance.png", 900, 560);
plot.cmd("set encoding utf8");
plot.cmd("set multiplot");
plot.cmd("set samples 500");
# Left panel: Amplitude Magnification M(omega / omega_0)
plot.script("set origin 0.0, 0.0\nset size 0.54, 1.0");
plot.script("set lmargin 8.5\nset rmargin 2.5\nset tmargin 3.2\nset bmargin 4.2");
plot.cmd("set title 'Resonance Magnification M(ω/ω_0) — Backbone Locus' font ',11.5'");
plot.cmd("set xlabel 'Normalized Driving Frequency r = ω / ω_0'");
plot.cmd("set ylabel 'Amplitude Magnification M(r) = X / X_{st}'");
plot.cmd("set xrange [0:2.2]");
plot.cmd("set yrange [0:13.8]");
plot.cmd("set grid xtics ytics lc rgb '#e2e8f0' lw 1");
plot.cmd("set key top right samplen 1.8 spacing 1.1 opaque box lc rgb '#cbd5e1' font ',8.5'");
plot.cmd("set style fill transparent solid 0.18 noborder");
plot.cmd("mag(r, Q) = 1.0 / sqrt((1.0 - r**2)**2 + (r / Q)**2)");
plot.cmd("backbone(r) = (r < 0.999 && r > 0.4) ? 1.0 / (sqrt(1.0 - r**4)) : 1/0");
# Annotate Q = 6 half-power bandwidth (-3 dB at M = 6/sqrt(2) = 4.24)
plot.cmd("set arrow 1 from 0.916, 4.26 to 1.083, 4.26 heads size screen 0.008,20 lw 1.4 lc rgb '#2563eb' front");
plot.cmd("set label 1 'Δω_{-3dB} = ω_0 / Q' at 1.12, 4.26 left front font ',8.5' tc rgb '#1d4ed8'");
plot.cmd("plot mag(x, 10.0) with filledcurves y1=0 lc rgb '#dc2626' notitle, mag(x, 10.0) with lines lw 2.5 lc rgb '#dc2626' title 'Q = 10.0 (ζ = 0.050)', mag(x, 6.0) with filledcurves y1=0 lc rgb '#2563eb' notitle, mag(x, 6.0) with lines lw 2.4 lc rgb '#1d4ed8' title 'Q = 6.0 (ζ = 0.083)', mag(x, 3.0) with lines lw 2.2 lc rgb '#d97706' title 'Q = 3.0 (ζ = 0.167)', mag(x, 1.5) with lines lw 2.2 lc rgb '#059669' title 'Q = 1.5 (ζ = 0.333)', [0.45:0.995] backbone(x) with lines dt 2 lw 1.6 lc rgb '#334155' title 'Peak locus ω_r = ω_0√(1-2ζ²)', $PEAKS using 1:2 with points pt 7 ps 1.05 lc rgb '#0f172a' notitle");
# Right panel: Phase Lag phi(omega / omega_0) in degrees
plot.cmd("unset arrow 1");
plot.cmd("unset label 1");
plot.script("set origin 0.54, 0.0\nset size 0.46, 1.0");
plot.script("set lmargin 8.5\nset rmargin 2.5\nset tmargin 3.2\nset bmargin 4.2");
plot.cmd("set title 'Phase Lag φ(ω/ω_0) — Quadrature at r = 1' font ',11.5'");
plot.cmd("set xlabel 'Normalized Driving Frequency r = ω / ω_0'");
plot.cmd("set ylabel 'Phase Response φ(r) (degrees)'");
plot.cmd("set xrange [0:2.2]");
plot.cmd("set yrange [-185:5]");
plot.cmd("set ytics (-180, -135, -90, -45, 0)");
plot.cmd("set key top right samplen 2.0 spacing 1.15 opaque box lc rgb '#cbd5e1' font ',9'");
plot.cmd("set arrow 2 from 1.0, -185 to 1.0, 5 nohead dt 3 lw 1.3 lc rgb '#475569'");
plot.cmd("set arrow 3 from 0.0, -90 to 2.2, -90 nohead dt 3 lw 1.3 lc rgb '#475569'");
let phase_note = "At resonance (r = 1):\nAll curves cross φ = -90°\nSlope dφ/dr = -2Q";
plot.cmd("set label 2 '" + phase_note + "' at 1.15, -52.0 left front boxed noenhanced font ',8'");
plot.cmd("phi(r, Q) = -atan2(r / Q, 1.0 - r**2) * 180.0 / pi");
plot.cmd("plot phi(x, 10.0) with lines lw 2.5 lc rgb '#dc2626' title 'Q = 10.0', phi(x, 6.0) with lines lw 2.4 lc rgb '#1d4ed8' title 'Q = 6.0', phi(x, 3.0) with lines lw 2.2 lc rgb '#d97706' title 'Q = 3.0', phi(x, 1.5) with lines lw 2.2 lc rgb '#059669' title 'Q = 1.5'");
plot.cmd("unset multiplot");
plot.close();
end _lmcode_
r = run_lmcode(p);
assert(r.ok, json_stringify(r));
println("wrote images/damped_driven_resonance.png");