• No results found

Graphing in Three Dimensions

In document Maple Learningguide (Page 121-129)

You can plot a function of two variables as a surface in three-dimensional space. This allows you to visualize the function. The syntax for plot3d is similar to that for plot. Again, an explicit function, z = f (x, y), is easiest to plot.

> plot3d( sin(x*y), x=-2..2, y=-2..2 );

You can rotate the plot by dragging in the plot window. The menus allow you to change various characteristics of a plot.

As with plot, plot3d can handle user-defined functions.

> f := (x,y) -> sin(x) * cos(y);

4.2 Graphing in Three Dimensions • 113 > plot3d( f(x,y), x=0..2*Pi, y=0..2*Pi );

By default, Maple displays the graph as a shaded surface, but you can change this by using either the menus or the style option. For example, style=hidden draws the graph as a hidden wireframe structure.

> plot3d( f(x,y), x=0..2*Pi, y=0..2*Pi, style=hidden );

For a list of style options, refer to ?plot3d,options.

The range of the second parameter can depend on the first parameter.

114 • Chapter 4: Graphics Parametric Plots

You cannot specify some surfaces explicitly as z = f (x, y). The sphere is an example of such a plot. As for two-dimensional graphs (see Section 4.1), one solution is a parametric plot. Make the three coordinates, x, y, and z, functions of two parameters, for example, s and t. You can specify parametric plots in three dimensions by using the following syntax.

plot3d( [ x-expr, y-expr, z-expr ], parameter1 =range, parameter2 =range ) Here are two examples.

> plot3d( [ sin(s), cos(s)*sin(t), sin(t) ], > s=-Pi..Pi, t=-Pi..Pi );

> plot3d( [ s*sin(s)*cos(t), s*cos(s)*cos(t), s*sin(t) ], > s=0..2*Pi, t=0..Pi );

Spherical Coordinates

The Cartesian (ordinary) coordinate system is only one of many coor- dinate systems in three dimensions. In the spherical coordinate system, the three coordinates are the distance r to the origin, the angle θ in the xy-plane measured in the counterclockwise direction from the x-axis, and the angle φ measured from the z-axis.

4.2 Graphing in Three Dimensions • 115

Figure 4.2 The Spherical Coordinate System

θ r φ 0 z y x

Maple can plot a function in spherical coordinates by using the sphereplot command in the plots package. To access the command with its short name, use with(plots). To avoid listing all the commands in the plots package, use a colon, rather than a semicolon.

> with(plots):

Use the sphereplot command in the following manner. sphereplot( r-expr, theta =range, phi =range )

The graph of r = (4/3)θsin φ looks like this:

> sphereplot( (4/3)^theta * sin(phi), > theta=-1..2*Pi, phi=0..Pi );

116 • Chapter 4: Graphics

Plotting a sphere in spherical coordinates is easy: specify the radius, perhaps 1, let θ run all the way around the equator, and let φ run from the North Pole (φ = 0) to the South Pole (φ = π).

> sphereplot( 1, theta=0..2*Pi, phi=0..Pi, > scaling=constrained );

(For more information on constrained versus unconstrained plotting, see section 4.1.)

The sphereplot command also accepts parametrized plots, that is, functions that define the radius and both angle-coordinates in terms of two parameters, for example, s and t. The syntax is similar to a parametrized plot in Cartesian (ordinary) coordinates. See this section, page 114.

sphereplot( [ r-expr, theta-expr, phi-expr ], parameter1 =range, parameter2 =range ) Here r = exp(s) + t, θ = cos(s + t), and φ = t2.

> sphereplot( [ exp(s)+t, cos(s+t), t^2 ],

4.2 Graphing in Three Dimensions • 117 Cylindrical Coordinates

Specify a point in the cylindrical coordinate system using the three coordinates r, θ, and z. Here r and θ are polar coordinates (see section 4.1) in the xy-plane and z is the usual Cartesian z-coordinate.

Figure 4.3 The Cylindrical Coordinate System

r θ 0 z y x

Maple plots functions in cylindrical coordinates with the cylinderplot command from the plots package.

> with(plots):

You can plot graphs in cylindrical coordinates by using the following syntax.

cylinderplot( r-expr, angle =range, z =range )

Here is a three-dimensional version of the spiral previously shown in section 4.1.

118 • Chapter 4: Graphics

Cones are easy to plot in cylindrical coordinates: let r equal z and let θ vary from 0 to 2π.

> cylinderplot( z, theta=0..2*Pi, z=0..1 );

The cylinderplot command also accepts parametrized plots. The syntax is similar to that of parametrized plots in Cartesian (ordinary) coordinates. See this section, page 114.

cylinderplot( [ r-expr, theta-expr, z-expr ], parameter1 =range, parameter2 =range ) The following is a plot of r = st, θ = s, and z = cos(t2).

> cylinderplot( [s*t, s, cos(t^2)], s=0..Pi, t=-2..2 );

Refining Plots

If your plot is not as smooth or precise as you want, calculate more points. The option for doing this is

grid=[m, n ]

where m is the number of points to use for the first coordinate, and n is the number of points to use for the second coordinate.

4.2 Graphing in Three Dimensions • 119 > plot3d( sin(x)*cos(y), x=0..3*Pi, y=0..3*Pi, grid=[50,50] );

In the next example, a large number of points (100) for the first co- ordinate (theta) makes the spiral look smooth. However, the function does not change in the z-direction. Thus, a small number of points (5) is sufficient.

> cylinderplot( theta, theta=0..4*Pi, z=-1..1, grid=[100,5] );

The default grid is approximately 25 by 25 points.

Shading and Lighting Schemes

Two methods for shading a surface in a three-dimensional plot are avail- able. In the first method, one or more distinctly colored light sources illuminate the surface. In the second method, the color of each point is a direct function of its coordinates.

Maple has a number of preselected light source configurations which give aesthetically pleasing results. You can choose from these light sources through the menus or with the lightmodel option. For coloring the sur- face directly, a number of predefined coloring functions are also available through the menus or with the shading option.

120 • Chapter 4: Graphics

the resulting coloring. Use either light sources or direct coloring. Here is a surface colored with zgrayscale shading and no lighting.

> plot3d( x*y^2/(x^2+y^4), x=-5..5,y=-5..5, > shading=zgrayscale, lightmodel=none );

The same surface illuminated by lighting scheme light1 and no shading follows.

> plot3d( x*y^2/(x^2+y^4), x=-5..5,y=-5..5, > shading=none, lightmodel=light1 );

The plots appear in black and white in this book. Try them in Maple to see the effects in color.

In document Maple Learningguide (Page 121-129)

Related documents