Solenoid Magnetic Field Simulator

JJ Ben-Joseph headshot JJ Ben-Joseph

Enter values and press Play.
Simulation summary will appear here.

1. Real‑world motivation

Coils of wire may appear mundane, yet they lie at the heart of motors, transformers, and magnetic resonance imaging scanners. When electric current threads the turns, a magnetic field blossoms inside the cylinder, storing energy and producing forces that can lift cars or flip atomic spins. Most classroom tools reduce this phenomenon to a single equation for field strength. The simulator above preserves that calculation but also animates the invisible field. Little tracers drift along the axis while a striped bar grows in proportion to the magnetic energy density. By watching the field build as current ramps, students perceive electromagnetism as a dynamic process rather than a static number. The canvas updates instantly when parameters change, inviting experimentation with different materials, geometries, and time steps.

2. Variables and assumptions

The model treats the solenoid as long and tightly wound so that the interior field is uniform and the exterior field negligible. Four user inputs characterize the coil: number of turns N, current I, length L, and relative permeability μr of the core. The canvas represents a one‑meter‑long window regardless of actual length and scales tracer speed to the computed field. Air occupies the surrounding space, and fringe fields at the ends are ignored. SI units are required throughout; the script validates that all entries are finite and positive. A time step Δt between 0.001 and 0.1 s controls the numerical integration of the current ramp and tracer motion.

3. Governing equations

For an ideal solenoid, Ampère’s law yields the axial magnetic field

B=μ0μrNLI

with μ04π×10-7 T·m/A. The magnetic energy density stored in the field is

u=B22μ0μr

The simulator imagines the current rising toward the target value following a simple first‑order differential equation reminiscent of an RL circuit:

dIdt=Itarget-Iτ

with time constant τ=0.5 s. The explicit Euler method updates the current each step, and the magnetic field and energy density follow immediately from the algebra above. Because Euler is only first order, the energy bar doubles as a diagnostic: if Δt is too large, the bar overshoots the theoretical maximum, revealing numerical error.

4. Numerical scheme

At the heart of the animation is a straightforward integration loop. During each frame the code computes new values according to

In+1=In+ΔtItarget-Inτ

Tracer particles positioned inside the coil move upward at a speed proportional to the instantaneous field strength: v=kB, with k chosen so motion remains visible. Their vertical positions update using the same Euler step yn+1=yn-vΔt. When a particle exits the top of the coil it reenters at the bottom, creating the impression of continuous flow. The simulation logs t, B, and u every frame so the CSV export mirrors exactly what the user sees.

5. Worked example

Consider a copper coil 0.3 m long with 200 turns carrying 5 A of current around an air core (μr=1). The target field computed from Ampère’s law is B=μ0NIL=4π×10-7×200×5/0.3, or 0.0042 T. The associated energy density is u=B22μ0μr7.0×10-3 J/m³. Pressing Play causes the current to rise toward 5 A over roughly one second. The tracer speeds increase in tandem, and the energy bar grows until it occupies about 70 % of its maximum width, matching the calculated density. If you halve the time step to 0.01 s, the peak energy comes closer to the theoretical value, demonstrating improved numerical stability.

6. Comparison table

The table contrasts the baseline coil above with two variations: doubling the turns and inserting a ferromagnetic core with μr=500. All other parameters remain the same, and values derive from the simulation data.

Scenario N μr B (T) u (J/m³)
Baseline 200 1 4.2×10−3 7.0×10−3
Double turns 400 1 8.4×10−3 2.8×10−2
Iron core 200 500 2.1 1.7×106

The iron core case highlights how permeability magnifies both field strength and stored energy by many orders of magnitude, explaining why transformers and inductors rely on high‑μ materials.

7. How to read the animation

The canvas depicts a side view of the solenoid. A gray rectangle marks the coil’s interior. Blue dashed tracers circulate upward, symbolizing field lines. Their speed encodes the field magnitude, while the striped blue bar beneath the canvas displays magnetic energy density as a percentage of the maximum attainable with the current inputs. The caption narrates the current, field, and energy at each moment so that screen‑reader users receive equivalent information. Keyboard users can focus the canvas and press the space bar to toggle play and pause. When the current reaches steady state, tracers move uniformly and the caption reports the final field, signaling that the system has equilibrated.

8. Limitations

The simulator assumes a perfectly long solenoid and neglects fringe fields, resistive heating, and core saturation. Real coils have finite diameter, so the field may vary across the interior, and high currents can push ferromagnetic cores into nonlinear regimes where μr drops. The current ramp model ignores inductance and power supply limitations. Numerically, the explicit Euler method can overshoot if Δt is too large; choose smaller steps to reduce error. Finally, energy density is computed per unit volume without specifying the coil’s cross‑sectional area, so total stored energy is not provided.

9. Suggested extensions

Future versions could model finite‑length effects by integrating the Biot–Savart law, display magnetic field lines in three dimensions, or couple the solenoid to a moving iron core to simulate an electromagnet lifting a weight. Adding resistance and inductance parameters would permit a more realistic RL circuit model with exponential current rise and decay. Users might also explore the Magnetic Field of a Circular Loop Calculator, estimate straight‑wire fields with the Straight Wire Field Calculator, or experiment with charged particle motion in the Lorentz Force Simulator.

10. References

Key resources include D. J. Griffiths’ Introduction to Electrodynamics for a thorough derivation of the solenoid field and J. Kraus’ Electromagnetics for practical coil design. The National High Magnetic Field Laboratory publishes data on material permeabilities and field limits. These references provide deeper context for the equations and assumptions employed here.

Embed this calculator

Copy and paste the HTML below to add the Solenoid Magnetic Field - Interactive Simulator to your website.