Animation - Bending time to your will

There are several different commands in Mathematica you can use to animate objects.  Personally, I prefer to use the Table command; I find it to be the most flexible.  Basically, it works like this:

Table[ stuff to do , {counter, start value, stop value, step size} ]

So, for example, to animate a sin curve with an amplitude that varies from -6 to 6, counting by 2's, you could do:

Table[Plot[k Sin[x], {x, -2π, 2π}], {k, -6, 6, 2}]

[Graphics:../HTMLFiles/index_116.gif]

{⁃Graphics⁃, ⁃Graphics⁃, ⁃Graphics⁃, ⁃Graphics⁃, ⁃Graphics⁃, ⁃Graphics⁃, ⁃Graphics⁃}

Notice a couple of things about this:  

When you run the command, it generates a bunch of separate graphs; these are the "frames" of your animation.  To see it "run", double-click on the first frame.  (If you don't want to see all the individual frames, double-click on the bracket to the right of the picture that contains all the frames - the second from the left.)

More disturbingly, the amplitude doesn't actually seem to change.  Actually, if you look carefully, you can see it does, but Mathematica automatically rescales all the pictures to show the graph as large as possible, so they all look the same.  To fix this,  you need to use the PlotRange option to tell Mathematica exactly what viewing window to use for all your frames:

Table[Plot[k Sin[x], {x, -2π, 2π}, PlotRange {-6, 6}], {k, -6, 6, 2}]

[Graphics:../HTMLFiles/index_126.gif]

{⁃Graphics⁃, ⁃Graphics⁃, ⁃Graphics⁃, ⁃Graphics⁃, ⁃Graphics⁃, ⁃Graphics⁃, ⁃Graphics⁃}

A very handy trick you can also use is to give the Table a name and then Show the table, drawing all the "frames" together on a single graph:

fred = Table[Plot[k Sin[x], {x, -2π, 2π}, PlotRange {-6, 6}], {k, -6, 6, 2}]

[Graphics:../HTMLFiles/index_136.gif]

{⁃Graphics⁃, ⁃Graphics⁃, ⁃Graphics⁃, ⁃Graphics⁃, ⁃Graphics⁃, ⁃Graphics⁃, ⁃Graphics⁃}

Show[fred]

[Graphics:../HTMLFiles/index_139.gif]

⁃Graphics⁃

The Table command can be used for a lot of other things as well (including nesting tables within tables).  The "counter" variable (k in my example) can be thought of as time in this usage, however.


Created by Mathematica  (February 28, 2007) Valid XHTML 1.1!