7
$\begingroup$

Bug introduced in 7.0, persisting through 13.2.


I am looking to plot the Magnetic Vector potential, A, as a function of position along the edge of a density plot. The density plot is fine, and I would combine the two plots using Show. After searching, I found a similar question here. I needed to modify the code a bit to get it to work in version 12.2, however the following code works just fine:

ClearAll
μ = 0.02; Px = -0.5; h = 1.0;
vData = Table[{1, j}, {j, -1, 1, 0.1}];
f[y_] := (h^2/(2 μ)) (-Px) (1 - (y/h)^2)
VectorPlot[{f[y], 0}, {x, 0, 3}, {y, -h, h}, VectorPoints -> vData, 
 VectorScaling -> Automatic, VectorColorFunction -> "Rainbow", 
 PlotRange -> {{0, 4}, {-1.1, 1.1}}, 
 Epilog -> {{Red, Thick, Line[{{0, 1}, {4, 1}}]}, {Red, Thick, 
    Line[{{0, -1}, {4, -1}}]}}]

And results in this plot: Vectors along an edge

However, when I substitute my own code, with as far as I can tell the same approach, I get a "Part::partw: Part 1 of {} does not exist." error. Besides a different function form, I do not see anything different between my error prone code and the working code above. Any guidance would be much appreciated! Thanks.

ClearAll
a = 5 10^-3; 
q1 = 1.602 * 10^-10;
eps0 = 8.85418781 * 10^-12; 
qEnclosed[r_] := If[r < a,  (q1 r^3)/a^3, q1];
eField[r_] := qEnclosed[r]/(4 π r^2 eps0);
potSphereTEMP[r_] = -Integrate[eField[r], r ];
potInfinity = Limit[potSphereTEMP[r], {r -> ∞} ];
potSphere[r_] := 
 potSphereTEMP[r] - 
  potInfinity  (* assume potential is zero at infinity *)
vData = Table[{0, j}, {j, -1.5 a, 1.5 a, 0.15 a}]
VectorPlot[{potSphere[y], 0}, {x, -a, a}, {y, -1.5 a, 1.5 a}, 
 VectorPoints -> vData, VectorScaling -> Automatic, 
 VectorColorFunction -> "Rainbow", 
 PlotRange -> {{-a, a}, {-1.5 a, 1.5 a}}]
$\endgroup$
7
  • 2
    $\begingroup$ welcome to Mathematica Stackexchange. In your second code chunk you have eField and eField1. Is this intentional? Otherwise eField1 is undefined. $\endgroup$
    – ubpdqn
    Jun 25, 2023 at 2:21
  • 1
    $\begingroup$ Oddly, this seems to work (replacing efield1 by efield): VectorPlot[{potSphere[y], 0}/1, {x, -a, a}, {y, -1.5 a, 1.5 a}, VectorPoints -> vData, VectorScaling -> Automatic, VectorColorFunction -> "Rainbow", PlotRange -> {{-a, a}, {-1.5 a, 1.5 a}}] $\endgroup$
    – Michael E2
    Jun 25, 2023 at 3:04
  • 4
    $\begingroup$ The problem is found before, but doesn't receive enough attention: mathematica.stackexchange.com/q/81182/1871 It's broken from the beginning: i.stack.imgur.com/1R7iU.png $\endgroup$
    – xzczd
    Jun 25, 2023 at 6:36
  • 4
    $\begingroup$ This bug of StreamPlot is essentially the same bug, I believe: mathematica.stackexchange.com/q/133381/1871 $\endgroup$
    – xzczd
    Jun 25, 2023 at 6:46
  • 1
    $\begingroup$ Yeah, you can report bug via e-mail: wolfram.com/support/contact/?topic=feedback They reply fairly soon. $\endgroup$
    – xzczd
    Jun 25, 2023 at 12:45

1 Answer 1

5
$\begingroup$

It might be a bug. I'm not sure why it makes a difference -- something to do with Piecewise maybe -- but using ?NumericQ seems to avoid the problem:

ClearAll[potSphere]; (***)
a = 5 10^-3;
q1 = 1.602*10^-10;
eps0 = 8.85418781*10^-12;
qEnclosed[r_] := If[r < a, (q1 r^3)/a^3, q1];
eField[r_] := qEnclosed[r]/(4 \[Pi] r^2 eps0);
potSphereTEMP[r_] = -Integrate[eField[r], r];
potInfinity = Limit[potSphereTEMP[r], {r -> \[Infinity]}];
potSphere[r_?NumericQ] :=  (***)
  potSphereTEMP[r] - 
   potInfinity ;(*assume potential is zero at infinity*)
vData = Table[{0, j}, {j, -1.5 a, 1.5 a, 0.15 a}];
VectorPlot[{potSphere[y], 0}/1, {x, -a, a}, {y, -1.5 a, 1.5 a}, 
 VectorPoints -> vData, VectorScaling -> Automatic, 
 VectorColorFunction -> "Rainbow", 
 PlotRange -> {{-a, a}, {-1.5 a, 1.5 a}}]

Addendum: MWE

Here's simple example of the bug, which seems to center on a vector List in which a component contains Piecewise for unknown reasons:

VectorPlot[{Piecewise[{{y, y > 0}}], 1},
 {x, -1, 1}, {y, -1, 1}]

Output is a pink graphic with a "Part is not a Graphics primitive or directive" message. The graphics generated are:

Graphics[{{}[[1]], {}},...<options>...]

These work:

  1. Head not List (see also this comment):
 VectorPlot[Identity@{Piecewise[{{y, y > 0}}], 1},
  {x, -1, 1}, {y, -1, 1}]
  1. Head Piecewise, value a List (e.g. PiecewiseExpand[{Piecewise[{{y, y > 0}}], 1}]):
VectorPlot[Piecewise[{{{0, 1}, y <= 0}}, {y, 1}],
 {x, -1, 1}, {y, -1, 1}]

These two produce the following graphics:

$\endgroup$
2
  • $\begingroup$ Its a bug, that even the usual Evaluate of the Plot object does not handle. Include the second coordinate into the definition potSphere[r_] := {potSphereTEMP[r] - potInfinity , 0}. VectorPlot[ potSphere[y] , ..., Authors of graphic routines expect bite-sized flat-numeric functions. Its the weak point of the LISP philosophy "everything is a list" $\endgroup$
    – Roland F
    Jun 25, 2023 at 4:26
  • $\begingroup$ @MichaelE2 didn’t have opportunity to run (just noting code anomaly) but this answer has been instructive in terms of Mathematica behaviour. $\endgroup$
    – ubpdqn
    Jun 25, 2023 at 5:37

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.