StreamPlot No Longer Works With Piecewise Function?
It does seem to be a bug. As a temporary workaround, you can re-express your function in terms of either UnitStep[]
or Boole[]
, like so:
f[x_] = Dot @@ MapAt[Boole, Internal`FromPiecewise[
Piecewise[{{2 x - 3, x > 1}, {-x, -1 <= x <= 1}, {2 x + 3, x < -1}}]], 1]
StreamPlot[{y - f[x], -x}, {x, -5, 5}, {y, -5, 5}]
Note the use of the undocumented function Internal`FromPiecewise[]
for decomposing the Piecewise[]
object.
A workaround:
g[x_] := -Boole[Abs[x] < 1] x + Boole[Abs[x] > 1] (2 x - 3 Sign[x])
Now,
StreamPlot[{y - g[x], -x}, {x, -5, 5}, {y, -5, 5}]
yields an incorrect plot!
Using Graphics
:
Graphics[Catenate@
Table[{Arrowheads[0.01],
Arrow[{{i, j}, {i, j} + 0.2 Normalize[{j - g[i], -i}]}]}, {i, -5,
5, 0.4}, {j, -5, 5, 0.4}]]