From Biot-Savart to a Playable Game by Deriving and Validating a Helmholtz Coil Pair in MATLAB

A formula driven walkthrough of HELMHOLTZ HERO: closed-form on axis proofs, numerical Biot-Savart, an RMS uniformity score, and a Boris integrated charged probe
Introduction
A Helmholtz pair is two identical circular coils on a common axis. They sit one coil radius apart and carry equal current in the same direction, which makes the magnetic field between them nearly uniform. HELMHOLTZ HERO, my entry for the MATLAB One Button Game Mini Hack, turns Wikipedia,"Helmholtz coil" fact into a game. You lock the coil seperation with one tap and the current ratio with another, and then a charged probe flies through your field. Your score is how uniform you made it. This post shows where each number in the game comes from, step by step.
Demo
Animated Demonstration
The physics in plain English
A single current loop makes a field that is strongest at its center and fades along its axis. Put a second loop beside it and the two fields add. If the loops are too close, the field bulges in the middle. If they are too far apart, it dips in the middle. At exactly one radius apart, the bulge and the dip cancel, and the field is flat to a very high order. In the game, you search for that spot with one tap, then try to balance the two currents with the next.
Tips & Tricks
Trick 1:Start from one formula you can start, then test your code against it
A single loop has a simple field along its axis. It falls off as one over (1+z^2) to the power 3/2, in units of the loop's center field. My Biot-Savart code, which sums the field from 96 pieces of each loop, matches that formula to about 2x10^-16. Checking the numerics against a closed form before drawing anything is the single best habit for a physics project. Two loops are just two shifted copies of the same curve, and the code agrees with that sum to the same precision.
Trick 2: Let calculus explain why the coils sit one radius apart
The field of the pair, slid to the middle,is the sum of two single loop curves shift by half the spacing. Its second derivative at the center is proportional to (d^2-1), where d is the spacing in radii, so it vanishes exactly at d=1. That matches the textbook argument that a single coil's on axis field has its inflection point at R/2 from its center, so the coils belong at +/-R/2. At d=1, the center field is 16 squareroot of 5/25, or about 1.4311 times the field of one loop's center. Along the axis, the field stays within 1% of that value out to about 0.31 radii.
Trick 3: Know where your numerical method is trustworthy
The exact field away from the axis is hard: it involves Bessel functions. The game avoids them by averaging the integrand over 96 angles. That is extremely accurate for a periodic integrand, and my tests show differences of about 10^-16 at 0.6 radii from the axis and 10^-9 at 0.9. But very close to the wire (0.05 radii away) the 96 point version is off by about 2x10^-2. That is why the game scores only a small window around the center,0.6 by 0.6 radii. Inside that window, it computes the RMS deviation of the field strength from its center value. The ideal pair scores 0.533% RMS, which maps to a round score of 998 out of 1000.
Trick 4: Move the probe with a royation, not an Euler update
Magnetic fields do no work, so a particle's speed should never change. The Boris method updates the velocity with two cross products. This is the standard rotation step: u^+ = u^- + (u^- + u^- * t) * s, with s=2ft/(1+t^2). It is an exact rotation about the field direction by an angle of 2*arctan|t|, which preserves the speed. Over a full flight in the game, the speed drifted by only about 10^-15. The game then shows three behaviors:
  • At d=R, the probe passes through.
  • At d=0.4R it is reflected.
  • With the second current 1.5 times the first, it is trapped, because the field is strong at the ends and weak in the middle. That is a magnetic bottle: the coils sit farther apart so the field expands in the middle, trapping charged particles.
Trick 5: See the whole game in one 3D plot
Plot the score over the whole range of spacing and current ratio, and you get one sharp peak at d=R with equal currents. That single picture explains the game: every tap moves you across this landscape. The live script draws it, along with a surface showing how the on axis field flattens as the spacing nears one radius. For smooth play, build every graphics object once and only update its data each frame.
Live script: the derivation, the code, and 3D plots
run('HelmholtzHero.m');
ans = 8×3 table
Symbol Meaning InGame _______________ _________________________________ ____________________ {'R' } {'coil radius (= 1)' } {'implicit 1' } {'B0' } {'mu0*I1/(2R)' } {'implicit 1' } {'d' } {'coil separation / R' } {'s.d' } {'I2/I1' } {'current ratio' } {'s.r' } {'z' } {'axial position' } {'z argument of bf'} {'rho' } {'distance from the axis' } {'x argument of bf'} {'bf(x,z,d,r)'} {'two-loop Biot-Savart field' } {'function bf' } {'rms' } {'uniformity error (7x7 window)'} {'s.rms in field()'}
ans = 5×4 table
z_over_R bf_numeric closed_form abs_error ________ __________ ___________ __________ 0 1 1 0 0.25 0.91308 0.91308 1.1102e-16 0.5 0.71554 0.71554 0 1 0.35355 0.35355 1.6653e-16 2 0.089443 0.089443 0
ans = 5×3 table
z_over_R bf_numeric sum_of_two_g ________ __________ ____________ -1 1.0091 1.0091 -0.3 1.154 1.154 0 1.0996 1.0996 0.4 1.1831 1.1831 1.2 0.81657 0.81657
ans = 5×3 table
d_over_R numeric_Bpp formula_Bpp ________ ___________ ___________ 0.6 -2.8401 -2.8401 0.8 -1.2848 -1.2848 1 -3.2974e-06 0 1.2 0.89995 0.89995 1.6 1.657 1.657
ans = 1×3 table
numeric formula abs_error _______ _______ __________ 1.4311 1.4311 2.2204e-16
ans = 4×3 table
z_over_R exact_percent quartic_percent ________ _____________ _______________ 0.1 0.011394 0.01152 0.2 0.17635 0.18432 0.3 0.84397 0.93312 0.4 2.4624 2.9491
ans = 3×5 table
rho zeta Bz_96 Bz_4000 abs_diff ____ ____ ______ _______ __________ 0.6 0.4 0.8068 0.8068 3.3307e-16 0.9 0.2 1.1001 1.1001 9.6122e-10 0.95 0.05 3.8563 3.8768 0.020523
ans = 7×4 table
d_over_R I2_over_I1 rms_percent score ________ __________ ___________ _____ 1 1 0.53296 998 0.8 1 1.7914 350 1.2 1 1.66 390 1 0.8 2.6942 165 1 1.2 2.2256 243 0.4 1 4.9981 24 1 1.5 4.7829 29
ans = 1×4 table
speed_change parallel_change rotation_angle two_atan_norm_t ____________ _______________ ______________ _______________ 0 -1.5613e-17 0.046295 0.046295
ans = 3×4 table
d_over_R I2_over_I1 outcome max_speed_drift ________ __________ _____________________ _______________ 1 1 {'TRANSMITTED' } 8.8818e-16 0.4 1 {'REFLECTED' } 7.7716e-16 1 1.5 {'TRAPPED IN BOTTLE'} 6.6613e-16
ans = 8×3 table
Step Check Result __________________________ _________________________________ ________________________________ {'Single loop' } {'bf vs closed form' } {'max error 1.7e-16' } {'Superposition' } {'bf vs two shifted loops' } {'max error 4.4e-16' } {'Why d = R' } {'B'' vs 768(d^2-1)/(d^2+4)^3.5'} {'max difference 3.3e-06' } {'Centre field' } {'bf vs 2(4/5)^1.5' } {'error 2.2e-16' } {'Flatness' } {'exact vs quartic' } {'1% deviation near z = 0.31 R'} {'Biot-Savart quadrature'} {'96 vs 4000 angles' } {'max difference 2.1e-02' } {'Score' } {'rms at d = R' } {'0.533%, score 998' } {'Boris probe' } {'speed drift' } {'max drift 8.9e-16' }
Concluding thoughts
HELMHOLTZ HERO shows that a game can also be a verified physics experiment. A closed form on axis formula explains why the coils belong one radius apart, a numerical Biot-Savart sum extends the field off the axis, and an RMS window turns "uniform" into a score. A Boris rotation then moves a charged probe through the result without losing energy. The limits are worth stating: the 96 point integration is only trustworthy away from the wire, the score uses a fixed window, and the probe's step is fixed, not tied to frame time. Next experiments could reverse one coil to make an anti Helmholtz pair. That arrangement gives a nearly uniform field gradient and is used for magnetic traps.
References
  1. Wikipedia, "Helmholtz coil"
  2. HyperPhysics, "Magnetic Field of a Current Loop"
  3. J. P. Boris and the standard Boris rotation, as given in "A comprehensive comparison of relativistic particle integrators" (arXiv:1710.09164)