4
$\begingroup$

While plotting Plot[Ceiling[FractionalPart[x]], {x, 0, 3}] I noticed that the dips at integers were not being plotted. Is there a way to achieve that?

enter image description here

$\endgroup$
3
  • 1
    $\begingroup$ Probably not. Plot[] does discrete sampling and it’s unlikely it would detect the integer values. Further it tends to exclude discontinuities, so if detected, would skip them. Look up PlotPiecewise on this site for one approach $\endgroup$
    – Michael E2
    Dec 21, 2019 at 0:21
  • $\begingroup$ @MichaelE2 the code at mathematica.stackexchange.com/questions/39445/… does the job but is mighty long. Is that the PlotPiecewise you alluded to or some in-built? $\endgroup$
    – lineage
    Dec 21, 2019 at 0:28
  • $\begingroup$ Yep, that’s it. It is long. $\endgroup$
    – Michael E2
    Dec 21, 2019 at 0:30

1 Answer 1

4
$\begingroup$

Use the options Exclusions, PlotPoints and Method as follows:

Plot[Ceiling[FractionalPart[x]], {x, 0, 3},  
 PlotPoints -> {30, Range[0, 3]}, 
 Exclusions -> None, 
 Method -> {"BoundaryOffset" -> False}]

enter image description here

Alternatively, use ParametricPlot with the options Exclusions and PlotPoints:

ParametricPlot[{x, Ceiling[FractionalPart[x]]}, {x, 0, 3}, 
 PlotPoints -> {30, Range[0, 3]}, 
 Exclusions -> None]

enter image description here

Note the special form of the option value for PlotPoints (see see this answer by Ullrich Neumann).

$\endgroup$
3
  • 1
    $\begingroup$ could you please explain what PlotPoints -> {30, Range[0, 3]} means? $\endgroup$
    – lineage
    Dec 21, 2019 at 0:38
  • 3
    $\begingroup$ @lineage, it says sample 30 points in the domain but also include the points {0,1,2,3}. $\endgroup$
    – kglr
    Dec 21, 2019 at 0:40
  • 2
    $\begingroup$ @kglr that’s a really neat undocumented feature! I’d never seen it before; thank for bringing it to my attention! $\endgroup$
    – MarcoB
    Dec 21, 2019 at 5:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.