Magnetic field of a dipole magnet

For an alternate, more accurate method with pstricks, please see this page.

magnet_fieldlines_dipoles-001.pngmagnet_fieldlines_dipoles-002.pngmagnet_fieldlines_dipoles-003.png

Edit and compile if you like:

% Author: Izaak Neutelings (March 2020)
\documentclass[border=3pt,tikz]{standalone}
\usepackage{amsmath} % for \dfrac
\usepackage{bm}
\usepackage{tikz,pgfplots}
\tikzset{>=latex} % for LaTeX arrow head
\pgfplotsset{compat=1.13}
\usetikzlibrary{decorations.markings,intersections,calc}
\usepackage{ifthen}
\usepackage{xcolor}
\usepackage{auto-pst-pdf}
\usepackage{pst-magneticfield}
\usepackage[outline]{contour} % glow around text
\colorlet{Bcol}{violet!90}
\colorlet{BcolFL}{violet!90}
\tikzstyle{north}=[thick,top color=red!60,bottom color=red!90,shading angle=20]
\tikzstyle{south}=[thick,top color=blue!60,bottom color=blue!90,shading angle=20]
\contourlength{1.5pt}
\tikzset{
EFielLineArrow/.style args={#1,#2}{BcolFL,thick,decoration={markings,
mark=at position #1 with {\arrow[rotate=#2]{latex}}},
postaction={decorate}}
}
\makeatletter
\newcommand{\xy}[3]{% % FIND X, Y
\tikz@scan@one@point\pgfutil@firstofone#1\relax
\edef#2{\the\pgf@x}%
\edef#3{\the\pgf@y}%
}
\makeatother
\newcommand{\EFielLineArrow}[2]{ % ELECTRIC FIELD LINE ARROW
\pgfkeys{/pgf/fpu,/pgf/fpu/output format=fixed} % for calculation between -1*10^324 and +1*10^324
\pgfmathsetmacro{\x}{#1/28.45pt}
\pgfmathsetmacro{\y}{#2/28.45pt}
\pgfmathsetmacro{\U}{\Q*((\y+\a)^2+(\x)^2)^(3/2)}
\pgfmathsetmacro{\V}{-(\Q)*((\y-\a)^2+(\x)^2)^(3/2)}
\pgfkeys{/pgf/fpu=false}
\pgfmathparse{
atan2(((\y+\a)*\V + (\y-\a)*\U),((\x)*\V + (\x)*\U))
}
 
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Click to download: magnet_fieldlines_dipoles.texmagnet_fieldlines_dipoles.pdf
Open in Overleaf: magnet_fieldlines_dipoles.tex

2 Replies to “Magnetic field of a dipole magnet”

  1. Hi Issak, Thanks for this wonderfull and great job!
    I’am wondering why this code wouldn;t compile on my computer and shows me this error below:
    Package pgf Error: No shape named `intersection-1′ is known. \EFieldLines
    Package pgf Error: No shape named `intersection-0′ is known. \EFieldLines

    1. Hey Sofiane,

      Sorry for the late reply, I only saw your comment now!

      Did you perhaps run the code after changing some of the contour ranges in \range or the figure size? To place arrowheads on the field lines, the \EFieldLines macro computes intersections with an ellipse or straight line. From the error you shared it looks like no intersections were found (\Nb=0), so the foreach “\foreach \i in {1,…,\Nb}” tries to loop from 1 to 0, but “intersection-1” and “intersection-0” do not exist. This might require some finetuning of the contours or ellipse, or you could just disable this part in \EFieldLines.

      Cheers,
      Izaak

Leave a Reply

Your email address will not be published.