Animating Plots in Mathematica

A friend asked me how to animate a polar plot graphing in Mathematica. It’s really easy. The Animate and Manipulate commands added in version 6 are a huge step in interactive mathematical visualizations.

The easiest way to visualize it is to have the upper plot range vary from a 0+ϵ to 2π.

 
Animate[
  PolarPlot[2 Cos[4 x], {x, 0, y}, 
    PlotRange -> {{-2, 2}, {-2, 2}}], 
  {y, 0.001, 2}
]

But, I thought this would be a good opportunity to try out Wolfram’s recently introduced Computable Document Format which makes Mathematica files interactive in the web browser. Instead of having to buy an expensive Mathematica license to play with interactive Mathematica tools, CDFs are accessible right from the browser.

If you haven’t already, you’ll have to install the CDF Player (free from Wolfram) to view my animation.1 Try clicking the different functions to see their polar plots drawn.

Here’s code which you should be able to copy and paste right into Mathematica.

 
Manipulate[
  PolarPlot[f[\[Theta]], {\[Theta], 0, \[Mu]},
    PlotRange -> {{-2, 2}, {-2, 2}}
  ], 
    {{f, 2 Cos[4 #] &, "Function"},
    {
      Sin -> "r=Sin[\[Theta]]",
      2 Cos[4 #] & -> "r=2 Cos[4\[Theta]]",
      2 Cos[16 #] & -> "r=2 Cos[16\[Theta]]"
    }, ControlType -> SetterBar},
  {\[Mu], 0.001, 2 \[Pi]},
  ControlType -> Animator,
  AppearanceElements -> None
]
  1. Wolfram has recently made their massive collection of Demonstrations interactive through the CDF format. This is a great feature for math teacher and students. []

2 Responses to “Animating Plots in Mathematica”

  1. Marc December 13, 2012 at 7:01 AM #

    Hi Tim,

    Could you give me a few hints of how to use mathematica syntax highlighting? Are you using the syntaxHighlighter of wordpress?

    Thanks in advance,

    Marc

  2. Bjarke Todbjerg Nielsen February 13, 2013 at 2:59 PM #

    Thanks! I’d been searching high and low for a simple way to animate a numerical solution to a system of differential equations I’m working with and this post was by far the most helpful.
    -Bjarke