From: Alexey on
Hello,
I am wondering that sometimes Plot[] takes a huge number of points
where a function changes very slowly. And it takes less points where
the function changes rapidly! Consider the following:

cache = {};
f[x_?NumericQ] := (AppendTo[cache, x]; 5 Csc[x]);
pl = Plot[f[x], {x, 0, 2}, PlotRange -> {Full, {0, 140}}];
Show[Histogram[cache], pl, PlotRange -> {All, {0, 140}}]
cache // Length

It is easy to see that Plot takes 122 points around x=1.5 - where the
function is nearly constant! But around x=0.3 (where the function
changes rapidly) it takes only 50 points!

Can anyone explain this?

From: Murray Eisenberg on
Yes, it's well known that Mathematica does that. And well documented.
See, for example, the "More Information" section of the page ref/Plot in
the Documentation Center.

But I don't think you'll find documented an explicit algorithm for how
the number of points is selected beyond that vague statement. That's
verging on what may be proprietary information.

On 2/17/2010 7:01 AM, Alexey wrote:
> Hello,
> I am wondering that sometimes Plot[] takes a huge number of points
> where a function changes very slowly. And it takes less points where
> the function changes rapidly! Consider the following:
>
> cache = {};
> f[x_?NumericQ] := (AppendTo[cache, x]; 5 Csc[x]);
> pl = Plot[f[x], {x, 0, 2}, PlotRange -> {Full, {0, 140}}];
> Show[Histogram[cache], pl, PlotRange -> {All, {0, 140}}]
> cache // Length
>
> It is easy to see that Plot takes 122 points around x=1.5 - where the
> function is nearly constant! But around x=0.3 (where the function
> changes rapidly) it takes only 50 points!
>
> Can anyone explain this?
>

--
Murray Eisenberg murray(a)math.umass.edu
Mathematics & Statistics Dept.
Lederle Graduate Research Tower phone 413 549-1020 (H)
University of Massachusetts 413 545-2859 (W)
710 North Pleasant Street fax 413 545-1801
Amherst, MA 01003-9305