I am trying to go beyond StreamPlot in the case of a current loop that produces a magnetic field. My code works for StreamPlot, but does not end when I use the same field expressions with StreamPlot3D. Code is as follows:

$Assumptions={_Symbol \[Element]Reals};
element[theta_]:={radius Cos[theta],radius Sin[theta],0};
modR=Sqrt[modR2] //ExpandAll //FullSimplify;
unitVector=fpToElement/modR // FullSimplify;



(* This works in a few seconds*)
StreamPlot[{Bx[x,0,z],Bz[x,0,z]},{x,-1,1},{z,-1,1},ImageSize->Large, StreamPoints->Coarse]//AbsoluteTiming
(* This does not end *)

The results of StreamPlot are as expected:

Magnetic lines in XZ plane

However, if I comment out the StreamPlot[] code and uncomment the StreamPlot3D[] part (at the bottom), I get no results for hours beyond three indications that the third field component

has evaluated to non-numerical values for all sampling points in the region with boundaries...

Any help will be much appreciated.


1 Answer 1


If you prevent the definition of Bx, By, Bz to be evaluated for symbolic argument and and introduce "Thread" in their definition you can get a reasonable time for evaluation. Further note, that the plot is symmetrical in x,y,z around the origin. Therefore it is good enough to restrict the plot region to: {x,y,z} element {0,1}. With this:

Thread[{Bx[x_, y_, z_] /; NumericQ[x], By[x_, y_, z_] /; NumericQ[x], 
    Bz[x_, y_, z_] /; NumericQ[x]} := {NIntegrate[
     dBx[x, y, z, theta], {theta, 0, 2 \[Pi]}], 
    NIntegrate[dBy[x, y, z, theta], {theta, 0, 2 \[Pi]}], 
    NIntegrate[dBz[x, y, z, theta], {theta, 0, 2 \[Pi]}]}];

StreamPlot3D[{Bx[x, y, z], By[x, y, z], Bz[x, y, z]}, {x, 0, 1}, {y, 
   0, 1}, {z, 0, 1}, StreamPoints -> Coarse]

enter image description here

I get 3 minutes for evaluation.


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.