3
$\begingroup$

I was looking at the Documentation Center for Round, Ceiling and Floor when I noticed that the plots for Round showed some variation, even at the small size that the Documentation Center displays them, when blown up... well:

Plot[Round[x, 10], {x, -30, 30}, Filling -> Axis]

Rounding at 10

Plot[Round[x], {x, -3, 3}, Filling -> Axis]

Rounding at 1

This intrigued me, I don't fully understand the rules governing generation of plots from functions. Clearly the method is adaptive, generating more data points where there is "activity," but is the (dis)continuity just based on the resolution that the plot is generated using?

Any insight is welcome.

$\endgroup$
4
  • $\begingroup$ You get a similar result if you use Round[x,1]. This uses 163 points, the same as Round[x,10] whereas without, it uses 175 points. You'll find some general info on how the points are chosen in this answer $\endgroup$
    – rm -rf
    Sep 26, 2012 at 18:01
  • $\begingroup$ note also Round[x] and Round[x,1] result in differenc evaluaiton points. How do you get those markers? $\endgroup$
    – george2079
    Sep 26, 2012 at 18:03
  • $\begingroup$ @george2079 If you repeatedly click on the plot, you'll select individual lines in the graphics editing tool $\endgroup$
    – rm -rf
    Sep 26, 2012 at 18:11
  • $\begingroup$ Related: mathematica.stackexchange.com/q/8482/121 $\endgroup$
    – Mr.Wizard
    Feb 20, 2013 at 1:23

2 Answers 2

3
$\begingroup$

This is probably not a complete answer. Plot attempts at some point to detect discontinuities symbolically. With a few built-ins, it can do it. Round[x] is an example but for some reason Round[x, 1] is not

Those discontinuities are excluded from the plots (see Exclusions)

So, for example, compare the output of these

Plot[Round[x], {x, -3, 3}]
Plot[Round[x, 1], {x, -3, 3}]

If you define r[x_?NumericQ]:=Round[x], then r[x] behaves the same way as Round[x, 1] because it doesn't know how to manipulate your custom r symbolically

Now, if Plot already knows beforehand it has a discontinuity somewhere, it can be smarter than usual. For example, it can try to make the discontinuities sharper by sampling right on the sides.

x1 = Reap[
     Plot[Round[x, 1], {x, -3, 3}, Filling -> Axis, 
      EvaluationMonitor -> Sow[x]]][[-1, 1]] // Rest;
x2 = Reap[
     Plot[Round[x], {x, -3, 3}, Filling -> Axis, 
      EvaluationMonitor -> Sow[x]]][[-1, 1]] // Rest;
Complement[x2, x1]

{-2.50191, -2.49809, -1.50191, -1.49809, -0.501913, -0.498087, \ 0.498087, 0.501913, 1.49809, 1.50191, 2.49809, 2.50191}

As to the rendering issues with the Filling, that's because it excludes discontinuities by default. Try with Exclusions->None

Plot[Round[x], {x, -3, 3}, Filling -> Axis, Exclusions -> None]

Related question: Managing Exclusions in Plot[ ]

$\endgroup$
4
  • $\begingroup$ So it's all in how the system is handling the exclusions that are generated by Round[x] but not in Round[x,1] ? Considering that they are supposed to be equivalent expressions: Round[x]=Round[x, 1] Round[x_,round_:1]:= I guess I'm curious to know what Wolfram implemented to have it handle that way. Sigh, the never-ending depths of Mathematica's mysteries... $\endgroup$
    – MRN16
    Sep 27, 2012 at 12:10
  • $\begingroup$ The link you included was very helpful in understanding the "interesting point detection" methodology that Mathematica employs and a little bit about it's exclusion detection method (thanks to your input it appears). Thanks for all your help! $\endgroup$
    – MRN16
    Sep 27, 2012 at 12:23
  • $\begingroup$ @MRN16 glad to help, thanks for the accept $\endgroup$
    – Rojo
    Sep 27, 2012 at 13:18
  • $\begingroup$ V9 computes exclusions for the two-argument form of Round. $\endgroup$ Nov 29, 2012 at 17:15
2
$\begingroup$

There are two excellent threads on StackOverflow that explore the inner workings of Plot sampling:

Answer from Yaroslav Bulatov

Answer from Alexey Popkov

One can get linear sampling by using the option MaxRecursion -> 0, and control the sampling rate with PlotPoints:

Plot[Round[x, 10], {x, -30, 30},
 Filling -> Axis,
 Mesh -> All, 
 MaxRecursion -> 0,
 PlotPoints -> 80
]

Mathematica graphics

$\endgroup$
1
  • $\begingroup$ Thanks for the links, I sometimes forget that the Mathematica questions can be found in the other stack websites too. $\endgroup$
    – MRN16
    Sep 27, 2012 at 12:16

Your Answer

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

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