1
$\begingroup$

I am trying to plot the following simple vector field $$\sqrt{4-x^2-y^2-z^2}*(y,-x,0)$$ but when I plug this field into the functions VectorPlot and VectorPlot3D, the output graph is completely blank. The MWE im using is

ClearAll[expr]
expr[x_, y_, z_] := 
 N@Piecewise[{{Sqrt[4 - (x^2 + y^2 + z^2)] {y, -x, 0}, 
     4 > (x^2 + y^2 + z^2)}}, {0, 0, 0}]
VectorPlot[expr[x, y, 0], {x, -4, 4}, {y, -4, 4}]
VectorPlot3D[
 Evaluate@expr[x, y, z], {x, -5, 5}, {y, -5, 5}, {z, -2, 2}, 
 VectorPoints -> Fine]

with output:

enter image description here enter image description here

which is no good :(.

I tried defining my vector field using the ConditionalExpression function:

ClearAll[expr]
field = Sqrt[4 - (x^2 + y^2 + z^2)] {y, -x, 0}
expr[a_, b_, c_] := 
 ConditionalExpression[field, Im[field] == {0, 0, 0}] /. {x -> a, 
   y -> b, z -> c}
expr[4, 4, 4]
VectorPlot3D[expr[x, y, z], {x, -5, 5}, {y, -5, 5}, {z, -2, 2}, 
 VectorPoints -> Fine]

Now, this actually produces something, but its largely nonsense (even though it contains what Im looking for):

Undefined

enter image description here

I entered expr[4,4,4] to verify it returns undefined, as it should. However, VectorPlot3D is still plotting something. I don't even know what it could be plotting in the area where it should be empty. Perhaps Mathematica is doing something strange and somehow evaluating the imaginary part and then plotting it? I really don't know.

I appreciate any help!

$\endgroup$

1 Answer 1

4
$\begingroup$

Try RegionFunction instead of Piecewise

VectorPlot3D[Sqrt[4 - (x^2 + y^2 + z^2)] {y, -x, 0}, {x, -2, 2}, {y, -2,2}, {z, -2, 2}  
, RegionFunction -> Function[{x, y, z}, 4 > (x^2 + y^2 + z^2)],VectorPoints -> Fine]

enter image description here

or alternatively Boole

VectorPlot3D[Boole[4 > (x^2 + y^2 + z^2)] Sqrt[4 - (x^2 + y^2 + z^2)] {y, -x,0}
, {x, -2, 2}, {y, -2, 2}, {z, -2, 2}  , VectorPoints -> Fine]
$\endgroup$
2
  • $\begingroup$ Beautiful. Thank you! $\endgroup$
    – shanedrum
    Commented Nov 23, 2020 at 12:35
  • $\begingroup$ You're welcome! $\endgroup$ Commented Nov 23, 2020 at 12:38

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.