3
$\begingroup$

StreamPlot likes to sample points outside the plot region. For example,

res = Reap[StreamPlot[{Sqrt[x], y}, {x, 0, 1}, {y, 0, 1},
  EvaluationMonitor :> Sow[{x, y}]]];
Show[res[[1]], ListPlot[res[[2]]]]

Mathematica graphics

As in this example, this is typically not a problem. However, I have some numerically defined functions that are not well defined outside the region, which I'd like StreamPlot to avoid. Is there an option or other trick to force StreamPlot to not evaluate outside the plot region?

$\endgroup$

2 Answers 2

1
$\begingroup$

You can also use ConditionalExpression:

res = Reap[StreamPlot[
        ConditionalExpression[{Sqrt[x], y}, 0 <= x <= 1 && 0 <= y <= 1], 
    {x, 0, 1}, {y, 0, 1}, 
    EvaluationMonitor :> Sow[{x, y}], StreamStyle -> Pink]];
Show[res[[1]], ListPlot[res[[2]]]]

Mathematica graphics

$\endgroup$
1
$\begingroup$

Gave it a little more thought & came up with a workable solution based on Condition (/;):

f[x_, y_] := Sqrt[x] /; (0 <= x <= 1 && 0 <= y <= 1);
g[x_, y_] := y /; (0 <= x <= 1 && 0 <= y <= 1);

res = Reap[StreamPlot[{f[x, y], g[x, y]}, {x, 0, 1}, {y, 0, 1},
  EvaluationMonitor :> Sow[{x, y}], StreamStyle -> Pink]];
Show[res[[1]], ListPlot[res[[2]]]]

Mathematica graphics

$\endgroup$

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.