8
$\begingroup$

The following gives an empty plot on V12 under Windows 10:

f[x_, n_] := Piecewise[{{1, n < x < n + 1}, {0, True}}]
Plot[f[x, 7], {x, -30, 30 }, PlotRange -> {All, {-1, 1}}, Exclusions -> None]

Mathematica graphics

Changing the range to make it little smaller, now shows it as:

Plot[f[x, 7], {x, -20, 20 }, PlotRange -> {All, {-1, 1}}, Exclusions -> None]

Mathematica graphics

Also increasing Plot points and using the longer range, it now shows as:

Plot[f[x, 7], {x, -30, 30 }, PlotRange -> {All, {-1, 1}},
 Exclusions -> None, PlotPoints -> 20]

Mathematica graphics

OK, so I thought I needed more PlotPoints and that was all. But changing f[x, 7] to f[x,8] now shows it using the longer range and without changing the PlotPints:

Plot[f[x, 8], {x, -30, 30 }, PlotRange -> {All, {-1, 1}}, Exclusions -> None]

Mathematica graphics

So there must be something else. It can't be the PlotPoints. Why would f[x, 7] need more points than f[x,8]?

The function only generates this:

Table[f[x, 7], {x, 7, 8, .1}]

Mathematica graphics

Table[f[x, 8], {x, 8, 9, .1}]

Mathematica graphics

BTW, this has nothing to do with odd or even numbers. It seems random. For example,

Plot[f[x, 2], {x, -30, 30}, PlotRange -> {All, {-1, 1}}, Exclusions -> None]

It also gives an empty plot.

The sampling algorithm for Plot seems to miss all the points from f[x, 7], but not from f[x, 8]. Why is that?

$\endgroup$
7
$\begingroup$

Your plots aren't empty: they have a line along the x axis.

Plot uses PlotPoints -> 50 by default, and since you go from -30 to 30, there will be some $n$ to $n+1$ intervals that don't get sampled:

In[66]:= Reap[
 Plot[x, {x, -30, 30}, MaxRecursion -> 0, 
   EvaluationMonitor :> Sow[x]];]

Out[66]= {Null, {{-30., -28.8223, -27.5455, -26.3533, -25.1845, \
-23.9167, -22.7335, -21.4512, -20.1922, -19.0179, -17.7446, -16.5558, \
-15.3904, -14.126, -12.9462, -11.6673, -10.4118, -9.24087, -7.97091, \
-6.78556, -5.50115, -4.24013, -3.06371, -1.78823, -0.597365, 0.570117,
    1.83666, 3.0186, 4.2996, 5.55721, 6.73021, 8.00228, 9.18974, 
   10.3538, 11.6169, 12.7955, 14.0731, 15.266, 16.4356, 17.7043, 
   18.8883, 20.1714, 21.4312, 22.6063, 23.8804, 25.07, 26.2362, 
   27.5014, 28.6821, 30.}}}

7 to 8, corresponding to f[x, 7] is one such interval. Since we don't sample between those values, we never see f[x,7] == 1 and the resulting plot is zero everywhere.

$\endgroup$
  • $\begingroup$ But this does not really explain why it shows the pulse when using other values of n for same range and same plot points? For example f[x, 2] it misses it, but f[x, 8] it does not. Same range. So is it hit and miss type of thing? ps. I know the plot is not empty, (I see the line y=0 there) I was lazy :) and just meant the pulse itself is not showing. I should be more clear. $\endgroup$ – Nasser Sep 30 '19 at 4:48
  • 4
    $\begingroup$ @Nasser Essentially, you see the pulse if the initial set of sampling points of Plot contains the pulse (if not, Plot has no reason to increase the sampling). If you look at the sampled points, you'll see that there are no points in $[2,3],[7,8],[13,14],[19,20],[24,25],[29,30]$, so if the pulse is in any of those intervals, Plot will miss it, otherwise it will find it $\endgroup$ – Lukas Lang Sep 30 '19 at 12:45
6
$\begingroup$

Brett's answer explains the why.

To force sampling at specified points you can use an undocumented form of PlotPoints (see this answer by Ullrich Neumann) :

f[x_, n_] := Piecewise[{{1, n < x < n + 1}, {0, True}}]

Plot[f[x, 7], {x, -30, 30}, 
  PlotPoints -> {50, {7}}, 
  PlotStyle -> CapForm["Butt"],
  Axes -> False, Frame -> True, 
  PlotRange -> {-1, 1}, 
  PlotRangePadding -> Scaled[.05]]

enter image description here

Add Exclusions -> None to get the vertical portions:

enter image description here

$\endgroup$

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.