Strange Error with Initialization within Animate - Mathematica Stack Exchange most recent 30 from mathematica.stackexchange.com 2019-09-18T06:15:51Z https://mathematica.stackexchange.com/feeds/question/110493 https://creativecommons.org/licenses/by-sa/4.0/rdf https://mathematica.stackexchange.com/q/110493 5 Strange Error with Initialization within Animate User18 https://mathematica.stackexchange.com/users/37924 2016-03-19T12:50:55Z 2016-03-20T22:52:26Z <pre><code>Animate[x, {x, 0, h}, Initialization :&gt; (h = 1)] </code></pre> <p>The code above gives the error:</p> <p><em>Animate::vsform: Animate argument {x,0,h} does not have the correct form for a variable specification. >></em></p> <p>It's strange to me in that if <code>Animate</code> is replaced by <code>Manipulate</code>, no error is generated. What then is the reason for the error?</p> <hr> https://mathematica.stackexchange.com/questions/110493/-/110496#110496 4 Answer by Sjoerd C. de Vries for Strange Error with Initialization within Animate Sjoerd C. de Vries https://mathematica.stackexchange.com/users/57 2016-03-19T13:52:30Z 2016-03-19T13:52:30Z <p>This has to do with the order in which the arguments of <code>Animate</code> are executed. Let's see if we can determine that:</p> <pre><code>ClearAll[eval, low, hi, init, x]; i = 1; Animate[ eval[If[i &gt; 10, 10, i++]] = x, {x, low[i++] = x; 0, hi[i++] = x; 1}, Initialization :&gt; (init[i++] = x;), AnimationRunning -&gt; False ] </code></pre> <p><img src="https://i.stack.imgur.com/w5uAo.png" alt="Mathematica graphics"></p> <pre><code>Information /@ {low, hi, init, eval}; </code></pre> <p><img src="https://i.stack.imgur.com/NSx0w.png" alt="Mathematica graphics"></p> <p>So, Mathematica first evaluates <code>Animate</code>'s lower bound, then its upper bound, then <em>repeats this process</em> (!?). </p> <p>It then sets <code>Animate</code>'s running variable to the lower bound and <em>only then does it execute the initialization</em>. </p> <p>Evaluation of the main body is the last step. Note that even though the animation isn't running (because of <code>AnimationRunning -&gt; False</code>) the main body is evaluated continuously, just like in <code>Manipulate</code> since an assignment took place there, triggering a refresh.</p> <p>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.</p> https://mathematica.stackexchange.com/questions/110493/-/110582#110582 1 Answer by MarcoB for Strange Error with Initialization within Animate MarcoB https://mathematica.stackexchange.com/users/27951 2016-03-20T22:52:26Z 2016-03-20T22:52:26Z <p>Sjoerd's investigation into the causes of the error is very fascinating, but in order to <strong>prevent the error</strong> it seems to me that you will want to use the <strong><code>SaveDefinitions -&gt; True</code></strong> option, which "specifies whether current definitions relevant for the evaluation of the expression being manipulated should automatically be saved" (<a href="http://reference.wolfram.com/language/ref/SaveDefinitions.html" rel="nofollow">docs</a>).</p> <p>I executed the following code in a new notebook, then saved the notebook, closed <em>Mathematica</em>, and reopened the saved notebook directly by double-clicking on it. <em>Mathematica</em> 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:</p> <pre><code>h = 3; f[x_] := x; Animate[ Graphics[Circle[{0, 0}, f[x]], PlotRange -&gt; 3.6], {x, 0, h}, SaveDefinitions -&gt; True ] </code></pre>