DEV Community

Surendra Singh Kamboj
Surendra Singh Kamboj

Posted on AI-assisted

Magnetic field inside a solenoid in JavaScript: B = mu0 n I and what an iron core does

Inside a long solenoid the field is uniform and equals B = mu0 n I, where n is turns per metre. With an iron core of relative permeability mu_r it becomes mu0 mu_r n I. Numbers below were run in Node.

How do you calculate the field of a solenoid?

const mu0 = 4 * Math.PI * 1e-7;
const n = 1000, I = 2;                                  // 1000 turns/m, 2 A
console.log((mu0 * n * I).toExponential(4));            // 2.5133e-3 T
console.log((mu0 * (500 / 0.25) * 1.5).toExponential(4)); // 3.7699e-3 T: 500 turns on 25 cm, 1.5 A
console.log((mu0 * 1000 * n * I).toFixed(3));           // 2.513 T with mu_r = 1000 (idealised iron)
Enter fullscreen mode Exit fullscreen mode
Case Field
1000 turns/m, 2 A, air 2.5133 mT
500 turns on 0.25 m, 1.5 A 3.7699 mT
Same as the first with mu_r = 1000 2.513 T (idealised, real iron saturates)

Does the field depend on the radius?

No. For a long solenoid the field depends only on n and I, not on the diameter. That is why you can wind the coil on a thin or fat tube.

FAQ: Why is the field zero outside?

For an ideal long solenoid the fields of the turns cancel outside. A real one has a small stray field.

I build PractiSci, a free-to-start virtual lab. Change turns and current yourself: Solenoid and toroid lab. Chapter guide: Moving charges and magnetism.

Top comments (0)