loading...
running...
The science
What is plotted. A magnetic field line is a curve that is everywhere tangent to the field B. The lines never cross, they show the direction of the force on a small north pole, and the local density of lines tells the strength. Every line either closes on itself or runs from a north pole to a south pole.
1. Bar magnet (magnetic-charge model). Outside the metal, a bar magnet behaves like a north pole +q and a south pole -q a distance 2H apart. Each pole produces the Coulomb-like field (units with μ0/4π = 1):
B(r) = q (r - rN)/|r - rN|3 - q (r - rS)/|r - rS|3
Gauss's law for magnetism, ∇·B = 0, is not violated: the flux "out of" the north pole is exactly the flux that returns into the south pole, so the net flux through any closed surface is zero. Far away the two poles merge into a dipole with moment m = 2qH and B falls as 1/r3 (axis value is twice the equator value). Inside a real magnet the field is different (B is continuous and its lines close through the iron); the pole model is only valid outside.
2. Current loop (Biot-Savart). A wire carrying current I creates, element by element:
B(r) = (μ0/4π) I ∮ dl × (r - r')/|r - r'|3, on axis: B = μ0 I R2 / [2 (R2 + z2)3/2]
The loop is summed numerically from 48 straight segments. Reverse the current and every arrow reverses (right-hand rule: curl your fingers along I, the thumb points along the axis). Ampere's law says the circulation of B along any circle linking one wire is μ0 I.
3. Superposition and the compass. Fields add as vectors, so two magnets, a loop and a uniform background field (the Earth, about 50 μT, is simply a constant arrow here) are summed point by point. A compass needle is a small magnet that feels a torque -μB sin(θ): it obeys θ'' = -(μB/I) sin(θ - θB) - c θ', so it swings, rings down and settles along B. Click or tap to place it. Iron filings do the same thing in bulk: each grain becomes a tiny magnet and lines up with the local field, drawing the pattern.
4. How lines are drawn. Field lines are integrated with fourth-order Runge-Kutta along the unit field direction dr/ds = B/|B| in steps of 0.06 tiles, starting on small rings around every north pole, around the wire crossings and along the left edge when a uniform field is on, and stopped at a south pole, at the edge of the view, or when the line closes.
Checks (run in the page). (1) flux of B through a sphere around a north pole against 4πq (Gauss, numerical quadrature of 3000 points); (2) the 48-segment Biot-Savart sum against the closed-form axis field at four points; (3) the far-field axis/equator ratio of a bar magnet against 2; (4) the circulation of B around one wire crossing against 4πI.
Simplifications. Linear superposition only: there is no induced magnetisation, so a magnet does not bend its neighbour's poles and no iron or hysteresis is modelled. The loop is one turn of thin wire (the field diverges at the wire, softened by a tiny constant). Units are scaled (μ0/4π = 1, lengths in tiles), so field values are relative, not teslas.