Skip to content

Magnetic Dipole

The magnetic field of a dipole moment M at position r relative to the origin is given by:

B(r)=μ04πr3(3(Mn^)n^M)

where n^=r/r is the unit vector and r=|r|.

Calculate the field of a magnetic dipole moment M=(0,0,1) at various points.

julia
using Magnetostatics, StaticArrays, LinearAlgebra
using CairoMakie

M = SVector(0.0, 0.0, 1.0)
dipole = Dipole(M)

r = SVector(1.0, 0.0, 0.0) # Point on x-axis
B = dipole(r)
println("B at $r: $B [T]")
B at [1.0, 0.0, 0.0]: [0.0, 0.0, -1.0e-7] [T]

Visualizing the field in the xz-plane:

julia
xs = range(-2, 2, length=51)
zs = range(-2, 2, length=51)

function field_xz(x::T, z::T) where T
    B = dipole(SVector(x, zero(T), z))
    return Point(B[1], B[3])
end

fig = Figure(size = (700, 600), fontsize=20)
ax = Axis(fig[1, 1];
    xlabel="x", ylabel="z", aspect=DataAspect(), title="Magnetic Dipole Field")

Bmag = [norm(dipole(SVector(x, 0.0, z))) for x in xs, z in zs]
hm = heatmap!(ax, xs, zs, log10.(Bmag .+ 1e-9), colormap=:plasma)
Colorbar(fig[1, 2], hm, label="log10(|B|)")

streamplot!(ax, field_xz, -2..2, -2..2; arrow_size = 8, linewidth = 1.5)

fig