Saturday, May 17, 2008

Animations in Mathematica

Download Mathematica notebook [right click / save as]

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




Download Mathematica notebook [right click / save as]

Cite as:
Saad, T. "Animations in Mathematica". Weblog entry from Please Make A Note. http://pleasemakeanote.blogspot.com/2008/05/create-mathematica-swf-animation.html

8 comments:

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

    ReplyDelete
  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!

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

    ReplyDelete
  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?

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

    AnimationRepetitions->Infinity

    I hope that works.

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

    ReplyDelete
  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.

    ReplyDelete
  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.

    ReplyDelete