1
$\begingroup$

I am trying to plot a sum of multiple HeavisidePi functions. A typical example would be something like this:

Plot[HeavisidePi[1/10 (-595 + x)] + HeavisidePi[1/10 (-535 + x)] + 
  HeavisidePi[1/10 (-425 + x)] + HeavisidePi[1/10 (-365 + x)], {x, 
  300, 800}, Exclusions -> None]

In this interval in the code above, I can see all four of them. However, if I were to back out to an interval of {100,800}, for instance, only two of them show up for me (the one at 595 and 425). Any idea why this would be the case?

Any help would be greatly appreciated.

$\endgroup$
5
$\begingroup$

There is another way to control the sample points which is effective in this case, where you know in advance where the interesting things happen. PlotPoints takes an optional second argument, which is a list of extra points to be added to the other sample points.

Since in this case we know where the function is nonzero, we can add a point from each region thus: PlotPoints -> {Automatic, {365, 425, 535, 595}}. In this way we can avoid adding unneeded sample points.

Plot[HeavisidePi[1/10 (-595 + x)] + HeavisidePi[1/10 (-535 + x)] + 
  HeavisidePi[1/10 (-425 + x)] + HeavisidePi[1/10 (-365 + x)],
 {x, 100, 800}, Exclusions -> None, 
 PlotPoints -> {Automatic, {365, 425, 535, 595}}]

Plot output

$\endgroup$
  • $\begingroup$ Interesting. This doesn't seem to be documented. Where did you learn about this? $\endgroup$ – Sjoerd C. de Vries May 8 '13 at 13:56
  • $\begingroup$ @SjoerdC.deVries I'm afraid I don't recall precisely - I think a friend, who has worked with some of the developers at WRI. It's the subject in this question. $\endgroup$ – Michael E2 May 8 '13 at 14:31
  • $\begingroup$ Thanks for the link. Hadn't seen that one before. $\endgroup$ – Sjoerd C. de Vries May 8 '13 at 15:06
1
$\begingroup$

This is an issue of sampling. You need to increase the number of initial sampling divisions (using the PlotPoints option) or Mathematica will not "explore" the area further as it appears to be uniformly zero.

Plot[HeavisidePi[1/10 (-595 + x)] + HeavisidePi[1/10 (-535 + x)] + 
  HeavisidePi[1/10 (-425 + x)] + HeavisidePi[1/10 (-365 + x)], {x, 100, 800}, 
 Exclusions -> None, PlotPoints -> 250]

enter image description here

See the excellent StackOverflow posts linked in this answer: Problem with ParametricPlot
These will explain the sampling that Plot uses and some options to tune it.

$\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.