MATH 162 Calculus II Computer Laboratory Topic: Introduction to Mathematica & Goals of the lab: To learn some basic operations in Mathematica, such as how to define a function, and how to produce various types of plots in 2 and 3 dimensions. To get an introduction to parametrizations of curves and surfaces. 1 A Simple Introduction 1.1 Starting up Start Mathematica (located on the dock). A blank notebook will appear. The things you type will go into a cell. While a blank notebook does not contain any cells, the moment you type, one will open up. As you continue to type, the cell grows as well. If you place the cursor somewhere else in the notebook where there is no cell then, upon typing, a different cell will be created. 1.2 Commands Mathematica has many built-in commands and functions, which may be entered and executed in a cell. The interpreter is case-sensitive (one of many possible syntactical pitfalls), and all built-in Mathematica commands begin with a capital letter. The arguments (inputs) to a command or function must be enclosed in square brackets. Parentheses are used in Mathematica exclusively for grouping expressions. Type the commands just as they appear below into a new cell. Note the underscore character that appears after the x, and the colon before the equals sign. f[x_] := Sin[x]/x Plot[f[x], {x,-3*pi,3*pi}] To execute these commands, which define a function f and then produce a graph in the viewing window 3π x 3π, hit the key marked Enter (or,
alternatively, hit Shift- Return ). Several new output cells will open up underneath your input cell. Many commands in Mathematica have a minimal syntax (like the Plot[ ] command above), but allow a number of switches so as to tailor the results. If you execute the command Options[Plot] you will see a list of switches which may be added inside Plot[ ]. They generally take the form OptionName -> Setting. Type and execute this command to see the effect of a couple of switches. Plot[f[x], {x,-10, 10}, PlotRange->{-1,2}, PlotStyle->Hue[1]] Commands for graphing functions of 2 variables are not much different than the one above. Say we want the graph of f(x, y) = cos x sin y. We may get it directly without naming the function first (i.e., calling it f), as in Plot3D[Cos[x]*Sin[y], {x,0,2*pi}, {y,0,2*pi}] or we may give it a name. When we do that, we can call the input variables x and y, or whatever names we like. f[doris_, Harold_] := Cos[Doris]*Sin[Harold] Plot3D[f[x,y], {x,0,2*pi}, {y,0,2*pi}, ColorOutput->GrayLevel] Without too much modification from the previous two plotting commands (only the name of the plotting function has been changed), we can produce a contour plot (i.e., a collection of level sets) for this same function f. ContourPlot[f[x,y],{x,0,2*Pi},{y,0,2*Pi}] Mathematica can do symbolic manipulations as well as make plots. For instance, to get f/ x and 2 f/ x 2 for the function f(x, y) = 4xy/(x 2 + y 2 ) we may use the commands f[x_,y_] := 4*x*y/(x^2 + y^2) D[f[x,y], x] D[f[x,y], {x, 2}] respectively. If we want f x expressed as a single fraction (that is, for Mathematica to find a common denominator between the two terms and combine them), we might type instead. Together[D[f[x,y], x]] 2
1.3 A little practice You may recall that we considered in class the function f(x, y) = 4xy/(x 2 + y 2 ). We found that, along every straight-line path to the origin, the function was constant, but that the constant changed for different lines. Hence, lim (x,y) (0,0) f(x, y) does not exist. Use the Mathematica commands you ve learned so far (modify as needed) to view both surface and contour plots of this function in a region of the domain surrounding the point (0, 0). Do these look anything like you expect? View these same two plots for the function g(x, y) = 4xy 2 /(x 2 + y 2 ). From what you see, would you expect the limit lim (x,y) (0,0) g(x, y) to exist? 2 2.1 Parametrized curves In MATH 161 you learn about parametric equations of the form x = f(t), y = g(t), a t b. If x and y vary continuously with t (i.e., if f and g are continuous functions of t), then the set of points (x(t), y(t)) for a t b form a curve C in the plane, traced out as t varies from a to b. The functions f and g, along with the interval [a, b], are said to be a parametrization of the curve C. The variable t is said to be the parameter. A parametrization of the unit circle (the circle centered at (0, 0) of radius 1) is provided by x = cos(s), y = sin(s), 0 s 2π. Type the command ParametricPlot[{Cos[s], Sin[s]}, {s, 0, 2*Pi}, AspectRatio -> Automatic] To get a better idea of what is going on, type these commands just as you see them here. << Graphics Animation 3
Animate[ GraphicsArray[{{ ParametricPlot[{Cos[s], Sin[s]}, {s, 0, T}, AspectRatio -> Automatic, PlotRange -> {{-1, 1}, {-1, 1}}, AxesLabel -> {"x", "y"}], Show[Graphics[{Hue[1], Line[{{0, 0}, {T, 0}}]}, PlotRange -> {{0, 2*Pi}, {-0.1, 0.1}}, Axes -> {True, False}, AxesLabel -> {"s", ""}]] }}], {T, 0, 2*Pi, 0.2}] The result will be some 30 frames or so. Once all frames have been generated, you may double-click any one of them to see them played (repeatedly) like a movie. What the movie should reveal is that, as s goes from 0 to 2π, the circle is traced out counterclockwise, starting and ending at the point (1, 0). The points (x, y) from a particular parametrization can always be produced by many other parametrizations. For instance, one can go clockwise, starting from (0, 1), around the unit circle with the parametrization x = sin(s), y = cos(s), 0 s 2π. and go around twice by letting s go through the larger interval 0 s 4π. Most any curve you can think of can be parametrized. For any continuous function y = f(x), a x b, this expression parametrizes the graph. (If one feels a separate parameter is required, then you could think of it as x = t, y = f(t), a t b, where the parameter t has been artificially introduced, and is no different than x.) But many interesting curves that do not correspond to functions y = f(x) (i.e., they fail the vertical line test) may be parametrized as well. Try out the one generated by ParametricPlot[{Cos[5*t], Sin[3*t]}, {t, 0, 20}, AspectRatio -> Automatic] 4
William Dickinson, of the mathematics department at Grand Valley State University, runs an annual parametric curve contest. You can view some of the clever artwork produced by freshman calculus students at the webpage http://faculty.gvsu.edu/dickinsw/parametricentries.html Parametrized curves do not have to lie in the plane. The simplest parametrized curves in space are lines, which take the form x(t) = a 1 t + b 1, y(t) = a 2 t + b 2, z(t) = a 3 t + b 3, < t <, where the a j, b j are constants. The first command below produces such a line in 3-space. It is followed by a more interesting-looking curve, called a helix, where the x and y coordinates are specified in the same way as they were for circles above, but the z coordinate changes: x(t) = cos(t), y(t) = sin t, z = t/6, < t <. The commands are ParametricPlot3D[{2*t-1, 1-t, 5-3*t}, {t,-5,5}] ParametricPlot3D[{Cos[t], Sin[t], t/6}, {t,-10,10}] 2.2 Parametrized surfaces Parametrization is something humans seem to do very naturally. As you enter a new state, an interstate highway will begin having mile-markers at 1 and count up each mile; the parameter here is arc length, and by knowing the value of your parameter, you can pinpoint where a vehicle is located on that interstate. (You may have noticed that our parametrizations of the unit circle had arc length as parameters as well.) The postal addresses of buildings along roads running north-south in Grand Rapids serve similarly as parameter-values indicating location relative to Fulton St. (the nearer to Fulton, the smaller the numbers). We do it naturally for surfaces as well as curves. In this case, two parameters are necessary instead of one. Consider the global-positioning system 5
afforded by latitude and longitude. Lines of longitude run from 0 to 360 (i.e., 0 to 2π radians), while lines of latitude run from π/2 to π/2, with latitude 0 being the equator. Mathematically, a more convenient parametrization would be to have latitudes running from 0 (at the north pole) to π (at the south). On a perfectly spherical globe of radius R, this translates into x = R sin u cos v y = R sin u sin v z = R cos u, { 0 u 2π, 0 v π. (Here the parameters are u and v.) Type the command ParametricPlot3D[{Sin[u]*Cos[v], Sin[u]*Sin[v], Cos[u]}, {u,0,2*pi}, {v,0,pi}] to see the sphere of radius 1. Below are two more commands that plot parametrized surfaces. Enter them and see what they are. ParametricPlot3D[ {(2 + Cos[v])*Cos[u], (2 + Cos[v])*Sin[u], Sin[v]}, {u, 0, 2*Pi}, {v, 0, 2*Pi}] ParametricPlot3D[{u*Cos[v], u*sin[v], 2*u}, {u, 0, 3}, {v, 0, 2*Pi}] Then try out the applet found at http://www.math.umn.edu/ rogness/multivar/parametrizedtorus.html to get a more precise idea of the relationship between the parameters u, v and the surface obtained by the first of these commands. 6