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!
2 Answers
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}] - $\begingroup$ Thank you! How would I do this in 2D as well? Just using VectorPlot instead to VectorPlot3D? $\endgroup$Jessica– Jessica2021-05-12 16:59:34 +00:00Commented 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$Daniel Huber– Daniel Huber2021-05-13 07:00:04 +00:00Commented 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], Arrow[BSplineCurve@ Table[{r Cos[t], r Sin[t], z0}, {t, 0, 2 Pi, Pi/50}]]}, {r, {1, 2, 3}}, {z0, {-3, 0, 3}}]}, Boxed -> False] 

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$