B Field for a Uniformly Magnetized Sphere

an experiment in magnetostatics

Needs["VectorFieldPlots`"];

Needs["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.

(Subscript[Φ, M])=(((4 π) (Subscript[M, 0]) (a^3) z)/(3 ((Sqrt[(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 VectorFieldPlot3D to generate a graphic of the field lines.

vecs=VectorFieldPlot3D[Bfield/.{a->1,M0->2},{x,-2,2},{y,-2,2},{z,-2,2},PlotPoints->10]

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_}}]:>{Hue[(1/4) 0.8` (Sqrt[(x2^2)+(y2^2)+(z2^2)])],EdgeForm[],Specularity[White,20],Cylinder[{{x1,y1,z1},{x2,y2,z2}},0.02`]};

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

mygr=Show[tubevecs,Graphics3D[{Opacity[.3],Sphere[{0,0,0},1]}],SphericalRegion->True,ImageSize->{600,600},Background->Black,Lighting->"Neutral"]