Animate[x, {x, 0, h}, Initialization :> (h = 1)]

The code above gives the error:

Animate::vsform: Animate argument {x,0,h} does not have the correct form for a variable specification. >>

It's strange to me in that if Animate is replaced by Manipulate, no error is generated. What then is the reason for the error?

  • $\begingroup$ The error message seems clear to me. The 2nd argument of Animate is not a control specification, so you should not expect it have the same semantics as it would have if it were the 2nd argument of Manipulate. What are you really trying to accomplish by using an initialized variable in a range specification? Also, you might consider injecting a value with With. $\endgroup$ – m_goldberg Mar 19 '16 at 13:23
  • $\begingroup$ In reply to the question asked, my original code, containing the structure exemplified by the simple code above, is much longer, and in there the range specification has to be calculated before the expression to be animated. $\endgroup$ – User18 Mar 19 '16 at 14:06
  • $\begingroup$ The reason I put that calculation within Initialization is that if it is placed outside Animate, then each time the notebook of this code is opened, Animate runs automatically - it runs without the range specification, hence errors appear. $\endgroup$ – User18 Mar 19 '16 at 14:08
  • $\begingroup$ Interesting, I thought Animate is just a Manipulate with ControlType -> Animator! $\endgroup$ – xzczd Mar 20 '16 at 4:32

Sjoerd's investigation into the causes of the error is very fascinating, but in order to prevent the error it seems to me that you will want to use the SaveDefinitions -> True option, which "specifies whether current definitions relevant for the evaluation of the expression being manipulated should automatically be saved" (docs).

I executed the following code in a new notebook, then saved the notebook, closed Mathematica, and reopened the saved notebook directly by double-clicking on it. Mathematica warned me about possibly dangerous dynamic content; I chose to enable the dynamic content, after which the animation re-started from where it had been paused without a hitch:

h = 3;
f[x_] := x;

  Graphics[Circle[{0, 0}, f[x]], PlotRange -> 3.6], {x, 0, h},
  SaveDefinitions -> True
  • $\begingroup$ This solution is perfect. Many thanks. $\endgroup$ – User18 Mar 21 '16 at 4:36
  • $\begingroup$ In addition, with this option one can also free themselves from certain cumbersome Initialization code inside Manipulate and Animate. So glad to have learned it. $\endgroup$ – User18 Mar 21 '16 at 5:28
  • $\begingroup$ @User18 I'm very glad I could help! $\endgroup$ – MarcoB Mar 21 '16 at 5:42
  • 1
    $\begingroup$ @User18 It should be noted that SaveDefinitions can be dangerous: 6579644, 31998, 39631. $\endgroup$ – Sjoerd C. de Vries Mar 22 '16 at 19:53
  • $\begingroup$ @Sjoerd Oh, I should have logged in earlier. I see; indeed one should be cautious about it. Thanks. $\endgroup$ – User18 Mar 28 '16 at 15:39

This has to do with the order in which the arguments of Animate are executed. Let's see if we can determine that:

ClearAll[eval, low, hi, init, x];
i = 1;
   eval[If[i > 10, 10, i++]] = x,
   {x, low[i++] = x; 0, hi[i++] = x; 1}, 
   Initialization :> (init[i++] = x;), 
   AnimationRunning -> False

Mathematica graphics

Information /@ {low, hi, init, eval};

Mathematica graphics

So, Mathematica first evaluates Animate's lower bound, then its upper bound, then repeats this process (!?).

It then sets Animate's running variable to the lower bound and only then does it execute the initialization.

Evaluation of the main body is the last step. Note that even though the animation isn't running (because of AnimationRunning -> False) the main body is evaluated continuously, just like in Manipulate since an assignment took place there, triggering a refresh.

Hence, the reason for your error is that you use the value set in the initialization in the animation bounds, but that part is executed earlier than the initialization.

  • $\begingroup$ What a great answer! Thank you very much. $\endgroup$ – User18 Mar 19 '16 at 15:13
  • $\begingroup$ Please allow me to ask a further question. As I mentioned in replying to m_goldberg, Animate runs automatically when the notebook containing it is open, but in certain cases we need the newly opened notebook to automatically calculate something else first, before Animate is activated. Is it possible to ensure that? $\endgroup$ – User18 Mar 19 '16 at 15:25
  • $\begingroup$ I'm afk at the moment. Have you tried AnimationRunning ->False? $\endgroup$ – Sjoerd C. de Vries Mar 19 '16 at 15:28
  • $\begingroup$ Yes I tried that option, but it doesn't work. The following example can illustrate this: $\endgroup$ – User18 Mar 20 '16 at 2:55
  • $\begingroup$ h = 3; f[x_] := x; Animate[Graphics[Circle[{0, 0}, f[x]], PlotRange -> 3.6], {x, 0, h}, AnimationRunning -> False] $\endgroup$ – User18 Mar 20 '16 at 3:12

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.