Mathematica Animations

for

Teaching Mathematics

by deej heath

The Minnesota Academy of

Mathematics & Science

Many has been the student to say "I need to visualize it in order to understand it." This paper contains a collection of animations to help students in visualizing mathematics, particularly Calculus. Mathematica code follows those animations where minor changes to the code produces useful variants of the animation. Comments designed to help teachers and students to make their own similar animations are offered.

The code does not always match the linked animation in every detail; the effort in this exposition has been to make the code as useful, flexible, and easy to use as possible, whereas the effort in making the animations was to make the animation as easy to understand as possible. There have been times when these two goals have been at odds with each other. On the assumption that most teachers do not know Mathematica well enough to produce their own animations without useful, flexible code and instructions, this paper may err in favor of making the code easy to use rather than on assuring that the animations produced are always picture perfect. Those who want better looking animations are encouraged to learn enough Mathematica to make more than superficial changes to the code presented.

Each of the pieces of code shown produces a sequence of graphs. If you double click on any of the graphs, Mathematica will animate the sequence. While it is animating, go to the Cell menu, and chose Convert To -> QuickTime. This will convert the sequence into a QuickTime animation that can be run from your desktop without using Mathematica. The animation created will have dependencies, meaning that it will only run on your own computer. To make an animation that can be transported via disc or ftp-ed via internet, you will need to Save As to a stand alone movie from the file menu of Quicktime Player.

Some comments for those with difficulties: every character typed into Mathematica may be important, including spaces. For this reason you should copy and paste the code from this document into Mathematica rather than retyping. This method has been used to recreate each animation; the code in this document produces useful animations in Mathematica 4. If you have difficulties with any of the code as pasted directly from this page, you may need to Quit Kernel -> Local from the Kernel menu of Mathematica and reenter it.

I. Algebra

1. The Focus of a Parabola: The linked animation shows the graph of a parabola and light rays coming in from above, together with the reflection of the rays. Each of the reflected rays hits one particular point, which is the focus of the parabola. This experiment can be done with a parabolic mirror, at least one laser pointer (two is best), and chalk dust (so that the path of light of the lasers can be seen), and is more fun for the students. If you have these items available, consider using them rather than this animation.

2. The Focus and Directrix of a Parabola: This animation shows the concept of the directrix of a parabola. It should be clear that the distance from focus to parabola equals the distance from directrix to parabola.

3. Foci of an Ellipse: This animation shows how the distance from the foci of an ellipse to the points on the ellipse remain constant, regardless of choice of point. This experiment can be accomplished using a piece of string, two thumbtacks, and a pencil. If you have these items available, please consider using them rather than this animation.

II. Trigonometry

1. The Wrapping Function: The linked animation presents the wrapping function in graphical form. Note that the height of the sine function is the same as the height of the y-coordinate of the unit circle.

III. Differentiation

1. Definition of the derivative: The following code generates the graph of a function and its secant lines in "n" incrementing (or decrementing) positions. The secant lines should slow down as they approach the tangent line, and the last secant line should look so close to the tangent line that it can be mistaken for the derivative. Here "a" is the left endpoint of the domain, "b" is the right endpoint, "c" is the bottom of the range, and "d" is the top. The point "p" is the point at which the derivative is to be taken, and the point "q" is the furthest point from which the secant lines are drawn. The number of frames in the animation is "n." If n is chosen to be small, the animation will generate quickly, but look choppy. If n is chosen large, the animation will take some time to generate, but will look smooth once finished. For experimentation, choosing n from 5 to 10 is usually sufficient. For a polished animation, 30 to 40 frames is usually best. The function is defined in the statement f[x_]:=Sin[2 x] Cos[x] +Cos[3/2 x] Sin[2 x];.

Note that each "k^2" can be replaced by a simple "k," and the animation will not change drastically. The k^2 was used to stress the idea that the closer points are more important in taking the derivative that the further points; they make the animation slow down as it approaches p.

a = 0; b = 2 Pi; c = -2; d = 2; p = 2.2; q = 5; n = 20;

f[x_] := Sin[2 x] Cos[x] + Cos[3/2 x] Sin[2 x];

Do[ParametricPlot[

{

{x, f[x]},

{x, (f[q k^2 + p - p k^2] - f[p])/(q k^2 - p k^2) (x - p) +f[p]},

{p(1 - x/(b - a)) + (q k^2 + p(1 - k^2)) x/(b - a), (f[q k^2 + p - p k^2] - f[p])/(q k^2 - p k^2) (p(1 - x/(b - a)) + (q k^2 + p(1 - k^2)) x/(b - a) - p) +f[p]}

},

{x, a, b},

PlotStyle -> {Hue[0], Hue[1/3], Hue[2/3]},

PlotRange -> {c, d}],

{k, 1, .001, -1/n}]

2. The Derivative Function: The following code generates the graph of a function, its tangent lines in "n" incrementing positions, and its derivative function, which is shown being created simultaneously with the tangent lines. Here "a" is the left endpoint of the domain, "b" is the right endpoint, "c" is the bottom of the range, and "d" is the top. The number of frames in the animation is "n." The function is defined in the statement f[x_]:=Sin[x].

To change the code for a new function, consider the example of y=x^(1/2) on the interval [0,4]. Then a=0 and b=4. The range of x^(1/2) on that interval is [0,2], and its derivative has a range from [1/4, infinity), so the natural range for both would be [0,infinity). It is easy to choose c=0, but Mathematica cannot graph all the way to infinity, so a reasonable bound is chosen more or less at random: d=4. This can be reset later if the graph is not sufficient. The first line below is replaced with the following: "a=0; b=4; c=0; d=4; n=10; f[x_]:=x^(1/2);". After one attempt, it is noted that Mathematica has difficulty with the point a=0. Replacing the troublesome point with a=.01 solves the problem.

a = -Pi; b = Pi; c = -2; d = 2; n = 10; f[x_] := Sin[x];

Do[ParametricPlot[

{

{x, f[x]},

{x, f'[k](x - k) + f[k]},

{a + (x - a)(k - a)/(b - a), f'[a + (x - a)(k - a)/(b - a)]}

},

{x, a, b},

PlotRange -> {c, d},

PlotStyle -> {Hue[0], Hue[1/3], Hue[2/3]}],

{k, a, b, (b - a)/n}]

3. The Second Derivative Function: In this animation, the green line segment represents the acceleration vector; it points up when the curve is concave up and points down when the curve is concave down. The norm of the vector represents the magnitude of the second derivative function, which is graphed simultaneously in blue. It should be noted that as concavity is changing, the green acceleration vector disappears and the blue 2nd-derivative function crosses the x-axis.

a = -1; b = 3; c = -3; d = 10; n = 29;

f[x_] := (x^2)^(1/3) (x - 2)^2;

Do[ParametricPlot[

{

{x, f[x]},

{k, f[k] + (x - a)/(b - a) f''[k]},

{a + (x - a)(k - a)/(b - a), f''[a + (x - a)(k - a)/(b - a)]}

},

{x, a, b},

PlotRange -> {c, d},

PlotStyle -> {Hue[0], Hue[1/3], Hue[2/3]}],

{k, a, b, (b - a)/n}]

4. Implicit Differentiation: The point here is that the derivative can be taken regardless of whether the curve is defined by a function or not. This is a difficult point for some students.

The following code generates the graph of the parametrically defined curve {t^2-1,t^3-t} in red, its tangent lines in "n" incrementing positions (shown in green), and its derivative curve in blue, which is shown being created simultaneously with the tangent lines.

Here "a" is the starting point for the parameter "t," and "b" is the ending point. The number of frames in the animation is "n." For this curve on this interval, n was chosen odd so as to skip the point where the tangent line is vertical. By way of experiment, resetting n=10 will make this clear. The parameterized curve is defined in the statements xx[t_]:=t^2-1; yy[t_]:=t^3-t. One more line needs comment: PlotRange defines limits of the display rectangle. If the curve is changed this also may need to be edited.

In the code, the derivative appears first, followed by the curve, followed by the tangent line. The reason for this choice of order is to give the user the option of erasing the asymptote(s) of the derivative curve without erasing sections of the other curves. If the asymptote bothers you, here is the trick to get rid of it. One line before the indented line of code reading {xx[t],yy[t]},insert another line reading "{-1, t},". Then change the line "PlotStyle -> {Hue[2/3], Hue[0], Hue[1/3]}" to read "PlotStyle->{Hue[2/3], RGBColor[1,1,1], Hue[0], Hue[1/3]}". This will erase the blue vertical line by drawing a white vertical line over it. Of course if you change the functions xx and yy you will need to calculate the asymptote(s) of the derivative yourself and add appropriate white lines to erase them.

a = -2; b = 2; n = 9; xx[t_] := t^2 - 1; yy[t_] := t^3 - t;

Do[ParametricPlot[

{

{xx[(k - a)(t - a)/(b - a) + a], yy'[(k - a)(t - a)/(b - a) + a] / xx'[(k - a)(t - a)/(b - a) + a]},

{xx[t], yy[t]},

{t, yy'[k]/xx'[k] (t - xx[k]) + yy[k]}

},

{t, a, b},

PlotStyle -> {Hue[2/3], Hue[0], Hue[1/3]},

PlotRange -> {{-1.5, 1.5}, {-1.5, 1.5}}],

{k, a, b, (b - a)/n}]

5. Optimization: Here are a couple animations which visually demonstrate functions which have a maximum, even though it is not clear exactly where the maximum is.

Volume of a Cone: A cone is created by taking a sector (pie slice) of a disc and gluing the edges together. The cone created from a small sector of the disc has a small radius and a large height, and the cone created from a large sector of the disc has a large radius and a small height. So what size of sector produces the cone of largest volume?

Area of a Rectangle: A rectangle can be inscribed in an ellipse in many ways. If the height is large, the width is small, and vice versa. So what dimensions will give the rectangle of largest area?

IV. Integration

1. Reimann Sums: To shade under a curve in Mathematica requires a bit more work. The first line of code simply loads a graphics package which is needed to shade between curves. Next, the functions and variables are set up: "a," "b," "c," and "d" signify the domain and range as before, while "n" gives the number of frames in the animation. The function that will be integrated using a Reimann sum is defined in the statement "f[x_] := Exp[-x^2];". The next statement defines a function of two variables which is the step function obtained from f[x] by dividing the interval into "m" subintervals and evaluating the function at the midpoint of each subinterval. To evaluate at the left-hand endpoint, simply eliminate the "+(b-a)/(2m) " near the end of the statement defining the function g[x_,m_]. To evaluate at the right-hand endpoint, eliminate the "+(b-a)/(2m)" as before, and change the word "Floor" in the same statement into "Ceiling".

Needs["Graphics`FilledPlot`"];

a = 0; b = 2; c = 0; d = 1; n = 10;

f[x_] := Exp[-x^2];

g[x_, m_] := f[(b - a)/m Floor[(x - a)/(b - a) m] + a + (b-a)/(2m)];

Do[h[k] = FilledPlot[

{g[x, k], 0},

{x, a, b},

PlotRange -> {c, d},

PlotStyle -> RGBColor[1, 1, 1]],

{k, 1, n}];

i = Plot[f[x], {x, a, b}, PlotStyle -> Hue[2/3], PlotRange -> {c, d}];

The output from the above commands will be a bunch of graphs which must now be pieced together using the following. Be careful: if you place the following command on the same line as the last set of commands you will get the final product, but it will have the preliminary graphs attached to it, so it will need more work on your part to make an animation from it.

Do[Show[h[k], i], {k, 1, n}]

2. The Integral Function: This animation also makes use of the FilledPlot command, so the first line simply loads it. The variables in the second line are the same as per all previous animations. The function, as usual, is defined in the statement f[x_]:=Exp[-x^2];. The next command plots a sequence of graphs: h[k] shows the function f[x] shaded up to the kth subinterval (out of n), i[k] plots the integral function for f[x] in red, and "j" shows the function in dark blue.

For some functions, especially piecewise defined functions, Mathematica has difficulty with the NIntegrate[f[t],{t,a,x}] statement. In these cases consider integrating the function by hand and inserting the formula for the integral in the place of the NIntegrate command.

If you have just used the code for (1) above, you may need to Quit Kernel -> Local from the Kernel menu of Mathematica before this code will run correctly.

Needs["Graphics`FilledPlot`"];

a = -2; b = 2; c = 0; d = 2; n = 10;

f[x_] := Exp[-x^2];

Do[

h[k] = FilledPlot[
{f[x], 0},

{x, a, k b/n + a(1 - k/n)},

PlotRange -> {c, d},

PlotStyle -> RGBColor[1, 1, 1]];

i[k] = Plot[NIntegrate[f[t], {t, a, x}],

{x, a, k b/n + a(1 - k/n)},

PlotStyle -> Hue[0],

PlotRange -> {c, d}],

{k, 1, n}];

j= Plot[f[x], {x, a, b}, PlotStyle -> Hue[2/3], PlotRange -> {c, d}];

Now that the preliminaries are finished, the pieces can be put together by entering the following command.

Do[Show[h[k], i[k], j], {k, 1, n}]

3. Arc Length: This animation shows the idea used to find the arc length of a curve. The curve is broken into subsections, each of which is estimated by a straight line. At first the curve and the line segments don't look anything alike, but very soon it becomes difficult to tell them apart, except for sections of high curvature. The function used to graph the line segments which approximate the curve becomes much slower as the number of subintervals increases, so beware of using large values of n.

a = -1; b = 2; c = -2; d = 1.5; n = 12; f[x_] := Sin[3 x] + Cos[5 x];

Do[

g[k_, x_] :=
Sum[((f[a + (b - a)(i + 1)/k] - f[a + (b - a)i/k])k/(b - a) (x - a - (b - a)i /k) + f[a + (b - a)i/k]) If[x < a + (b - a) i/k, 0, If[x > a + (b - a)(i + 1)/k, 0, 1]],

{i, 0, k - 1}],

{k, 1, n}];

Do[

Plot[{g[k, x], f[x]},

{x, a, b},

PlotStyle -> {Hue[0], Hue[2/3]},

PlotRange -> {c, d}],

{k, 1, n}]

V. Taylor Series

1. Converging polynomials: This animation shows how Taylor polynomials converge to the function they are estimating. The variables a, b, c, and d as usual define the domain and range. The variable "p" is the center of the Taylor Series. In the example, since p=0, the series is the MacLaurin Series. Each frame number k from 0 to n graphs both the curve f[x] and the kth degree Taylor Polynomial. Although the ends seem to "whip around wildly," in the animated form, the middle is converging rapidly.

a = -4; b = 4; c = -2; d = 2; p = 0; n = 10; f[x_] := Sin[x] + Cos[x];

Do[m[k][t_] := Evaluate[Normal[Series[f[t], {t, p, k}]]], {k, 0, n}];

Do[Plot[{f[x], m[k][x]}, {x, a, b}, PlotRange -> {c, d},

PlotStyle -> {Hue[0], Hue[2/3]}], {k, 0, n}]

VI. Miscellany

1. Parametrized curves, hypocycloids: The code shown graphs the hypocycloid created by tracing the path of a point on a circle of radius "b" when it is rolled around on the inside of a circle of radius "a." As usual, "n" refers to the number of frames. We found that for larger values of "a" and "b" the variable "pts" was also needed. It is used to reset the number of points Mathematica plots to produce the final graph. If you find the image looking "choppy," set "pts" to a larger value. To produce an exterior hypocycloid, make b negative and change "PlotRange -> {{-a, a}, {-a, a}}" to read "PlotRange -> {{-a+2b, a-2b}, {-a+2b, a-2b}}". To demonstrate a standard cycloid, consider rolling a coffee can (with a point on the circumference marked) on a desk or table rather than making an animation.

a = 5; b = 2; n = 36; pts=60;

Do[ParametricPlot[

{

{a Sin[t], a Cos[t]},

{(a - b)Sin[b k] + b Sin[t], (a - b)Cos[b k] + b Cos[t]},

{(2 Pi - t)/(2 Pi) (a - b) Sin[b k] + t/(2 Pi) ((a - b)Sin[b k] - b Sin[(a - b) k]), (2 Pi - t)/(2 Pi) (a - b) Cos[b k] + t/(2 Pi) ((a - b)Cos[b k] + b Cos[(a - b) k])},

{(a - b) Sin[b t k/(2 Pi)] - b Sin[(a - b) t k/(2 Pi)], (a - b) Cos[b t k/(2 Pi)] + b Cos[(a - b) t k/(2 Pi)]}

},

{t, 0, 2 Pi},

PlotStyle -> {Hue[0], Hue[1/3], Hue[1/2], Hue[2/3]},

PlotRange -> {{-a, a}, {-a, a}},

AspectRatio -> 1,

PlotPoints -> pts,

Axes -> None,

ImageSize -> 100],

{k, 4 Pi/n, 2 Pi, 2 Pi/n}]

2. Polar Coordinates: This animation demonstrates the idea of the polar graph of a function defined with respect to an angle t. The graph shows the function f[t] simultaneously graphed in Polar coordinates at the origin, and in Cartesian coordinates shifted to the right by "c" units. Notice that the height (in blue) of the Cartesian graph is the same as the distance from the origin (in blue) of the Polar graph, and the sign of the Cartesian graph gives the direction of measure of the polar graph. Depending on the function that you use, you may find that you will need to fiddle with the PlotRange and the AspectRatio. As usual "a" and "b" refer to the starting and ending points, and "n" refers to the number of frames in the animation. The green curve shows the radius of the unit circle pointing in direction t. The purple curve shows the arc of the unit circle that has been so far subtended. If you wish to delete these, remove the first two lines after the line containing only a left brace symbol "{", and remove the "Hue[1/3],Hue[5/6]," from the PlotStyle assignment.

a = 0; b = Pi; c = 3.5; n = 12; f[t_] := Cos[t];

Do[

ParametricPlot[
{

{Cos[k] (t - a)/(b - a), Sin[k] (t - a)/(b - a)},

{Cos[a + (t - a)(k - a)/(b - a)], Sin[a + (t - a)(k - a)/(b - a)]},

{Cos[k] (t - a)/(b - a) f[k], Sin[k] (t - a)/(b - a) f[k]},

{f[a + (t - a)(k - a)/(b - a)] Cos[a + (t - a)(k - a)/(b - a)], f[a + (t - a)(k - a)/(b - a)] Sin[a + (t - a)(k - a)/(b - a)]},

{c + a + k, (t - a)/(b - a) f[k + a]},

{c + a + (t - a)(k - a)/(b - a), f[(t - a)(k - a)/(b - a) + a]}

},

{t, a, b},

PlotStyle -> {Hue[1/3], Hue[5/6], Hue[2/3], Hue[0], Hue[2/3], Hue[0]},

PlotRange -> {{-1, 7}, {-1, 1}},

AspectRatio -> 1/3,

ImageSize -> 250,

Ticks -> None],

{k, a, b, (b - a)/n}]

VII. Concluding Remarks

I would like to thank the Minnesota Academy of Mathematics and Science for creating an environment in which experimentation in the teaching of mathematics is not merely tolerated but actively encouraged. It is my sincerest wish that the ideas contained in this paper may prove useful to others in helping students to visualize mathematics. I encourage others to publish parallel papers using other computer algebra systems, and will assist with technical details. I am also willing to assist teachers who would like to create educational animations that cannot be produced using this short list. To this end, I may be contacted at deej@mps.org.