Duffing Oscillator
Simulate the forced nonlinear equation \(\ddot{x} + \delta\dot{x} + \alpha x + \beta x^3 = \gamma\cos(\omega t)\) and explore hardening springs, double-well potentials, and chaos.
The Duffing Equation
The Duffing oscillator, introduced by Georg Duffing (1918), models a damped, periodically forced nonlinear spring. The second-order ODE is:
Writing \(v = \dot{x}\): the first-order system is \(\dot{x}=v\), \(\dot{v}=\gamma\cos(\omega t) - \delta v - \alpha x - \beta x^3\). The potential energy is \(V(x)=\tfrac{\alpha}{2}x^2+\tfrac{\beta}{4}x^4\).
Parameters
- \(\delta \ge 0\): Damping. Larger \(\delta\) dissipates energy faster.
- \(\alpha\): Linear stiffness. Positive = restoring; negative = destabilising.
- \(\beta\): Cubic stiffness. Positive = hardening; negative = softening.
- \(\gamma\): Forcing amplitude. Drives sustained oscillations against damping.
- \(\omega\): Forcing frequency. Period \(T_f = 2\pi/\omega\).
Regimes
- Linear (\(\beta=0,\,\alpha>0\)): Driven harmonic oscillator. Resonance at \(\omega=\sqrt{\alpha}\).
- Hardening (\(\alpha>0,\,\beta>0\)): Single-well potential. Resonance peak bends to higher \(\omega\).
- Softening (\(\alpha>0,\,\beta<0\)): Single-well potential. Resonance peak bends to lower \(\omega\).
- Double well (\(\alpha<0,\,\beta>0\)): Stable equilibria at \(x^*=\pm\sqrt{-\alpha/\beta}\) separated by an unstable saddle at \(x=0\).
- Chaotic (\(\alpha<0,\,\beta>0\), small \(\delta\), moderate \(\gamma\)): Strange attractor in phase space; trajectory visits both wells unpredictably.
Presets
The period-doubling cascade uses \(\delta=0.3,\alpha=-1,\beta=1,\omega=1.2,\,x_0=1,\,v_0=0\), with \(\gamma\) varying:
- \(\gamma=0.20\): period‑1 limit cycle in one well.
- \(\gamma=0.28\): period‑2 after the first doubling.
- \(\gamma=0.29\): period‑4.
- \(\gamma=0.37\): period‑5 window.
- \(\gamma=0.50\): chaotic strange attractor.
- \(\gamma=0.65\): period‑2 after the cascade re-coalesces.
- Hardening (strong): \(\delta=0.02,\alpha=1,\beta=5,\gamma=8,\omega=0.5\) — large-forcing frequency-response example.
- Ueda: \(\delta=0.05,\alpha=0,\beta=1,\gamma=7.5,\omega=1\) — Ueda strange attractor.
Trail Coloring
- Color by speed: blue = slow, red = fast (magnitude of \(\dot{x}\)) on both the time-series and phase portrait.
- Solid color: fixed blue trace.
- Fading trail: phase portrait only — last 3000 segments drawn with speed coloring and a linear opacity ramp (recent opaque, older fades). The displacement \(x(t)\) plot retains the speed coloring without fading.
Adaptive display scale
The sim canvas and wall limits are sized from the actual solved trajectory, so large-amplitude regimes (e.g. \(\gamma=8\)) remain drawable without clipping against the walls.
Multiple initial conditions scatter — animated zoom
When Multiple initial conditions scatter is enabled, the phase portrait starts with a wide view that guarantees every initial condition and transient arc sits inside the plot. Each trajectory has its own colour and a short fading trail (quadratic alpha curve) so only recent motion stays prominent and the plot doesn't clutter when trajectories overlap. After about one forcing period the camera smoothly zooms in (ease-in-out) until the strange attractor fills the frame. The zoom is keyed to simulation time, so pausing pauses the zoom, and the full wide→attractor sequence replays each time the animation loops at \(t=T\).
Potential Energy Panel
The lower panel plots \(V(x)=\tfrac{\alpha}{2}x^2+\tfrac{\beta}{4}x^4\). The moving dot tracks the current displacement on this curve; the dashed line connects the mass position to the curve. Minima of \(V\) mark stable equilibria of the unforced system.
Solver: RK45 (Dormand–Prince)
Adaptive order-5 Runge–Kutta. Six derivative evaluations per step; error estimated by comparing order-4 and order-5 solutions. Step is accepted when the normalised error \(\le 1\); otherwise the step is rescaled. Reliable across all regimes including chaotic transients.
Space pause / resume R reset