B Field for a Uniformly Magnetized Sphere

an experiment in magnetostatics

<< Graphics`PlotField3D`

<< Calculus`VectorAnalysis`

For a uniformly magnetized sphere of radius, a, the potential inside the sphere can be described by the following.

(Subscript[Φ, M]) = 
      (1/3) (4 π) (Subscript[M, 0]) z

innerfield = 
      Grad[((4 π)/3) M0 z, Cartesian[x, y, z]]

The potential outside the sphere can be described by the following.

\(TraditionalForm\`Φ\_M = \(\((4\ π)\)\ M\_0\ a\^3\ z\)\/\(3\ \(\@\(x\^2 + y\^2 + z\^2\)\)\^3\)\)

outerfield = -Grad[((4 π)/3) M0 (a^3) (z/Sqrt)[(x^2) + (y^2) + (z^2)]^3, Cartesian[x, y, z]]

Bfield = 
      Piecewise[{{innerfield, Sqrt[(x^2) + (y^2) + (z^2)] < a}, {outerfield, 
            Sqrt[(x^2) + (y^2) + (z^2)] ≥ a}}]

We can use PlotVectorField3D to generate a graphic of the field lines.

vecs = 
        PlotVectorField3D[
          Bfield /. {a -> 1, M0 -> 2}, {x, -2, 2}, {y, -2, 
            2}, {z, -2, 2}, PlotPoints -> 10, 
          SphericalRegion -> True, ImageSize -> 600];

For more of a 3D effect, we can convert the field lines into cylinders that respond to lighting.  First, we need to define a function that generates a tube going from one point to another with a specified radius.

myCylinder[{p1_, p2_}, 
        r_] := 
Module[{gc, len, v, n1, n2, n1y, n1z, n2x, 
          n2y, n2z}, 

 (* 
          Calculate Normals *) 
len = 
          Norm[p2 - p1]; 
v = 
          N[(p2 - p1)/len]; 
If[
          MatchQ[v, {x_, 0. , 
              0. }], 
{n1, 
                n2} = {{0, 1, 0}, {0, 0, 1}};, 
{n1, 
              n2} = {{0, n1y, n1z}, {n2x, n2y, n2z}}; 
n1 = 
            n1 /. First[
                Solve[{n1 . v == 0, n1 . n1 == v . v}, {n1y, 
                    n1z}]]; 
n2 = 
            n2 /. First[
                Solve[{n2 . v == 0, n2 . n1 == 0, 
                    n2 . n2 == v . v}, 
                  n2]];
]; 

 (* 
          Create Cylinder *) 
ParametricPlot3D[(h v + r Sin[t] n1 + r Cos[t] n2) + (p1 + (p2 - p1)/2), {h, -len/
                2, len/2}, {t, 0, 2 π}, PlotPoints -> 15, 
            DisplayFunction -> 
              Identity][[1]]
]

Now, we can replace the Line primitives in the previous graphic with cylinders.  The colors are chosen such that cylinders with an endpoint further from the origin appear more blue.

tubevecs = 
        vecs /. Line[{{x1_, y1_, z1_}, {x2_, y2_, 
                  z2_}}] :> {EdgeForm[], 
              SurfaceColor[Hue[ .8 Sqrt[x2^2 + y2^2 + z2^2]/4], White, 20], 
              myCylinder[{{x1, y1, z1}, {x2, y2, z2}},  .02]};

We also should represent the conducting sphere itself.  However, to prevent the sphere from obscuring our view near the origin, we use ParametricPlot3D to plot only a hemisphere and hence, more of a cut-away view.

mysphere = 
        ParametricPlot3D[{Sin[v] Cos[u], Sin[v] Sin[u], 
            Cos[v], {EdgeForm[], SurfaceColor[Gray, White, 50]}}, {u, 
            0, π}, {v, 0, π}, PlotPoints -> 50];

Now, we combine the sphere and the field cylinders and apply lighting and other visual features.

mygr = 
        Show[tubevecs, mysphere, SphericalRegion -> True, 
          ImageSize -> 800, Background -> Black, 
          LightSources -> {{{1, 1, 1}, White}}];