## Saturday, May 17, 2008

### Animations in Mathematica

A good way of presenting your work is to use animations. This is true when you have an analytical model where you need to explore the behavior of the solution under a change of certain control parameters. There's an easy way of doing this using Mathematica. Here is a sample code in mathematica
(* Define function to be animated *)
f[x_, L_] = Cos[L*x];
(* Create a table of plots *)
theTable = Table[ Plot[{f[x, L]}, {x, -4, 4}], {L, 1, 10}];
(* Export this table into an animated GIF - Replace gif with swf to export as a flash swf file*)
Export["C:\\TestAnimation.gif", theTable, ImageSize->350];
The animation will look like this

Voila!

Of course, you need to set the parameters that are relevant to your animation. You can also animate several functions. In this case, include the functions in the Plot section of the table. For example
(* Define functions to be animated *)
f[x_, L_] = Cos[L*x];
g[x_, L_] = Sin[L*x];
(* Create a table of plots *)
theTable = Table[ Plot[{f[x, L], g[x, L]}, {x, -4, 4}], {L, 1, 10}];
(* Export this table into an animated GIF - Replace gif with swf to export as a flash swf file*)
Export["C:\\TestAnimationCombo.gif", theTable, ImageSize->350];
The animation in this case looks like this

Cite as:

1. Thanks, this was very helpful for an mathematical animation I was working on.

2. Brilliant! I spent the last two days trying to export animations from Mathematica 7, to no avail. Your simple no-nonsense examples are a breath of fresh air!

3. Thank you Dave! I am glad that this post was helpful for you. I greatly appreciate your feedback!

4. I followed your instructions, but my animated gif file stops after one loop through the plots. I am using Mathematica 5.2 on Linux and Firefox 3.0.7 to view the gif file. How do I put the animation in an "infinite repeat" loop?

5. Thanks for your comment. Try adding the following option to the Export command:

AnimationRepetitions->Infinity

I hope that works.

6. Interesting, thanks so much for sharing, this can be very useful if you know how to use it.

7. I was looking for this information, thanks for put in this easy way, I mean in a easy way to understand it jajaja, well until the next time.

8. thanks I wasn't just able to do it as I was using manipulate and simply replaced it by Table ans it worked great.