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!

  • 1
    $\begingroup$ Do you mean several lines or just one line? $\endgroup$ May 12, 2021 at 14:17
  • $\begingroup$ Just one line of current (E) in the Z direction, but several magnetic field lines (B) $\endgroup$
    – Jessica
    May 12, 2021 at 14:29
  • $\begingroup$ 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? $\endgroup$ May 12, 2021 at 14:45
  • $\begingroup$ Yeah I need the arrows, so I know where the magnetic field acting upon the electric field is $\endgroup$
    – Jessica
    May 12, 2021 at 16:41

2 Answers 2


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:


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}]

enter image description here

  • $\begingroup$ Thank you! How would I do this in 2D as well? Just using VectorPlot instead to VectorPlot3D? $\endgroup$
    – Jessica
    May 12, 2021 at 16:59
  • $\begingroup$ 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}] $\endgroup$ May 13, 2021 at 7:00

We can use 3D graphics as follows

Graphics3D[{{Red, Arrowheads[0.025], 
   Arrow[Line[{{0, 0, -4}, {0, 0, 4}}]]}, 
  Table[{Blue, Arrowheads[0.025], 
      Table[{r Cos[t], r Sin[t], z0}, {t, 0, 2 Pi, Pi/50}]]}, {r, {1, 
     2, 3}}, {z0, {-3, 0, 3}}]}, Boxed -> False]

Figure 1


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.