Physics simulations
Chaos in a Magnetic Pendulum
- Chaos
- Lyapunov exponent
- RK4
In simple words
Hang a metal ball on a string above three magnets, pull it to one side and let go. It swings, gets tugged this way and that, and in the end it stops over one of the magnets.
Which one? Nothing here is random. The ball follows exact rules, so if you know where you let go, you should be able to say where it ends up. Right? Try to guess before you look. That simple question has a very surprising answer.
Where it comes from
Originally a study paper by Hosein (Mohammadhossein) Gholami for the course Complex Systems Studies, taught by Prof. Afshin Montakhab, Shiraz University, summer 2016.
This simulation began as my study paper during my bachelor's degree in physics at Shiraz University, written in the summer of 2016 for Prof. Afshin Montakhab's course on complex systems. The first version was a Mathematica notebook that integrated the motion and estimated how quickly nearby trajectories drift apart. It was later rewritten for the browser, and in the process one flaw of the original model came to light. In 2016 the gap between the bob and the magnets changed with the bob's height on the string, but the force that this height dependence implies was left out. That force was not conservative, so the energy drifted even without friction. The version here uses a clean planar model whose energy is conserved exactly when friction is off, and the default parameters still follow the original notebook.
The model
The bob hangs on a string of length above magnets placed evenly on a circle of radius , at the positions
with a single magnet sitting at the centre when . Because the swing is small compared with the string, the bob is taken to move in a horizontal plane at a constant height above the magnets. Its distance to magnet is then , and the equation of motion is
The first term is gravity pulling the bob back to the centre, the second the attraction of the magnets, which falls off with the square of the distance, and the third a linear friction with coefficient . The factor gives the small-angle restoring force, and the option keeps the exact horizontal part of gravity along the arc. With the two differ by less than 0.1 percent at . Both forces derive from a potential,
so the energy can only decrease, at exactly the rate the friction removes it,
The simulation integrates the motion with the classical fourth-order Runge-Kutta method and a fixed step of 0.0025 s, on a physics clock that does not depend on the speed setting or on your screen. Without friction the energy stays constant to about one part in a million over five minutes of simulated time. The defaults are , , , , , and .
Why it is chaotic
It helps to start with a case that is not chaotic. With a single magnet at the centre, or with a ring of infinitely many magnets, the pull is the same in every direction. Without friction the angular momentum about the centre is then conserved, and together with the energy it pins the motion down completely: the bob traces a rosette that can be computed for all time, and friction only makes it spiral in. Chaos needs this rotational symmetry to be broken, and a few separate magnets do exactly that. Each time the bob passes close to a magnet it is flung off in a direction that depends sharply on how it came in, and these small differences compound from one pass to the next. My 2016 report found the same thing numerically: with many magnets on a small circle the separations between nearby trajectories never kept growing, while with three or more well separated magnets they did.
Friction gives the story an end. Every bob eventually loses enough energy to be trapped over one magnet, so the chaos here is transient: it lives in the wandering before capture. Motter, Gruiz, Károlyi and Tél studied exactly this situation, with the magnetic pendulum as their central example, and named it doubly transient chaos [3]. What survives the capture is the dependence of the final magnet on the starting point. Starting points that lead to different magnets are separated by intricately folded boundaries. In many chaotic systems such boundaries are truly fractal, and near them no finite precision is enough to predict the outcome [2]. Here the friction stops the folding at a finite depth. Motter and co-workers showed that under enough magnification the boundaries of the magnetic pendulum become smooth, with a whole-number dimension, so the unpredictability is real but confined to a band of scales [3].
The three bobs released at the start make this visible. They begin about a hundredth apart, at
and they end over three different magnets after about twenty seconds. These points are the triangle of the original notebook, moved by , because with the corrected model the original points end on only two magnets. A test checks this outcome, stepping exactly as the animation does, so that a future change to the numerics cannot quietly break it.
Measuring the divergence
The standard measure of chaos is the maximal Lyapunov exponent, the long-time rate at which two infinitely close trajectories separate,
A positive value means exponential divergence. Computing it properly requires following infinitesimal separations and renormalizing them as they grow [4], see also Ref. [1]. The simulation uses a simpler finite-time indicator built from the three bobs. With the pair separations it plots
together with the growth factor and . The first is a running average of the growth rate since the start, not an instantaneous rate. While the bobs wander, climbs with a steady slope, which is roughly the exponent, and reaches tens or hundreds. Because the separations are finite and never renormalized, they stop growing once they reach the size of the region the bobs explore or once the bobs are captured. From then on flattens and decays towards zero like . Without friction there is no capture, and the separations grow until they fill the accessible region and then fluctuate there. With strong friction the bobs are captured before they can diverge, and turns negative as they converge on the same magnet. The chart keeps the most recent 300 s.
The basin map
The basin map answers the question for every starting point at once. Each of its pixels, about 25,000 of them filling the picture, is a bob released at rest from that point. Its colour is the magnet it ends over, and darker shades mean it wandered longer before it was caught. The map is computed in the background by several Web Workers in parallel, with a step that shrinks near the magnets, and it is available for up to six magnets with friction switched on. Large calm regions of one colour surround each magnet, and between them the colours interleave in finer and finer bands, until, at magnifications far beyond this map, the friction smooths them out. Clicking any pixel releases three bobs there.
Inside the code
The force follows the equation of motion line by line. The height enters only through the distance , and it keeps the force finite even directly above a magnet.
// s = [x, y, vx, vy]. Gravity pulls back to the centre, friction slows
// the bob, and each magnet pulls with k / rho^2 along the 3D direction.
function deriv(t, s, out) {
const [x, y, vx, vy] = s;
const G = nonlinear ? Math.sqrt(1 - (x*x + y*y) / ell2) : 1;
let ax = -w2 * G * x - fc * vx;
let ay = -w2 * G * y - fc * vy;
for (let j = 0; j < N; j++) {
const dx = mx[j] - x, dy = my[j] - y;
const q = dx*dx + dy*dy + d2; // rho^2, with the height d
const inv = k / (q * Math.sqrt(q)); // k / rho^3
ax += dx * inv;
ay += dy * inv;
}
out[0] = vx; out[1] = vy; out[2] = ax; out[3] = ay;
}The basin map would be slow if every bob had to be followed until it stopped. The trick is an energy test. The lowest point on the ridges between neighbouring magnets is computed once, and as soon as a bob's energy drops below it, the bob can never cross into another magnet's sector, so its fate is decided long before it comes to rest.
// The lowest point on the ridges between neighbouring magnets, found
// once per parameter set. Below this energy a bob is trapped for good.
let barrier = Infinity;
for (let n = 0; n <= 4000; n++) {
const s = (n / 4000) * smax;
barrier = Math.min(barrier, potential(s * Math.cos(Math.PI / N), s * Math.sin(Math.PI / N)));
}
// While classifying a pixel of the basin map: stop as soon as it is decided.
if (energy(st) < barrier) return { magnet: sectorOf(st[0], st[1]), time: t };Close to a magnet the force changes quickly, and a fixed step would either waste time far away or lose accuracy nearby. The basin integrator therefore chooses its step from the distance to the nearest magnet, in the same way the three-body simulation chooses it from the closest pair.
// Near a magnet the force changes quickly, so the step shrinks there:
// h = min(hmax, c rho^(3/2) / sqrt(k)), rho = distance to the nearest magnet.
const rho = Math.sqrt(qmin + d2);
const h = Math.min(hmax, c * rho * Math.sqrt(rho) / Math.sqrt(k));
rk4(h);The divergence indicator is sampled every twentieth of a simulated second.
// Every 0.05 s of simulated time: the mean log growth of the three pair
// separations since the start, and the log of their mean.
function sampleLyap() {
const d = pairDists();
let sumLog = 0;
for (let i = 0; i < 3; i++) sumLog += Math.log(d[i] / d0[i]);
const lambda = sumLog / 3 / simTime;
const L = (d[0] + d[1] + d[2]) / 3;
lyap.push(simTime, lambda, Math.log(L));
}Try it yourself
Click inside a large region of one colour on the basin map and the three bobs travel together to the same magnet. Click on a boundary between colours and they part ways. Set the friction to zero and the bobs never settle, wandering forever over the magnets. Switch to a single magnet, or to a hundred magnets on a small circle, and the chaos disappears, as the symmetry argument predicts. The spread slider sets how far apart a new triple starts, and it is a good way to feel how small a difference is enough to change the outcome.
References
- [1] C. Gros, Complex and Adaptive Dynamical Systems: A Primer, 4th ed. (Springer, 2015).
- [2] S. W. McDonald, C. Grebogi, E. Ott and J. A. Yorke, Fractal basin boundaries, Physica D 17, 125 (1985).
- [3] A. E. Motter, M. Gruiz, G. Károlyi and T. Tél, Doubly transient chaos: generic form of chaos in autonomous dissipative systems, Phys. Rev. Lett. 111, 194101 (2013).
- [4] G. Benettin, L. Galgani, A. Giorgilli and J.-M. Strelcyn, Lyapunov characteristic exponents for smooth dynamical systems and for Hamiltonian systems, Meccanica 15, 9 (1980).