Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This question already has an answer here:

I've tried animating two different sets of 3D plots. Interestingly, one plot will always work, regardless of whether I restart Mathematica, yet another plot will only work in its current Mathematica session.

For instance, I have:

h = 1;
α = 1;
H = (1/2)*(p^2 + x^2);
rho[n_] = (1/Pi)*(-1)^n*Exp[-2*H/h]*LaguerreL[n, 4 H/h];
list = ParallelTable[Plot3D[rho[n], {x, -10, 10}, {p, -10, 10}, PlotRange -> All,
PlotLabel -> Style[n, FontSize -> 40], ImageSize -> 800, 
AxesLabel -> {Style[x, FontSize -> 30], 
  Style[p, FontSize -> 30]}], {n, 0, 10}];
ListAnimate[list, AnimationRate -> 1, AnimationRunning -> False]

This yields: enter image description here:

If I save my file and restart Mathematica, while none of my variables are initialize, the animation is still able to run.

However, I have another piece of code to make a different animation:

α = 0.15;
h = 1;
η = 1;
a = -n - Sqrt[1 + 16 r];
r = η^2/(4*α^2);
qrn = (1/2)*(n + 1/2) + (1/4)*Sqrt[1 + 16 r];
c = 1 - 2*qrn;

Clear[x, p]
ψ[p_] = (1/(1 + α*p^2)^qrn)*  Hypergeometric2F1[a, -n, c, (1/2) +    (I/2)*Sqrt[α]*p]

For[i = 0, i <= 2, i++, Clear[list2]]
For[n = 0, n <= 2, n++, 
list2[n] = Append[list2[n], 
ParallelTable[{x = RandomReal[{-5, 5}], p = RandomReal[{-10, 10}],
    Conjugate[ψ[p + u/2]]*ψ[p - u/2], {u, -10000, 
    10000}, MaxRecursion -> 12]}, {40}]]] // AbsoluteTiming
Animate[ListPlot3D[list2[n][[2]], PlotRange -> All, ImageSize -> 400], {n, 0, 2, 1}, AnimationRunning -> False]

This gives:

enter image description here

After saving, and restarting Mathematica, I now get:

enter image description here

Creating my animation with 40 points only takes a couple seconds, so it isn't terrible that I'd have to recreate it. However my plan is to increase the number of points to more than 12000 and increase n to 10, which will take much more time than a couple seconds.

Why does one animation run after re-opening Mathematica, yet another animation will give me an error message?

As a related question, is there a way for Mathematica to always remember the values of my variables, even after restarting Mathematica?


share|improve this question

marked as duplicate by Michael E2, Mr.Wizard Jul 30 '14 at 18:19

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

The data list2 is not regenerated on a new kernel automatically. – RunnyKine Jul 30 '14 at 17:06
Is there a way to regenerate it on a new kernel? Alternatively, is there a way to modify my code to bypass this problem? – user85503 Jul 30 '14 at 17:10
Add SaveDefinitions -> True – RunnyKine Jul 30 '14 at 17:14

To answer the first question: ListAnimate[list] stores the list in the output, while Animate[code,...] stores only the code. Animate does not store the definitions of symbols referenced by code. If code is not self-contained, then the definitions will have to be executed before Animate will run properly.

To answer the second question, which is also answered in saving Manipulate data inside a notebook, as well as in a comment by @RunnyKine: To store the definitions , use Animate[code,..., SaveDefinitions -> True]. Note that according to the documentation, Animate creates a Manipulate and takes all the Manipulate options. In some cases, using the Initialization option may be better. (In the present case, since storing a large amount of calculated data is the object, SaveDefinitions seems more convenient.)

share|improve this answer
I propose that we close this question as a duplicate of 11705 as I doubt the first question applied to a broad audience. +1 for the answer however. – Mr.Wizard Jul 30 '14 at 18:03
@Mr.Wizard I think linking them as duplicates seems right. I was confused because it looks like RunnyKine voted to close as a dupe, but there's no close vote. (?) – Michael E2 Jul 30 '14 at 18:15
Ha, I voted to close as "Easily found in documentation" before I found the duplicate, but since the system doesn't allow you to change your vote (I think that's crazy), I retracted mine (and you're not allowed to vote again). – RunnyKine Jul 31 '14 at 0:36

Not the answer you're looking for? Browse other questions tagged or ask your own question.