4
$\begingroup$

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.

$\endgroup$
5
$\begingroup$

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}]

streamplot

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].

$\endgroup$
  • $\begingroup$ Hello Jens, this problem had confounded me, thanks for your help! $\endgroup$ – SongWithoutWords Mar 27 '16 at 2:27
  • $\begingroup$ 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. $\endgroup$ – Jens Mar 27 '16 at 2:30

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.