16

When plotting a slow function, it would be nice to know how much of the work has already be done. However due to the refinement algorithm, simply monitoring the integration variable doesn't give an useful estimate unless MaxRecursion is set to 0 (and it's clear that the best I can hope for is an estimate). For example, consider

Monitor[Plot[Pause[0.01];Sin[x],{x,0,100}],x]

This goes through the interval dozens of times, although the number of points in each iteration goes down.

Therefore my question: Is there any way to get a reasonable estimate of how much of a plot is already done?

celtschk
  • 19,133
  • 1
  • 51
  • 106
  • 3
    Interesting question. The problem is that you can't predict how many points will be added in each refinement step. Here's a refinement function to experiment with, if you like. I've been using both this code as well as a 2D version of it with a very slow to compute function, and from experience: the number of points added depends very much on the function: it could increase exponentially or stay near-constant. – Szabolcs May 24 '12 at 08:39
  • However, if you know your function well (as I did), you'll be able to take good guesses. – Szabolcs May 24 '12 at 08:44
  • @Szabolcs: Is that the algorithm Plot uses, or is it an alternative sampling algorithm to be used instead for a self-written Plot replacement? – celtschk May 24 '12 at 09:11
  • I don't know the precise details, and my memory is not great, but from what I remember I read before, Plot should use either the same or a very similar algorithm, with a threshold angle of 5 degrees. It will refine the sampling grid if the angle between tho consecutive line segments exceeds 5 degrees. (The code is mine, I didn't take it from Plot) – Szabolcs May 24 '12 at 09:17
  • 4
    @Szabolcs and celtschk: the old package Graphics\Spline` featured a set of subroutines that were essentially explicit reimplementations of the algorithm used withinPlot[]/ParametricPlot[]`. You might wish to take a look at them. – J. M.'s missing motivation May 24 '12 at 10:33
  • Progress bars are notoriously difficult to code. Maybe that's why Microsoft stopped using them in the update procedure. – nilo de roock May 24 '12 at 10:39

1 Answers1

9

If you're willing to sacrifice automaticity on MaxRecursions, you could do this

SetAttributes[celtschkPlot, HoldAll];
celtschkPlot[fun_, {v_, r1__}, mr_: 6, op : OptionsPattern[]] := 
 Module[{i = 0, xant},
  Monitor[
   Plot[fun, {v, r1},
    MaxRecursion -> mr,
    EvaluationMonitor :> (If[v < xant, ++i]; xant = v), op],
   ProgressIndicator[i, {0, 2^mr}]]]

So, try

celtschkPlot[Pause[0.001]; Sin[2 Pi t], {t, 0, 10}]

and play with the functions. The last argument specifies the max recursion with defaults to a hard 6

celtschkPlot[Pause[0.001]; Sin[2 Pi t], {t, 0, 10}, 6]

This would be an upper bound. You could get the happy surprise that the plot finishes early

Rojo
  • 42,601
  • 7
  • 96
  • 188
  • 1
    As an alternative you could use EvaluationMonitor :> (If[v<ant, i++]; xant = v). – Heike May 24 '12 at 10:01
  • @Heike, thank god I have an alternative. My code is disgusting – Rojo May 24 '12 at 10:04
  • @Heike, there we go, thanks – Rojo May 24 '12 at 10:14
  • I wanted to give a counterexample or the computation steps growing exponentially, but it turned out that even in my example they do exactly that ... ListPlot[Reverse@ Table[Part[ Reap@Plot[1/(1 + Exp[-10 x]), {x, -3, 3}, Mesh -> All, EvaluationMonitor :> Sow[x], MaxRecursion -> k], 2, 1], {k, 0, 3}], PlotRange -> All] – Szabolcs May 24 '12 at 10:19
  • @Szabolcs, in any case, if it works for most cases, even when you don't know in which, it's probably good enough to be useful as an "estimate" I think – Rojo May 24 '12 at 10:24
  • Thanks, that's a quite nice solution. Indeed I am willing to sacrifice automaticity on MaxRecursion because for those plots where it matters I have to do that anyway to get my plot in a reasonable time. – celtschk May 24 '12 at 10:31
  • Thanks for the accept @celtschk, I hope it was not because of the function name. I'll be editing the "which I you're probably not" out – Rojo May 24 '12 at 10:33
  • Of course it was not because of the function name. I did a few tests, and surprisingly often the progress bar gives a quite good estimate. – celtschk May 24 '12 at 10:53
  • BTW, further experiments make me think that the automatic MaxRecursion for Plot is just 6 anyway. – celtschk May 24 '12 at 12:44