|
Multi-link (N-Segment) Pendulum Simulation
This interactive tutorial visualizes a multi-segment pendulum (also called an N-link pendulum): a chain of N rods, each with a mass at the end, pivoting from a fixed point. The motion is chaotic for N ≥ 2: tiny changes in initial conditions lead to completely different trajectories over time—a classic example of sensitive dependence on initial conditions (chaos theory). You can run in Rigid mode (Lagrangian, fixed rod lengths) or Elastic mode (spring–mass, Hooke’s law).
The simulation uses Lagrangian mechanics (rigid) or Hookean spring dynamics (elastic) and a 4th-order Runge–Kutta (RK4) integrator for numerical stability. You can drag any mass to set the chain; CCD inverse kinematics keeps the motion natural. A time-domain plot (θ vs t) and two state plots (phase portrait θ vs ω, and adjacent angles θi vs θi+1) show the dynamics. Rendering is done with the HTML5 Canvas API and controls with jQuery.
Mathematical foundation: Rigid vs. Elastic multi-link dynamics
The representation and equations depend on the chosen mode (Rigid Lagrangian vs. Elastic Spring-Mass).
1. State and geometry
- Rigid mode (Lagrangian): The state is defined by angles θi and angular velocities ωi. The position of each joint is a derived geometric constraint: joint i is at (Σk≤i ℓk sin θk, Σk≤i ℓk cos θk). Lengths ℓi are fixed.
- Elastic mode (Cartesian): The state is defined by the position (xi, yi) and velocity (vx,i, vy,i) of each mass. The “rod” is now a vector between joints; its length can deviate from the rest length ℓi.
2. Equations of motion
A. Rigid mode (Lagrangian mechanics)
We solve the system M · α = B.
- Mass matrix M: Mij = (Σk ≥ max(i,j) mk) · ℓi ℓj cos(θi − θj).
- Generalized forces B: For each i, Bi includes: (1) gravity: −(Σk≥i mk) g ℓi sin θi; (2) centrifugal coupling: −(Σk≥max(i,j) mk) ℓi ℓj ωj2 sin(θi − θj) for j ≠ i.
At each step we form M and B, solve for α (e.g. Gaussian elimination), then advance (θ, ω) with RK4.
B. Elastic mode (Hookean dynamics)
The force on each mass i is the sum of gravity, damping, and the tension from the springs above and below it.
- Spring force (Hooke’s law): F = −k(d − ℓi) · û, where d is the current distance between joints, ℓi is the rest length, and û is the unit vector along the segment.
- Acceleration: ai = Fi / mi (with gravity mi g downward and damping applied to velocity).
3. Numerical integration (RK4)
Whether in Rigid or Elastic mode, we use the 4th-order Runge–Kutta (RK4) method. The state vector y changes:
- Rigid: y = (θ0, …, θN−1, ω0, …, ωN−1).
- Elastic: y = (x0, y0, …, xN−1, yN−1, vx,0, vy,0, …, vx,N−1, vy,N−1).
Why RK4 is critical for elasticity: Spring systems are “stiff” differential equations. High k creates high-frequency oscillations. A simple Euler integrator can cause these to grow exponentially (the system “explodes”). RK4 samples the spring forces at four points within a single time step to cancel out these errors.
4. Energy conservation and chaos
- Rigid mode: Total energy E = T + V (kinetic + gravitational potential). Divergence in “Ghost mode” is caused by the non-linear coupling of the cos(θi − θj) terms in the mass matrix.
- Elastic mode: Total energy E = T + Vgrav + Velastic. The elastic potential Velastic = ½k Σi (di − ℓi)2 adds a new dimension to the dynamics. As k → ∞, the elastic simulation’s behavior converges toward the rigid simulation.
5. Visualization: the “stretching” effect
In Elastic mode, rods are drawn with strain ε = (d − ℓ) / ℓ: color transitions from white (rest) to bright red when stretched (ε > 0) and to bright blue when compressed (ε < 0); line width is reduced when stretched (Poisson effect) and increased when compressed.
Usage
Use this simulation to explore the multi-link pendulum and chaos:
- Number of segments (N): Slider from 1 to 10. N = 1 is a simple pendulum; N ≥ 2 shows chaotic motion. Higher N increases complexity and sensitivity.
- Gravity, Damping, Speed: Sliders adjust g, damping factor (e.g. 0.98–1.0), and animation speed (1×–5×). Damping slightly below 1 removes energy each step for stability.
- Elastic mode: Checkbox switches from rigid Lagrangian dynamics to spring–mass (Cartesian) dynamics. K slider (10–1000) sets the spring constant; high k approximates rigid behavior. When toggling Elastic ON/OFF, state is converted between (θ, ω) and (x, y, vx, vy) seamlessly.
- Drag any mass: Click and drag any bob to set the chain. CCD (Cyclic Coordinate Descent) inverse kinematics updates angles so the dragged bob follows the cursor naturally. In Elastic mode, the dragged bob’s position is set directly and its velocity zeroed.
- Trace: Toggle to draw the path of each bob with a fading trail (per-mass colors). Shows fractal-like trajectories.
- Time-domain plot: Shows angle θ vs time for each segment (last 40 s). State plots below: left = phase portrait (θ vs ω) per mass; right = adjacent angles (θi vs θi+1) or θ vs ω for N = 1.
- Ghost mode (chaos comparison): When enabled, a second “shadow” pendulum is cloned with a tiny angle offset (e.g. 0.001 rad) on the first link. Both are integrated with the same dt. Initially they overlap; after a few seconds they diverge. A Divergence meter shows how far they have drifted (rigid mode only).
- Step Fwd / Step Bwd / Run–Stop: Step advances or rewinds the simulation by one time step (with hold-to-repeat). Run–Stop toggles continuous animation.
- Reset: Resets state to default (or current dragged configuration) and clears the trace and time history.
Tips:
- Chaos: With N ≥ 2, use Ghost mode to see two nearly identical pendulums diverge—the “butterfly effect” in a deterministic system.
- Speed slider: Use 1×–5× to slow down or speed up the animation without changing physics.
- Phase space (θ vs ω): The left state plot shows non-periodic, tangled trajectories in chaotic regimes; colors match the mass legend.
- Elastic “jelly”: Low k gives soft, stretchy motion; high k (e.g. 1000) approximates the rigid pendulum. Rod color (red = stretched, blue = compressed, white = rest) and line thickness show strain.
Visualizations
- Canvas animation: Ball-and-rod style: pivot at the top, segments as lines, bobs as circles (per-mass colors). In Elastic mode, rod color and thickness show strain ε = (d − ℓ)/ℓ: white at rest, red when stretched, blue when compressed; line thinner when stretched (Poisson effect), thicker when compressed. The dragged bob is highlighted (white glow).
- Trace: When enabled, each bob’s trajectory is stored in a circular buffer and drawn with per-mass colors and fading opacity (oldest faint, newest bright).
- Time-domain plot: Angle θ (rad) vs time (s) for each segment over the last 40 s; one curve per mass, colors match the legend.
- State plots (left and right): Left = phase portrait (θ vs ω) for each mass. Right = adjacent angles (θi vs θi+1) for N ≥ 2, or θ vs ω for N = 1. Multiple traces when N ≥ 2.
- Ghost mode: Second pendulum rendered with lower opacity and a distinct color (rigid mode only). Both are stepped with the same physics; a divergence meter shows how far the end bobs have drifted.
- Divergence meter: Progress bar (0–100%) showing the Euclidean distance between the main and ghost end-effector positions when Ghost mode is on.
Controls
- N (segments) slider: 1–10. Changing N reinitializes the pendulum (angles/velocities or Cartesian state).
- Gravity slider: Sets g (e.g. 0–20). Affects swing speed and period.
- Damping slider: Multiplier applied to velocities each step (e.g. 0.98–1.0). Values < 1 gradually remove energy.
- Speed slider: Animation speed 1×–5× (default 3×). Scales the time step per frame when Run is on.
- Elastic checkbox: Switches between rigid (Lagrangian) and elastic (spring–mass) dynamics. State is converted between (θ, ω) and (x, y, vx, vy) when toggling.
- K slider: Spring constant (10–1000, default 200) in Elastic mode. Higher k stiffer rods; very high k approximates rigid behavior.
- Trace and Ghost checkboxes: Trace toggles per-bob trails. Ghost toggles the second pendulum and divergence meter (rigid mode).
- Step Bwd / Run–Stop / Step Fwd: Step Bwd restores the previous state (undo). Run–Stop toggles continuous animation. Step Fwd advances one time step. Hold Step Fwd/Bwd for repeated stepping.
- Reset button: Restores initial state and clears trace and time/state history.
- Drag: Click and drag any bob. CCD inverse kinematics (rigid) or direct position update (elastic) keeps the chain following the cursor.
Code structure (reference)
The simulation is organized for clarity and performance:
- State: Rigid:
theta, omega, lengths, masses. Elastic: bobX, bobY, bobVx, bobVy, restLengths. Conversion: thetaToCartesian() and cartesianToTheta() when toggling Elastic; syncThetaOmegaFromCartesian() after each elastic step for plots.
- Solver: Rigid: Builds M and B from the Lagrangian, solves M·α = B (Gaussian elimination), advances (θ, ω) with RK4. Elastic:
getElasticAccelerations() (Hooke’s law, gravity, per bob), rk4StepElastic() advances (x, y, vx, vy). Damping applied to velocities; animation speed scales dt when Run is on.
- Drag (IK):
hitTestBob() finds which bob is under the cursor (canvas coordinates scaled for resolution). Rigid: CCD loop (10 iterations) from dragged bob back to pivot updates θ so the end-effector follows the target; then ω zeroed. Elastic: set bob position to target and zero its velocity.
- Renderer:
drawPendulum(): rigid = rods from θ and lengths; elastic = rods from bobX, bobY with strain-based color (red/blue/white) and line width. Trace, time-domain plot, and two state plots drawn each frame. Ghost and divergence meter in rigid mode.
Educational insights
Ghost mode (rigid):
- Lead time: For the first few seconds, the main and ghost pendulums overlap—the system is locally predictable.
- Breakpoint: After a short while, the ghost suddenly follows a completely different path—small initial difference leads to large later difference (chaos).
- Energy: Even when positions diverge, total energy (KE + PE) of both systems remains roughly equal (and slowly decreases with damping), showing the physics is correct and the divergence is due to sensitivity, not numerical error.
Elastic vs rigid: In Elastic mode, rods can stretch and compress. Strain visualization (red = stretched, blue = compressed) and line thickness (Poisson effect) help students see the elastic potential. As the spring constant k is increased, the motion approaches the rigid pendulum—connecting Lagrangian constraints to the limit of very stiff springs.
State plots: The phase portrait (θ vs ω) and adjacent-angle plot (θi vs θi+1) reveal chaotic, non-periodic trajectories and the structure of the system’s configuration space.
|
|