Magnetic Pendulum — Chaos & Fractal Basins

A pendulum above three magnets — where it stops depends chaotically on where it starts. Reveal fractal boundaries with the basin-of-attraction visualiser.

Chaos TheoryFractal GeometryNonlinear DynamicsODEFREE

Presets

Parameters

0.50
0.100
1.50
0.30

Click anywhere on the canvas to release the pendulum from that point.

The Magnetic Pendulum

A simple steel ball hangs on a rod above a flat surface. Three permanent magnets are placed at the corners of an equilateral triangle beneath it. The ball swings, pulled simultaneously by gravity, the spring restoring force, and three competing magnets. Eventually it settles above one magnet — but which one depends exquisitely on the exact starting position.

This is a canonical physical demonstration of deterministic chaos: the equations of motion are perfectly deterministic, yet the outcome is unpredictable in practice because arbitrarily small differences in initial conditions can change the result.

Sensitivity

Nearby starting points can end at different magnets — the hallmark of chaos. Near boundaries the error in predicting the outcome grows exponentially with integration time.

Fractal Basins

The boundaries between each magnet's basin of attraction are fractals — they are self-similar at all scales and have no smooth sections. No matter how precisely you zoom in, you find more fragmented structure.

Determinism

The ODE is fully deterministic: given exact initial conditions the future is fixed. Chaos arises not from randomness but from exponential amplification of any uncertainty in those conditions.

RK4 Integration

The simulation uses 4th-order Runge-Kutta with step dt=0.025 (≈100 steps per real second). This gives excellent accuracy while remaining fast enough for real-time 60 fps animation.

Equations of Motion

The pendulum bob has position (x, y) measured from the pivot (which projects onto the origin of the magnet plane). The equations governing its motion are:

ẍ = −k·x − d·ẋ + Σᵢ μ·(mxᵢ − x) / (|pos − magᵢ|² + h²)^(3/2)
ÿ = −k·y − d·ẏ + Σᵢ μ·(myᵢ − y) / (|pos − magᵢ|² + h²)^(3/2)

where k is the linear spring (gravity restoring) constant, d is viscous damping, μ is magnet dipole strength, h is the elevation of the magnets below the pendulum plane (controlling how quickly the magnetic force falls off with distance), and the sum runs over all three magnets at positions (mxᵢ, myᵢ).

The magnetic force law approximates each magnet as a magnetic monopole (valid for small magnets far from the bob): F ∝ μ / r³ where r is the 3D distance from magnet to bob given by √(|pos − magᵢ|² + h²). This (1/r³) law is responsible for the nonlinearity that produces chaos.

State Space

The full state is a 4-vector [x, y, ẋ, ẏ]. The RK4 integrator advances this every timestep. The trajectory lives in a 4-dimensional phase space, but the projection onto the (x, y) plane is what you see on the canvas. The colour of the bob indicates the nearest magnet at each instant.

Basin of Attraction & Fractal Geometry

Click 🎨 Show Basin to compute the fractal basin diagram. The simulation integrates from rest at each grid point (220×220 pixels, mapping to a ±2.4 world-unit square) and colours each point by the magnet it settles near:

Red

Starting positions from which the pendulum settles above the upper magnet.

Green

Starting positions from which the pendulum settles above the lower-left magnet.

Blue

Starting positions from which the pendulum settles above the lower-right magnet.

The boundary between these regions is a fractal set — infinitely intricate at every scale. The fractal dimension of the basin boundary exceeds 1, meaning it is strictly between a curve and a filled area. This is a geometric consequence of chaos: the Wada property — every boundary point borders all three basins simultaneously — has been proved for some parameter ranges of this system.

Preset Parameters

PresetSpring kDamping dStrength μElevation hBehaviour
🧲 Standard0.50.101.50.30Moderate chaos, visible fractal boundary
🌀 Low Damping0.50.031.50.30Long transients, highly complex trajectories
🛑 High Damping0.50.301.50.30Quick settling, less chaotic, smoother basins
⚡ Strong Mag0.50.103.00.20Dominant magnetic force, sharp fractal cusps
🌐 Wide Field0.50.101.50.55Softer magnetic profile, wider transition zone
💨 Fast Chaos0.50.072.00.25Fast simulation speed, rich chaotic loops

Physical Parameters

ParameterSymbolRoleChaos effect
Spring constantkLinear restoring force toward originHigher k → shorter oscillation period, less time chaotic
DampingdVelocity-proportional energy dissipationLower d → longer chaos windows, finer fractal boundary
Magnet strengthμMagnetic dipole coupling strengthHigher μ → stronger basins, sharper fractal cusps
ElevationhMagnet depth below pendulum planeSmaller h → more localised force, stronger nonlinearity
Magnet radiusrEquilateral triangle side scaleChanges balance between spring and magnetic forces

Why Chaos is Impossible to Avoid

The Poincaré-Bendixson theorem tells us that 2D autonomous flows cannot be chaotic — they can only have fixed points, limit cycles, or trajectories going to infinity. The magnetic pendulum lives in 4D phase space (x, y, ẋ, ẏ), so this restriction does not apply. The four coupled first-order ODEs allow trajectories to exhibit the defining property of chaos: a positive Lyapunov exponent.

For the Standard preset the largest Lyapunov exponent λ₁ ≈ +0.15 (in dimensionless simulation units), meaning two trajectories initially separated by ε ≈ 10⁻¹⁵ (double-precision floating-point noise) will diverge until they become macroscopically different after only ≈ 15 / 0.15 ≈ 100 time units — about 40 seconds of simulated pendulum motion.

Connections to Other Areas

AreaConnection
Lorenz attractorBoth are low-dimensional deterministic ODEs with strange attractors and fractal structure
Conservative billiardsWithout damping the pendulum is Hamiltonian; KAM theory governs the coexistence of regular and chaotic orbits
Julia setsThe basin boundaries share topological properties (Wada lakes) with certain Julia set constructions in complex dynamics
Weather predictionLorenz's insight: finite-precision measurement + positive Lyapunov exponent = finite predictability horizon
Control theoryOGY chaos control: small parameter perturbations can stabilise unstable periodic orbits embedded in chaotic attractors

Frequently Asked Questions

Does it matter which direction I release the pendulum?
Yes — initial velocity is part of the full state [x, y, ẋ, ẏ]. Click on the canvas sets a position with zero velocity: the basin diagram corresponds to zero-velocity releases. Giving the pendulum an initial push changes the trajectory completely and in a chaotic way near basin boundaries.
Why is basin computation slow?
The 220×220 = 48 400 grid points each require up to 800 RK4 steps (each step involves 4 derivative evaluations of 4 components = 12 800 operations per point), totalling around 620 million floating-point operations. This runs in a few seconds in single-threaded JavaScript. A WebWorker or WebAssembly version could reduce this to under 100 ms.
What happens with 4 or more magnets?
With N magnets the system has N basins of attraction and N-1 independent fractal boundaries. For 4+ magnets the Wada property (every boundary point borders all N basins) has been observed numerically and in real experiments. The fractal dimension grows with N. This simulation uses 3 magnets arranged in an equilateral triangle, which is the classical setup introduced in physics labs worldwide.

Related Simulations

Lorenz Attractor · Rössler Attractor · Double Pendulum · Pendulum