Plotting and Animating Surfaces with Mathematica

advertisement
Plotting and Animating Surfaces with Mathematica
D. P. Morstad, University of North Dakota
Objectives of Assignment
1.
2.
3.
4.
I.
To learn how to graph functions of two variables using Mathematica.
To learn how to display more than one surface on a single axes system.
To learn how to generate and animate a sequence of surfaces by varying a
parameter.
To demonstrate how surfaces are determined by the addition of functions.
Four Important Characteristics of Mathematica
è Mathematica is case sensitive. This means, for example, that you cannot type “sin” or
“SIN” instead of “Sin”.
è To enter a line or a command, you must press SE instead of just E .
è You must use the correct grouping symbols. Do not interchange the parentheses, square
brackets, and squiggly brackets.
è A space can be used instead of an asterisk to denote multiplication .
è To make Mathematica use larger or smaller letters and graphs, click on “Format” at the
top of the screen and then choose “Magnification” at the bottom of the menu.
II.
Plotting Individual Surfaces
The command that makes Mathematica plot a function of two variables requires only
three pieces of information: a function, a segment of the x–axis, and a segment of the y–axis.
For example, to graph
f ( x , y ) = 3 sin( x ) + 2 y for 0 ≤ x ≤ 2π and − 1 ≤ y ≤ 1 ,
simply type:
Plot3D[3 Sin[x]+2 y, {x, 0, 2 Pi}, {y, –1,1}] and press SE.
Did you leave spaces everywhere for the multiplication? If you did everything correctly,
18
you should have the following surface displayed on your screen.
5
2.5
0
-2.5
-5
0
1
0.5
0
2
-0.5
4
6
III.
-1
Three Options Out of Many: PlotPoints, BoxRatios, and ViewPoint
Although Plot3D only requires three pieces of information, you can include instructions
which will alter the appearance of your graph. If you want a more finely meshed graph, put the
cursor right after {y, –1, 1}, add a comma, and then type “PlotPoints–>25”. Press SE.
Now your screen should have
Plot3D[3 Sin[x] + 2 y, {x, 0, 2 Pi}, {y, –1, 1}, PlotPoints–>25]
5
2.5
0
-2.5
-5
0
1
0.5
0
2
-0.5
4
6
-1
To change the aspect ratios of the box in which the surface is graphed, add a comma
and “BoxRatios–>{ 2, 2, 2}” right after the “25”. This will generate the following:
1
0.5
0
-0.5
-1
5
2.5
0
-2.5
-5
0
2
4
6
19
Finally, if you want a different perspective of the surface, put a comma right after the
{2,2,2} and click the cursor right between the comma and the square bracket. Then go up and
click on “Input” and then on “3D ViewPoint Selector”. Now by clicking and dragging the new
cube on the screen, you can select a new point of view for your surface. After getting the
perspective you want, simply click on Paste, and then press SE. You may get
something like this:
0
2
4
6
1
0.5
0
-0.5
5-1
2.5
0
-2.5
-5
To find out about other possible options, type ??Plot3D and press SE.
IV.
Plotting Several Surfaces on the Same Graph
To plot two surfaces simultaneously, you make Mathematica graph and remember
each surface separately, but then you must tell it to show them to you at the same time.
Suppose you want to see f ( x , y ) = x 2 and f ( x , y ) = y 2 graphed together. Simply
type the following three lines and press SE at the end of each line.
plot1 = Plot3D[x^2, {x, –2, 2}, {y, –2, 2}]
plot2 = Plot3D[y^2, {x, –2, 2}, {y, –2, 2}]
Show[plot1, plot2]
Of course you can play with the options, but most of them must be the same for both plots.
Instead of telling Mathematica to remember the surfaces as plot1 and plot2 you could have
used Ralph and Charlie or just about anything else. Mathematica can show many surfaces at
once, not just two.
You can also define the functions instead of writing them out each time. For example,
you could type:
Clear[f1, f2]
f1[x_, y_] := x^2
f2[x_, y_] := y^2
20
This clears any previous definitions for the names f1 and f2, and it labels your two functions f1
and f2.
Since you’ve seen what these two functions look like graphed together, see what they
look like added together. Try:
plot3 = Plot3D[f1[x, y] + f2[x, y], {x, –2, 2}, {y, –2, 2}, BoxRatios–>{2, 2, 2}]
After this has been plotted, look at x2 –y2. You won’t need to retype a whole new line, simply
change the name to plot4 and change the addition sign to a subtraction sign. Next Show plot3
and plot4 at the same time. You should get a circular paraboloid on top of a hyperbolic
paraboloid. ViewPoint can be used with the Show command, so you can look at this double
graph from other perspectives if you like.
2
1
0
-1
-2
5
0
-2
-1
0
1
2
V.
Animating Sequences of Surfaces
From the previous example, it should be clear that the functions f ( x , y ) = x 2 + y 2 and
f ( x , y ) = x 2 − y 2 are closely related to one another. One has y2 added to x 2, the other has
y2 subtracted from x 2. Also, they are both variations of f ( x , y ) = x 2 + a∗ y 2 . The first
function has a = 1 and the second has a = –1. What would happen if a took on several values
between –1 and 1? You can see what would happen by having Mathematica animate these
surfaces.
First, to load Mathematica’s animation package, type
<<Graphics`Animation` and press SE.
21
The backwards looking little quotes are just to the left of 1 on the keyboard. Mathematica
has many other packages along with Graphics`Animation`. This is the standard notation used to
load each of them.
Type in :
MoviePlot3D[x^2 – a y^2, {x, –2, 2}, {y, –2, 2}, {a, –1, 1}, Frames–>15,
BoxRatios–>{2, 2, 2}]
This will generate 15 surfaces starting with x 2 – 1×y2 and ending with x 2 + 1×y2. Kind of
boring at this point, but just wait until you see what happens next . . .
The thin blue vertical lines on the right side of the Mathematica page identify cells of
input and output, and also groups of cells. Each surface is in its own cell, but in turn, all these
cells are contained in yet a larger cell. The thin blue line farthest to the right spans all of the
surfaces. You want to include all the surfaces. To select this large cell, simply move the pointer
to that rightmost blue line and click. There should then be a solid black line right next to the blue
line. The black line tells you which cell is selected. Now press Cy . You can control the
speed and direction of the animation with the control buttons that have appeared at the bottom
right of the screen.
What if, instead of adding x 2 to y2, you added sin(x) to y2? Try plotting
f ( x , y ) = sin x + 0.25 y 2 on − 6 ≤ x ≤ 6 and − 6 ≤ y ≤ 6 . Use BoxRatios–>{2,2,2}.
Notice how it simply adds sine wave oscillations to the parabolic sheet f(x, y) = 0.25y2.
5
2.5
0
-2.5
-5
10
7.5
5
2.5
0
-5
-2.5
0
2.5
5
Now make those sine waves wave by typing
MoviePlot3D[Sin[x + a] + .25 y^2, {x, –6, 6}, {y, –6, 6}, {a, 0, 2 Pi},
Frames–>15, BoxRatios–>{2, 2, 2}]
22
VI.
Practice Problems
When plotting the following functions, notice the similarities in shapes between these and
shapes you are familiar with in functions of one variable. For example, what does x 2 + y2
generate in two and three dimensions, and what are the characteristics of higher degree
polynomials?
You might have to experiment with the PlotPoints and the ranges of x and y to get good
graphs. Try altering and/or animating some of the more interesting surfaces.
1.
Plot f ( x , y ) = x 4 + y 4 .
2.
Plot f ( x , y ) = x 4 − y 4 .
3.
Plot f ( x , y ) = x 4 + y 4 − 6x 2 y 2 .
4.
Plot f ( x , y ) = sin( x − y ) cos( x + y ) .
5.
Plot f ( x , y ) = sin( x 2 + y 2 ) .
6.
2
2
− x +y )
Plot f ( x , y ) = e (
.
23
Download