# How do I display magnetic field lines in 2D/3D with a stick of current?

I'm new to Mathematica (trying it out for an electromagnetism course) and struggling with plotting magnetic field lines using a line of current. Not a torturous, not a cylinder, not a circle. A line. Almost every example I see is far more complex and it's beyond what I'm trying to do... I know I have to use VectorPlot, but I'm feeling kind of lost. Any help on this would be welcome!

• Do you mean several lines or just one line? May 12, 2021 at 14:17
• Just one line of current (E) in the Z direction, but several magnetic field lines (B) May 12, 2021 at 14:29
• Try ParametricPlot3D[{{0, 0, t}, Table[{r Cos[t], r Sin[t], z0}, {r, {1, 2, 3}}, {z0, {-3, 0, 3}}]}, {t, -4, 4}, Boxed -> False, PlotStyle -> {Red, Blue}]. Do you need some arrows? May 12, 2021 at 14:45
• Yeah I need the arrows, so I know where the magnetic field acting upon the electric field is May 12, 2021 at 16:41

in your course you will have learned that the magnetic field at r of a a current infinitesimal dI at position r0 is proportional to:

Cross[dI,(r-r0)]/Norm[r-r0]^3

Now if we consider a current of 1 along the x ax at {x0,0,0}: dI= {1,0,0} dx0 and the field at {x,y,z};

dField= Cross[{1,0,0},({x,y,z}-{x0,0,0})]/ Norm[{x,y,z}-{x0,0,0}] dx0

If we integrate this from -Infinity to Infinity we get a field that is proportional to H or B. As we integrate from -Infinity to Infinity the integral can not depend on x, we may therefore calculate the integral for x==0:

field[y_,z_]= -Integrate[Cross[{1,0,0},{x0,y,z}]/ Norm[{x0,y,z}]^3,{x0,-Infinity,Infinity},Assumptions -> {y > 0, z > 0}]

We can plot this field using "VectorPlot3D", but note: by default all arrows have the same length, field strength is indicated by color. To get different length arrows, we use the options , VectorScaling -> Automatic, VectorSizes -> {0, 1}]:

d = 1;
VectorPlot3D[field[y, z], {x, -d, d}, {y, -d, d}, {z, -d, d},
VectorScaling -> Automatic, VectorSizes -> {0, 1}]

• Thank you! How would I do this in 2D as well? Just using VectorPlot instead to VectorPlot3D? May 12, 2021 at 16:59
• In principle it is already 2D. Simply eliminate the x coordinate: VectorPlot[field[y, z][[2 ;; 3]], {y, -d, d}, {z, -d, d}, VectorScaling -> Automatic, VectorSizes -> {0, 1}] May 13, 2021 at 7:00

We can use 3D graphics as follows