3
$\begingroup$

I've been trying to restart an animation after variables change from Manipulate PopUpMenus. My animation takes the form:

Animate[expr, {u, umin, umax, du}]. 

I can change u, after variations to Manipulate, back to an initial value momentarily, but from there u jumps back to its previous position (or farther forward) as if its continued running. I would be content with simply restarting the entire animation after changing the Manipulate variables.

My code for Animate[] is in the following simplified form (I hope to make the i slider bar return back to the left and the red dot to begin being plotted at x=-10 each time the function is changed):

gPrev[x_] := Sin[x];
Manipulate[
 Switch[functionNumb, 
   0, g[x_] := Sin[x], 
   1, g[x_] := Cos[x], 
   2, g[x_] := ArcTan[x]], 
 {functionNumb, 0, 2, 1}]

Animate[
 Plot[{g[x]}, {x, -10, 10}, 
   Epilog -> {Red, PointSize[0.015], Point@{i, g[i]}}], {i, -10, 10, 1}, 
   DisplayAllSteps -> True, AnimationRunning -> False]
$\endgroup$
  • 1
    $\begingroup$ if you post a (formatted) minimal working example you will probably get more/better answers. $\endgroup$ – Ajasja Jun 4 '13 at 14:42
  • $\begingroup$ You are welcome, but the code snippet is not working. It is also not exactly minimal:) $\endgroup$ – Ajasja Jun 4 '13 at 15:08
  • $\begingroup$ Simplified, working code posted, thanks again. $\endgroup$ – twhoward99 Jun 4 '13 at 16:01
  • $\begingroup$ It's much better now (+1). Hmm I see where the problem is. Well, let me first give you an improved version of your original code. Manipulate[ Animate[Plot[{f[x]}, {x, -10, 10}, Epilog -> {Red, PointSize[0.015], Point@{i, f[i]}}], {i, -10, 10, 1}, DisplayAllSteps -> True, AnimationRunning -> False], {f, {Sin, Cos, ArcTan}}] $\endgroup$ – Ajasja Jun 4 '13 at 16:18
  • $\begingroup$ So basically the problem is how to reset the i index from the animation? Sorry can't see an obvious way right now. $\endgroup$ – Ajasja Jun 4 '13 at 16:23
3
$\begingroup$

Not a complete answer yet: I've built the functionality using DynamicModule and Animator, thinking that it will be possible to manually set the index i when using the second argument of Dynamic. Sadly this only works if the animation is not running. So if the animation is not running i is reset correctly. Still, here is the code

EDIT Stealing from Kuba, I'm 80% there. The animation is restarted, but the old animation state is lost. In other words, changing the function will always run the animation.

DynamicModule[{i = -10, f = Sin, run = False}, 
 Column@{SetterBar[
    Dynamic[f, (i = -10; run = False; FinishDynamic[]; run = True; 
       Print@i; f = #) &], {Sin, Cos, ArcTan}], 
   Dynamic@Animator[Dynamic[i], {-10, 10, 0.1}, 
     ContinuousAction -> True, AnimationRunning -> run], 
   Dynamic@Plot[{f[x]}, {x, -10, 10}, ImageSize -> Medium, 
     Epilog -> {Red, PointSize[0.015], Point@{i, f[i]}}]}]
$\endgroup$
  • $\begingroup$ Hmm, there is an "ResetButton" under AppearanceElements, but I don't know how to call it from code. $\endgroup$ – Ajasja Jun 4 '13 at 16:51
  • $\begingroup$ Ahh, You are faster :) I think it works like twhoward99 wanted. +1 $\endgroup$ – Kuba Jun 4 '13 at 17:08
  • $\begingroup$ I can't upvote due to my meager reputation, but thank-you so much for your help! $\endgroup$ – twhoward99 Jun 4 '13 at 17:18
2
$\begingroup$

First I've give one way that fits with the setup in your question, and then I'll give the way I would do it myself, in case that helps.


Wrap Animate in Dynamic and make it track g; also keep the Manipulate from tracking g. Wrapping Animate will make it reset whenever g is changed.

gPrev[x_] := Sin[x];
Manipulate[
 Switch[functionNumb, 0, g[x_] := Sin[x], 1, g[x_] := Cos[x], 2, 
  g[x_] := ArcTan[x]]; g[x], {functionNumb, 0, 2, 1}, 
 TrackedSymbols :> {functionNumb}]

Dynamic@Animate[
  Plot[{g[x]}, {x, -10, 10}, 
   Epilog -> {Red, PointSize[0.015], 
     Dynamic@Point@N@{i, g[i]}}], {i, -10, 10, 1}, 
  DisplayAllSteps -> True, AnimationRunning -> False, 
  TrackedSymbols -> {g}]

Here's a single Manipulate that has the same functionality, as I see it, and a more integrated look.

Manipulate[
 Column[{
   i = -10; 
   Animator[Dynamic[i], {-10, 10, 1}, AnimationRunning -> False],
   Plot[function, {x, -10, 10}, 
    Epilog -> {Red, PointSize[0.015], Dynamic@Point@N@{i, function /. x -> i}},
    ImageSize -> Medium]
   }],
 {function, {Sin[x], Cos[x], 2 ArcTan[x]}},
 {i, -10, 10, 1, ControlType -> None},
 TrackedSymbols :> {function}]

enter image description here

Putting the Animator in the control area is a bit trickier, it seems. It can be done, but my solution is a bit too kludgy to post.

$\endgroup$
  • $\begingroup$ what exactly it is that makes your second example work (I mean that makes the plot update correctly and the animator reset correctly when you change the function)? I ask because if you for example change the Plot[...] bit with a more simple Graphics like Graphics[{Dynamic@Point@{i, i}}, PlotRange -> {{-10, 10}, {-10, 10}}] it doesn't work anymore, as changing the function doesn't reset i. I cannot see the difference between these these two cases: why does one work and the other not? $\endgroup$ – glS Nov 10 '16 at 19:16
  • $\begingroup$ @glS It's because plot output depends on function but your Graphics[] does not. When function is changed in your case, it does not trigger an update. $\endgroup$ – Michael E2 Nov 10 '16 at 23:30

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.