6

I have following code

Animate[
Plot[myvariable x,{x,0,100}
],
{myvariable,0.1,1},AnimationRunning->If[myvariable==1,False,True]
]

How do I make the animation stop automatically when reaching a defined value?

NOTE: I tried to make it stop by evaluating myvariable in a If expression, but I get the error "An unrecognized option name ("AnimationRunning") was encountered while reading a AnimatorBox."

elhombre
  • 367
  • 2
  • 7

2 Answers2

7

Mathematica is very flexible when it comes to these sorts of things. You can easily build your own little animation, and add as much complexity to the logic of termination by using Dynamic directly.

For example, this plots Cos[a x] and stops the animation when a>0.2.

Clear[p];
Dynamic[p]
Do[p = Plot[Cos[a x], {x, 0, 100}, Frame -> True, 
   FrameLabel -> {{None, None}, {x, 
      Style[Column[{"doing my own animation !", Cos[a x]}, 
        Alignment -> Center], 14]}}, GridLines -> Automatic];
 If[a > 0.2, Break[]]; (*condition to stop*)
 Pause[.2],
 {a, 0.1, 1, .01}
 ]

Mathematica graphics

Nasser
  • 143,286
  • 11
  • 154
  • 359
3

The condition in your case is to stop at the maximum value of the parameter. This can be achieved by setting the number of animation repetitions to 1 (i.e., no looping):

Animate[Plot[myvariable x, {x, 0, 100}], {myvariable, 0.1, 1}, 
 AnimationRunning -> True, 
 AnimationRepetitions -> 1
]
Jens
  • 97,245
  • 7
  • 213
  • 499