• Aucun résultat trouvé

Other Graphics Commands

Dans le document Second Edition (Page 163-171)

Three-Dimensional Graphics

5.2 Other Graphics Commands

A level curve of a function of two variables, f(x, y), is a two-dimensional graph of the equation f(x, y) = k for some fixed value of k. A contour plot is a collection of level curves drawn on the same set of axes.

The Mathematica command ContourPlot draws contour plots of functions of two variables. The contours join points on the surface having the same height. The default is to have contours corresponding to a sequence of equally spaced values of the function.

ContourPlot[f[x, y], {x, xmin, xmax}, {y, ymin, ymax}] draws a contour plot of f(x, y) in a rectangle determined by xmin, xmax, ymin, and ymax.

Contour plots produced by Mathematica are drawn shaded, in such a way that regions with higher values of f(x, y) are drawn lighter. As with all Mathematica graphics commands, options allow you to control the appearance of the graph.

Contours ã n allows you to determine the number of contours to be drawn. The default is ten equally spaced curves.

Contoursã{k1, k2,...} draws contours corresponding to function values k1, k2, . . .

ContourShadingãFalse turns off shading. This option is particularly useful if your monitor or printer does not handle grayscales well.

ContourLinesãFalse eliminates the lines that separate the shaded contours.

PlotPointsãn controls how many points will be used in each direction in an adaptive algorithm to plot each curve. The default is 15. (The default for two-dimensional graphics is 25.)

A complete list of options and their default values can be obtained using the command Options[ContourPlot].

A density plot shows the values of a function at a regular array of points. Lighter regions have higher values.

DensityPlot[f[x, y], {x, xmin, xmax}, {y, ymin, ymax}] draws a density plot of f(x, y) in a rectangle determined by xmin, xmax, ymin, and ymax.

The option Mesh draws a rectangular mesh that subdivides the region.

Mesh ã None (default) draws no mesh.

Mesh ã n draws n equally spaced mesh divisions.

Mesh ã Automatic draws automatically chosen mesh divisions.

Mesh ã All draws mesh divisions between all elements.

Mesh ã Full draws mesh divisions through regular data points.

EXAMPLE 12

DensityPlot[x2 + y2, {x, –10, 10}, {y, –10, 10}]

DensityPlot[x2 + y2, {x, –10, 10}, {y, –10, 10}, Mesh ã Automatic]

–10 –5 0 5 10

–10 –5 0 5 10

–10 –5 0 5 10

–10 –5 0 5 10

The commands ListContourPlot and ListDensityPlot are the analogs of ContourPlot and DensityPlot for lists of numbers. These commands are appropriate for use with functions defined on a lattice of integer coordinates.

ListContourPlot[array] generates a contour plot from a two-dimensional array of numbers.

ListDensityPlot[array] generates a density plot from a two-dimensional array of numbers.

array = {{z11, z12,...}, {z21, z22,...}, ...}, representing the heights of points in the x-y plane, must be a nested array of dimension 2 × 2 or larger. zij is the z-coordinate of the point ( j,i ). The options for ListContourPlot and ListDensityPlot are the same as for ContourPlot and DensityPlot, except that the axes are labeled, by default, with positive integers starting with 1. The option DataRange allows you to change the labeling of the axes to correspond to the actual values of the data.

DataRange ã {{xmin, xmax}, {ymin, ymax}} labels the x and y axes from xmin to xmax and from ymin to ymax, respectively.

EXAMPLE 13

list = Table[Random[], {x, 1, 10}, {y, 1, 10}];

ListContourPlot[list, DataRange ã {{ –5, 5}, {3, 7}}] ListDensityPlot[list, DataRange ã {{ –5, 5}, {3, 7}}]

–4 –2 0 2 4

3 4 5 6 7

–4 –2 0 2 4

3 4 5 6 7

ContourPlot3D is the three-dimensional counterpart of ContourPlot. ContourPlot3D will sketch the level surfaces of f, i.e., the set of points (x, y, z) such that f(x, y, z) = k.

ContourPlot3D[f[x, y, z], {x, xmin, xmax}, {y, ymin, ymax},{z, zmin, zmax}] draws a three-dimensional contour plot of the level surface f(x, y, z) = 0 in a box determined by xmin, xmax, ymin, ymax, zmin, and zmax.

The most commonly used options for ContourPlot3D are

Contours ã {k1, k2,...} draws level surfaces corresponding to k1, k2, . . .

PlotPoints ã {nx, ny} determines the initial number of evaluation points that will be used in the x and y directions, respectively. PlotPoints ã n is equivalent to PlotPoints ã {n, n}.

EXAMPLE 14

ContourPlot3D[z – x2– y2,{x, –5, 5}, {y, –5, 5}, {z, 0, 10},Contours ã {0, 5}, Mesh ã None]

–5

0

5 –5

0 5

0 5 10

Generates a 10 × 10 array of random numbers.

ListPlot3D is the three-dimensional analog of ListPlot.

ListPlot3D[{{z11, z12,...}, {z21, z22,...},...}] generates a three-dimensional surface based upon a given array of heights, zij (must be a nested array of dimension at least 2 × 2).

The x- and y-coordinate values for each data point are taken to be consecutive integers beginning with 1.

ListPlot3D[{{x1, y1, z1}, {x2, y2, z2},...}] generates a three-dimensional surface based upon a given array of heights, zj which are the z-coordinates corresponding to the points {xi, yi}.

Some options for ListPlot3D include:

MeshShading ã shades generates a surface shaded according to the descriptions in the array shades (GrayLevel, Hue, RGBColor, etc.). If array has dimensions m ×n, then shades must have dimensions (m–1)×(n–1).

DataRange ã {{xmin, xmax}, {ymin, ymax}} labels the x- and y-axes from xmin to xmax and from ymin to ymax, respectively. The default is DataRangeãAutomatic, which assigns values starting with 1.

Mesh is an option that specifies how mesh divisions should be drawn. The default is MeshãAutomatic.

Mesh ã n specifies that n equally spaced mesh divisions (lines) should be drawn in each direction.

Mesh ã All specifies that mesh divisions should be drawn between all elements.

Mesh ã None eliminates all mesh divisions from being drawn.

EXAMPLE 15 (Graphs are grouped together for easy comparison.) list = {{1, 5, 2, 2}, {3, 6, 1, 4}, {3, 1, 7, 2}};

ListPlot3D[list]

ListPlot3D[list, Mesh ã None]

shades={{Red,Orange,Green}, {Cyan,Yellow,Magenta}};

ListPlot3D[list,MeshShadingãshades]

The discrete analog of ContourPlot3D is ListContourPlot3D.

ListContourPlot3D[array] draws a contour plot of the values in array, a three-dimensional array of numbers representing the values of a function.

Contours ã n is an option that draws contours at n equally spaced levels. The default is Contours→3. Contours ã {k1,k2,. . .} draws contours corresponding to function values k1, k2, . . .

DataRange ã {{xmin, xmax}, {ymin, ymax}, {zmin, zmax}} labels the x, y, and z axes from xmin to xmax, ymin to ymax, and zmin to zmax, respectively. The default is DataRangeãAutomatic, which assigns values starting with 1.

EXAMPLE 16 This example generates a discrete set of values of the function f(x, y, z) = x2 + y2+ z2 and draws two contour plots of f(x, y, z) = k for k = .5 and k = 1.5. The surfaces generated are spheres, but the larger sphere is drawn in a box that is too small to contain it completely. The result is that the inner sphere is partially visible in the picture.

list = Table[x2 + y2 + z2, {x, – 1, 1, .25}, {y, – 1, 1, .25}, {z, – 1, 1, .25}];

ListContourPlot3D[list, DataRange ã {{– 1, 1}, {– 1, 1}, {– 1, 1}}, Contours ã {.5, 1.5}]

–1.0 –0.5

0.0 0.5

1.0 –1.0

–0.5 0.0

0.5 1.0

–0.5 0.0 0.5 1.0

–1.0

SOLVED PROBLEMS

5.14 Obtain a contour plot of f(x, y) = sin x + sin y on the square – 4π≤ x, y ≤ 4π and compare it to the three-dimensional graph of the function.

SOLUTION (Graphs are placed side by side for easy comparison.) Plot3D[Sin[x] + Sin[y], {x, – 4o, 4o}, {y, – 4o, 4o}]

ContourPlot[Sin[x] + Sin[y], {x, – 4o, 4o}, {y, – 4o, 4o}]

5.15 Compare a contour plot and a density plot for the function f(x, y) = sin xy over the rectangle –π≤ x, y ≤π.

SOLUTION (Graphs are placed side by side for easy comparison.) ContourPlot[Sin[x y], {x, – o, o}, {y, – o, o}] DensityPlot[Sin[x y], {x, – o, o}, {y, – o, o}]

–10

0

10

–10 0

10 –2

–1 0 1 2

10

5

0

–5

–10

–10 –5 0 5 10

–3 –2 –1 0 1 2 3

–3 –2 –1 0 1 2 3

–3 –2 –1 0 1 2 3

–3 –2 –1 0 1 2 3

5.16 Obtain a contour plot and a density plot of the discrete function Quotient[x, y] as x and y range from 1 to 10.

SOLUTION (Graphs are placed side by side for easy comparison.) list = Table[Quotient[x, y], {x, 1, 10}, {y, 1, 10}];

ListContourPlot[list]

ListDensityPlot[list]

5.17 Let f(x, y, z) = 5x2+ 2y2+ z2. Draw the level surfaces f(x, y, z) = k for k = 1, 4, 9, 16, and 25. Sketch the surfaces only for y ≥ 0 so that all the surfaces will be visible.

SOLUTION

ContourPlot3D[5 x2 + 2 y2 + z2, {x, – 5, 5}, {y, 0, 5}, {z, – 5, 5}, Contours ã {1, 4, 9, 16, 25}]

2 4 6 8 10

2 4 6 8 10

2 4 6 8 10

2 4 6 8 10

4 2 0

5

0

–5 –5

0

5

5.18 Generate a 5 × 5 array of random integers between 1 and 10 and construct a three-dimensional list plot of these values.

SOLUTION

list = Table[Random[Integer, {0, 10}], {x, 1, 5}, {y, 1, 5}];

ListPlot3D[list]

5.19 Draw hyperbolic cylinders x2 – y2= k, k = 0, 2, and 5, by computing f(x, y, z) = x2 – y2 at integer values between –5 and 5 for each variable and using ListContourPlot3D.

SOLUTION

We use integer values of x, y, and z to construct our list.

list = Table[x2 – y2, {z, –5, 5}, {y, –5, 5}, {x, –5, 5}];

ListContourPlot3D[list, Contours ã {0, 2, 5},

DataRange ã {{–5, 5}, {–5, 5}, {–5, 5}}] 1

2

3

4

5 1 2

3 4

5

0 5 10

–5

0

5 –5

0

5

–5 0 5

Dans le document Second Edition (Page 163-171)