How can I have Animate[] create an animation only with the current instances of assigned variables, and then not update dynamically when they are changed later? For instance, if my code were

Animate[Plot[f[a - x], {x, -1, 1}], {a, 0, 2}]

it is going to overwrite the animation based on x^2 with the animation based on x^3. Is there an easy way to stop it from doing that?


1 Answer 1


You need to make Animate independent of f. How do you do that?

Quick and dirty

A non-robust way, which works in this case, would be:

Module[{ff, xx},
 ff = f[xx];
 Animate[Plot[ff /. xx -> a - x, {x, -1, 1}], {a, 0, 2}]

This is unaffected by future redefinitions of f, and works in this this case. Note, however, that this relies upon f[xx] evaluating to something that is both (1) independent of future redefinitions* and (2) the same, upon replacement, as supplying f[a-x] directly. In particular, this would fail if f were initially defined as follows:

(* f[xx] won't evaluate until xx is replaced with a real number *)
f[x_] /; x > 1 := x^2
f[x_] /; x < 1 := x


(* f[xx] will evaluate differently than f[aRealNumber] *)
f[x_Real] := x^2
f[x_] := x

*Even here you could later do something silly like Unprotect[Power]; Power[x_,y__]:=x to mess things up, though I think it's reasonable to assume you're not worried about that.

More robust

 DownValues[ff] = (DownValues[f] /. f -> ff);
 Animate[Plot[ff[a - x], {x, -1, 1}], {a, 0, 2}]

Here you've effectively copied the definition of f into ff.

  • 2
    $\begingroup$ Use DynamicModule and it will even survive across sessions. $\endgroup$
    – Kuba
    Nov 10, 2017 at 21:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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