2
$\begingroup$

I'm trying to figure out how to get animations to render smoother in Mathematica. I'm very new to this but I'd like to get a 30fps animation of something. Here's a simple example:

Animate[Plot[Sin[a*x],{x,-2Pi,2Pi}],{a,-1,1,.05},RefreshRate->40]

However, this is not a smooth animation at all. I tried manipulating both step size and refresh rate, but neither seem to work. Am I missing something? I tried this both in cloud and desktop.

$\endgroup$

3 Answers 3

5
$\begingroup$

Generate the slides outside of the Animate, i.e.

slides = Table[Plot[Sin[a*x], {x, -2 Pi, 2 Pi}, PlotRange -> {{-2 Pi, 2 Pi}, {-1, 1}}],
               {a, -1, 1, .01}];
Animate[slides[[i]], {i, 1, 201, 1}, RefreshRate -> 40]

Otherwise, Animate will try to generate them on the fly, which can slow things down.

Hopefully this helps achieve what you're after.

$\endgroup$
4
  • $\begingroup$ Thanks for the response! This still seems to be giving me very slow framerates. $\endgroup$
    – rb612
    Commented Sep 20, 2017 at 4:52
  • $\begingroup$ Maybe my computer is faster so I didn’t notice that. Perhaps export the slides as a GIF, at which point they’d be at a standard speed? $\endgroup$
    – ktm
    Commented Sep 20, 2017 at 4:53
  • $\begingroup$ This works! Thanks! Why does the methods I presented in my question not work? $\endgroup$
    – rb612
    Commented Sep 20, 2017 at 5:15
  • 1
    $\begingroup$ @rb612 "Otherwise Animate will try to generate them on the fly, which can slow things down." $\endgroup$
    – ktm
    Commented Sep 20, 2017 at 13:33
2
$\begingroup$

ListAnimate is probably what you're looking for. Set up your slides:

slides = Table[
   Plot[Sin[a*x], {x, -2 Pi, 2 Pi}, 
    PlotRange -> {{-2 Pi, 2 Pi}, {-1, 1}}], {a, -1, 1, 0.05}];

Then ListAnimate allows you to specify the frames per second directly in the second argument:

ListAnimate[slides, 40]

Obviously, you can mess with the stepsize in your Table to get the length of the animation right.

$\endgroup$
2
  • $\begingroup$ Thank you for the answer! This looks like it should work, but the animation is being generated when I call it, so it's still running at a few fps. It won't even run when I'm at 40fps $\endgroup$
    – rb612
    Commented Sep 20, 2017 at 4:51
  • $\begingroup$ @rb612 It won't be re-generating the plots when you call ListAnimate. ListAnimate just runs through whatever list you give it. All I can think is that there's something else sucking up your resources (unless you're using SetDelayed, :=, or something). $\endgroup$ Commented Sep 20, 2017 at 8:00
1
$\begingroup$

I was close to giving up on Mathematica, which I'd bought for visualisation as much as for its symbolic abilities, but recently I found that using Dynamic[] gives much smoother animations than Animate or Manipulate

Execute this in one cell

Dynamic[Plot[Sin[a*x], {x, -2 Pi, 2 Pi}]]

And this in another

For[a = -1, a < 1, a = a + .05, Pause[0.1]]

You may need to adjust the step size and the pause duration.

$\endgroup$

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.