# Plotting a vector function composed of piecewise scalar functions

I am relatively new to Mathematica, and am having difficulty doing something that is conceptually very simple.

I am trying to plot a vector field in which the vector function is composed of piecewise scalar functions.

f[a_] := Piecewise[{{a, Abs[a] >= 0.2}, {0, Abs[a] < 0.2}}];
StreamPlot[{f[x], f[y]}, {x, -1, 1}, {y, -1, 1}]


The code above produces an empty graph.

If anyone can spot the problem, I would be very much obliged.

The following works:

Clear[f];
f[a_?NumericQ] := Piecewise[{{a, Abs[a] >= 0.2}, {0, Abs[a] < 0.2}}];

StreamPlot[{f[x], f[y]}, {x, -1, 1}, {y, -1, 1}] It looks like the symbolic preprocessing of f produces an empty list of streamlines, so I added the qualifier ?NumericQ to the definition of f to suppress this preprocessing. I wouldn't have expected this plot to fail with your original definition, so there does seems to be something wrong here.

Another workaround is this:

Clear[f];
f[a_] := Piecewise[{{a, Abs[a] >= 0.2}, {0, Abs[a] < 0.2}}];
g[x_, y_] := {f[x], f[y]};
StreamPlot[g[x, y], {x, -1, 1}, {y, -1, 1}]


The plot appears just as above, simply by inserting the intermediate step of combining the two components into a function g[x,y].

• Hello Jens, this problem had confounded me, thanks for your help! – SongWithoutWords Mar 27 '16 at 2:27
• It does look like a bug to me. I also get the same failure with VectorPlot... and it can also be fixed without using ?NumericQ as I note in the edited answer. Strange. – Jens Mar 27 '16 at 2:30