I’ll help you create a $Python$ program to calculate and visualize the magnetic field of a current loop.
We’ll solve a specific example and create visualizations to better understand the field distribution.
1 | import numpy as np |
Let me explain the code and the physics behind it:
Physics Background:
- A circular current loop generates a magnetic field described by the $Biot$-$Savart$ $law$
- The field has axial symmetry around the $z$-axis
- The field is strongest near the loop and decreases with distance
- Inside the loop, the field is mostly uniform and parallel to the $z$-axis
Code Structure:
- The
magnetic_field_loopfunction calculates the magnetic field components (Bx, By, Bz) at any point in space - We use the analytical solution for the magnetic field of a current loop
- The calculation includes the permeability of free space (μ₀)
- We create a $3D$ grid of points to evaluate the field
- The
Visualizations:
Left plot: Magnetic field lines in the $xz$-plane ($y = 0$)
- The red circle represents the current loop
- The streamlines show the direction of the magnetic field
- Colors indicate field strength
Middle plot: Bz component along the $z$-axis
- Shows how the field strength varies along the axis of the loop
- The field is strongest near the loop and decreases with distance
Right plot: $3D$ vector representation
- Arrows show the direction of the magnetic field in $3D$ space
- The length of the arrows is normalized for better visualization
Parameters Used:
- Loop radius ($R$) = $0.1$ meters ($10$ cm)
- Current ($I$) = $1$ ampere
- Calculated field is in Tesla units

The visualizations show that:
- The field is symmetric around the $z$-axis
- Field lines emerge from one side of the loop and curve around to enter the other side
- The field is strongest near the loop and decreases with distance
- Inside the loop, the field is relatively uniform and points along the $z$-axis









