11

Here is what I want to do:

enter image description here

And here is what I've done so far

\begin{tikzpicture}[scale=1]
\draw[line width=1pt,->] (-4,0) -- (4,0);
\draw[line width=1pt,->] (0,-4) -- (0,4);

\foreach \u in {0,...,8}
\draw[color=red,domain=-3.14:3.14,samples=200,smooth] plot (canvas polar cs:angle=\x r,radius={\u*sin(\x r)*\u*sin(\x r)*5}); 
\end{tikzpicture}

enter image description here

As you can see, there is a problem: the function exceeds the axes.

How can we change that please?

(By the way I am French so if my explanations are not clear let me know)

Loïc Poncin
  • 319
  • 2
  • 9
  • 3
    You can clip the picture. –  Jun 17 '16 at 17:27
  • 1
    Off-topic: The magnetic field vectors are not really tangent vectors to the field lines in the screen shot ;-) (Welcome to TeX.SX!) –  Jun 17 '16 at 17:30
  • @ChristianHupfer Sorry! you're right :) (Happy to join the community!) – Loïc Poncin Jun 17 '16 at 17:34
  • Please always post compilable code rather than just a fragment. Compilable code is much easier to work with and helps reduce all kinds of misunderstandings when people complete the code in ways other than the OP. (It is never 'obvious' how to complete an MWE as some people seem to think. TeX is too various for that ... ;).) – cfr Jun 17 '16 at 22:46
  • 1
    http://www.texample.net/tikz/examples/dipolar-magnetic-field/ – Ignasi Jun 18 '16 at 07:41

3 Answers3

12

If you like you can adapt this drawing of mine --- the field lines are not exact, they're just qualitative: (notice that you can rotate the magnet changing \angle at the start of the picture).

\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{shadings, calc, decorations.markings}
\tikzset{->-/.style={decoration={
  markings,
  mark=at position #1 with {\arrow{>}}},postaction={decorate}},
  ->-/.default=0.5,
  }
\begin{document}
\begin{tikzpicture}
    \def\angle{50}% change this to rotate the magnet
    \path [use as bounding box] (-3.2,-3.2) rectangle (3.2,3.2);
    %\draw[very thin, gray!50] (-3,-3) grid (3,3);
    \draw[->](-3,0) -- (3,0) node [above] {$x$};
    \draw[->](0,-3) -- (0,3) node [right] {$z$};
    \draw[thick, color=blue] (0, 0) circle (2);
    \draw [semithick, ->] (0:1.2) arc (0:\angle:1.2);
    \node at (0.5*\angle:1.4) {$\theta$};
    \begin{scope}[gray,text=black, rotate=\angle]
        \node[draw, left color=red!60,right color=blue!60,  middle color=white, 
        rotate=\angle, shading angle=90+\angle, minimum width=1cm,
        ]
        at (0,0) (I) {};
        \draw[->-] (I.north east) .. controls (1.5,1) and (-1.5,1) ..  (I.north west);
        \coordinate (n1) at ($ (I.north east)!0.5!(I.east) $);
        \coordinate (s1) at ($ (I.north west)!0.5!(I.west) $);
        \draw[->-] (n1) .. controls (3, 2) and (-3, 2) .. (s1);
        \draw[->-] (I.east) .. controls (6,3) and (-6,3) .. (I.west);
        \draw[->-] (I.south east) .. controls (1.5,-1) and (-1.5,-1) ..  (I.south west);
        \coordinate (n2) at ($ (I.south east)!0.5!(I.east) $);
        \coordinate (s2) at ($ (I.south west)!0.5!(I.west) $);
        \draw[->-] (n2) .. controls (3, -2) and (-3, -2) .. (s2);
        \draw[->-] (I.east) .. controls (6,-3) and (-6,-3) .. (I.west);
    \end{scope}
\end{tikzpicture}
\end{document}

Magnet

Rmano
  • 40,848
  • 3
  • 64
  • 125
  • It is a minor thing, but the arrows point the other way around in OP. – Matsmath Jun 17 '16 at 17:47
  • @Matsmath yep, told you it was qualitative :-). Just swap \draw[->-] start and end points or change \arrow{>} in the decoration. – Rmano Jun 17 '16 at 17:50
6

This is the nearest thing that I got.

enter image description here

  1. Used arcs to draw the field lines and cropped using \clip.
  2. Used decorations.markings to mark arrows on field lines.
  3. The green arrows has to be improved further.

    \documentclass{standalone}
    \usepackage{tikz}
    \usetikzlibrary{calc}
    \usetikzlibrary{decorations.markings}
    
    \tikzstyle directed=[postaction={decorate,decoration={markings, % arrows on the field lines
      mark=at position .1 with {\arrowreversed[scale=1.5]{stealth}},
      mark=at position .9 with {\arrowreversed[scale=1.5]{stealth}}}}]
    \tikzstyle tangent=[postaction={decorate,decoration={markings, % Tangent to the field line
      mark=at position .7 with {\draw[ultra thick,stealth-,green!60!black,solid](-12pt,0)--(12pt,0)node[above]{$\vec{B}$};}}}]
    \tikzstyle fLines=[thick,dashed,directed,tangent]
    
    \begin{document}
    \begin{tikzpicture}
    \def\lmag{1.8}  % length of magnet
    \def\wmag{0.4}  % thickness of magnet
    \def\nc{5}      % no. of lines = 2*\nc+1
    
    \begin{scope}
    \coordinate (A) at (-\lmag/2,\wmag/2);
    \coordinate (B) at (\lmag/2,-\wmag/2);
    \draw[fill, color=blue](A) rectangle ++(\lmag/2,-\wmag)node[white,midway]{S};
    \draw[fill, color=red](0,-\wmag/2) rectangle ++(\lmag/2,\wmag)node[white,midway]{N};
    
    \clip (-5,-3) rectangle (5,3);
    \foreach \r in {1,...,\nc}{
    \draw[fLines]($(A)-(0,0.5*\r*\wmag/\nc)$) arc(({270-asin(\lmag/(2*\r))}):({-90+asin(\lmag/(2*\r))}):\r);
    \draw[fLines]($(B)+(0,0.5*\r*\wmag/\nc)$) arc(({90-asin(\lmag/(2*\r))}):({-270+asin(\lmag/(2*\r))}):\r); }
    \draw[fLines] (-\lmag/2,0) -- ++(-6,0);
    \draw[fLines] (\lmag/2,0) ++(6,0)--(\lmag/2,0);
    \end{scope}
    \end{tikzpicture}
    \end{document}
    
nidhin
  • 7,932
  • Good, but the arrows are reversed in the top and bottom parts. This can be fixed by reversing the path on the bottom part. – Andyc Mar 08 '22 at 08:18
6

6 years seems to be the right time. I'm not a huge fan of your example since it seems to be just a couple of cropped circles. The streamlines of a magnetic dipole are defined by $$y^2=x^{\frac{4}{3}}C-x^2$$ You can easily find informations about this online.
Now here is the code I used to illustrate the earth's magnetic field. The code is dirty but the output is nice and physically accurate.
Edit: I modified the little red arrows so they are now tangent to the streamlines.

\usepackage{tikz}
\usetikzlibrary{arrows.meta,positioning}

\begin{document}

\def\R{8} \def\Rp{7.8} \def\CC{10}

\centering \begin{tikzpicture}[line cap=round,line join=round,x=1.0cm,y=1.0cm] \clip(-10,-10) rectangle (10,10); \drawcolor=gray, line width=3pt,dash pattern=on 7pt off 7pt --(0,-10);

\begin{scope}[rotate=11] \foreach \C in {2,3,4}{ \draw[line width=2.pt,smooth,samples=200,domain=0:{\C^(3/2)}] plot(\x,{sqrt( abs((\x)^(4/3)\C-(\x)^(2)))}); \draw[line width=2.pt,smooth,samples=200,domain=0:{\C^(3/2)}] plot(-\x,{sqrt(abs((\x)^(4/3)\C-(\x)^(2)))}); \draw[line width=2.pt,smooth,samples=200,domain=0:{\C^(3/2)}] plot(\x,{-sqrt(abs((\x)^(4/3)\C-(\x)^(2)))}); \draw[line width=2.pt,smooth,samples=200,domain=0:{\C^(3/2)}] plot(-\x,{-sqrt(abs((\x)^(4/3)\C-(\x)^(2)))});

\draw[-{Stealth[scale=3]},draw opacity=0] ({\C^(3/2)},0) -- ({\C^(3/2)},0.25); \draw[-{Stealth[scale=3]},draw opacity=0] ({-\C^(3/2)},0) -- ({-\C^(3/2)},0.25);}

\foreach \C in {6,10,25}{ \draw[line width=2.pt,smooth,samples=200,domain=0:{\C^(3/2)}] plot(\x,{sqrt( abs((\x)^(4/3)\C-(\x)^(2)))}); \draw[line width=2.pt,smooth,samples=200,domain=0:{\C^(3/2)}] plot(-\x,{sqrt(abs((\x)^(4/3)\C-(\x)^(2)))}); \draw[line width=2.pt,smooth,samples=200,domain=0:{\C^(3/2)}] plot(\x,{-sqrt(abs((\x)^(4/3)\C-(\x)^(2)))}); \draw[line width=2.pt,smooth,samples=200,domain=0:{\C^(3/2)}] plot(-\x,{-sqrt(abs((\x)^(4/3)\C-(\x)^(2)))});%}

\draw[-{Stealth[scale=3]},draw opacity=0] ({\R^(3/2)\C^(-3/4)},{\Rsqrt(1-\R\C^(-3/2))})-- ({\Rp^(3/2)\C^(-3/4)},{\Rpsqrt(1-\Rp\C^(-3/2))});

\draw[-{Stealth[scale=3]},draw opacity=0] ({-\R^(3/2)\C^(-3/4)},{\Rsqrt(1-\R\C^(-3/2))})-- ({-\Rp^(3/2)\C^(-3/4)},{\Rpsqrt(1-\Rp\C^(-3/2))});

\draw[-{Stealth[scale=3]},draw opacity=0] ({\Rp^(3/2)\C^(-3/4)},{-\Rpsqrt(1-\Rp\C^(-3/2))})-- ({\R^(3/2)\C^(-3/4)},{-\Rsqrt(1-\R\C^(-3/2))});

\draw[-{Stealth[scale=3]},draw opacity=0]({-\Rp^(3/2)\C^(-3/4)},{-\Rpsqrt(1-\Rp\C^(-3/2))})-- ({-\R^(3/2)\C^(-3/4)},{-\Rsqrt(1-\R\C^(-3/2))}); }

\draw[line width=2pt] (0,-10) -- (0,10);
\draw-{Stealth[scale=3]},draw opacity=0--(0,\Rp); \draw-{Stealth[scale=3]},draw opacity=0--(0,-\R);

\drawcolor=red, fill=redrectangle (1,3); \draw [color=white] (0,1.5) node{\Large S} ; \drawcolor=blue, fill=bluerectangle (1,-3); \draw[color=white] (0,-1.5) node{\Large N} ;

\end{scope}

\drawcolor=blue, line width=3pt circle (4);
\drawcolor=blue, line width=3pt,dash pattern=on 7pt off 7pt --(4,0);

\begin{scope}[rotate=11] \draw-Stealth,color=red,line width=2pt--(4.1,0.5); \draw-Stealth,color=red,line width=2pt--(0,4); \draw-Stealth,color=red,line width=2pt--(0,-5);

\draw[-Stealth,color=red,line width=2pt] (4.48081,1.68121) --(3.50953,1.91916); \draw-Stealth,color=red,line width=2pt -- (4.48081,-1.68121);

\draw-Stealth,color=red,line width=2pt -- (2.08677,3.41253); \draw-Stealth,color=red,line width=2pt --(2.83512,-4.07584);

\end{scope}
\end{tikzpicture} \end{document}

enter image description here