# Strange Error with Initialization within Animate

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?

• 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. – m_goldberg Mar 19 '16 at 13:23
• 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. – User18 Mar 19 '16 at 14:06
• 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. – User18 Mar 19 '16 at 14:08
• Interesting, I thought Animate is just a Manipulate with ControlType -> Animator! – 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;

Animate[
Graphics[Circle[{0, 0}, f[x]], PlotRange -> 3.6], {x, 0, h},
SaveDefinitions -> True
]

• This solution is perfect. Many thanks. – User18 Mar 21 '16 at 4:36
• 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. – User18 Mar 21 '16 at 5:28
• @User18 I'm very glad I could help! – MarcoB Mar 21 '16 at 5:42
• @User18 It should be noted that SaveDefinitions can be dangerous: 6579644, 31998, 39631. – Sjoerd C. de Vries Mar 22 '16 at 19:53
• @Sjoerd Oh, I should have logged in earlier. I see; indeed one should be cautious about it. Thanks. – 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;
Animate[
eval[If[i > 10, 10, i++]] = x,
{x, low[i++] = x; 0, hi[i++] = x; 1},
Initialization :> (init[i++] = x;),
AnimationRunning -> False
]


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


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.

• What a great answer! Thank you very much. – User18 Mar 19 '16 at 15:13
• 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? – User18 Mar 19 '16 at 15:25
• I'm afk at the moment. Have you tried AnimationRunning ->False? – Sjoerd C. de Vries Mar 19 '16 at 15:28
• Yes I tried that option, but it doesn't work. The following example can illustrate this: – User18 Mar 20 '16 at 2:55
• h = 3; f[x_] := x; Animate[Graphics[Circle[{0, 0}, f[x]], PlotRange -> 3.6], {x, 0, h}, AnimationRunning -> False] – User18 Mar 20 '16 at 3:12