6
$\begingroup$

I am really baffled by this. Call to StreamPlot does not return empty plot, nor an error message, nor a beep. It just return unevaluated.

I do not think I've seen something like this before. What would the cause for this?

Is this expected behavior?

Normally, when a plot can not be generated, either an error is returned or empty plot.

Mathematica graphics

ClearAll[x, y, f]; 
f = (x*y - Sqrt[-1 + x^2 + y^2])/(-1 + x^2); 
StreamPlot[{1, f}, {x, -2, 2}, {y, -2, 2}]

btw, it will plot OK when changing the -1 to 1 in the above, under the square root:

Mathematica graphics

So it seems due to value of the function becomes complex over some region. But normally when this happens, an empty plot is returned, right?

V 12 on windows 10.

$\endgroup$
  • $\begingroup$ Same on V10.0.1 on Mac OSX 10.12. $\endgroup$ – march Aug 12 at 19:49
  • $\begingroup$ StreamPlot[{I, x}, {x, -2, 2}, {y, -2, 2}] returns unevaluated, so maybe that's what StreamPlot does with complexes, except when it crashes. :) -- the docs suggest it might be the way you say: "StreamPlot does not show streamlines at any positions for which the Subscript[v, i] etc. do not evaluate to real numbers." $\endgroup$ – Michael E2 Aug 12 at 21:50
9
$\begingroup$

I think it should give an error message, perhaps. I agree with the OP's guess that problem stems from the complex values of the vector field. It may be that it's because the field is real around the boundaries and then becomes complex. Here is a workaround:

StreamPlot[
 Evaluate[ConditionalExpression[#, # ∈ Reals] & /@ {1, f}],
 {x, -2, 2}, {y, -2, 2}]

Update: Here is another that seems to be more robust. It zeros out the complex-valued parts, and zero vectors are not drawn nor lead to stream lines:

StreamPlot[Boole[f ∈ Reals] {1, f}, {x, -2, 2}, {y, -2, 2}]

enter image description here

$\endgroup$
  • 1
    $\begingroup$ very good work around. It also solved the kernel crash in this why-call-to-streamplot-terminates-kernel-each-time so if you like to post your workaround there also, I'll be happy to accept it as well. $\endgroup$ – Nasser Aug 12 at 21:23
  • $\begingroup$ Sorry, but I found a big problem with the above. Now other StreamPlot call crash. Not this example, but other ones when I changed the code to use your trick. Should I open a new question? Here is an example: f = (6*(2*x^4*y^(2/3) + y))/x; StreamPlot[ Evaluate[(ConditionalExpression[#1, Element[#1, Reals]] & ) /@ {1, f}], {x, -2, 2}, {y, -2, 2}] When I remove the code you added, it works. StreamPlot[{1, f}, {x, -2, 2}, {y, -2, 2}] So it looks like your method fixed some, but also caused some others not to work. I am using the same command to process hundreds of functions. $\endgroup$ – Nasser Aug 12 at 22:15
  • $\begingroup$ Is it possible to make it work for both examples? Or should I open a new question for the new example that does not work? $\endgroup$ – Nasser Aug 12 at 22:17
  • $\begingroup$ I'll see what I can do. I get Hold[Throw[Null, "IntraStepEvaluationException"]] from the example in the comment. (Mac -- maybe a version difference.) $\endgroup$ – Michael E2 Aug 12 at 22:26
  • 1
    $\begingroup$ @Nasser See if this gets any mileage: StreamPlot[ Evaluate[Boole[fff \[Element] Reals] {1, fff}], {x, -2, 2}, {y, -2, 2}] $\endgroup$ – Michael E2 Aug 12 at 22:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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