Page 151 - Computational Statistics Handbook with MATLAB
P. 151

138                        Computational Statistics Handbook with MATLAB



                             Sur
                                aacePlotPlot
                                 ce
                             Su  rf faacePlotcePlots  s ss
                             SSuurr ff
                             If we have data that represents a function defined over a bivariate domain,
                                        (
                                          ,
                             such as z =  f x y)  , then we can view our values for z as a surface. MATLAB
                             provides two functions that display a matrix of z values as a surface: mesh
                             and surf.
                              The mesh function displays the values as points above a rectangular grid
                             in the x-y plane and connects adjacent points with straight lines. The mesh
                             lines can be colored using various options, but the default method maps the
                             height of the surface to a color.
                              The surf function is similar to mesh, except that the open spaces between
                             the lines are filled in with color, with lines shown in black. Other options
                             available with the shading command remove the lines or interpolate the
                             color across the patches. An example of where the ability to display a surface
                             can be used is in visualizing a probability density function (see Chapter 8).

                             Example 5.12
                             In this example, we begin by generating a grid over which we evaluate a
                             bivariate normal density function. We then calculate the z values that corre-
                             spond to the function evaluated at each x and y. We can display this as a sur-
                             face using surf, which is shown in Figure 5.18.
                                % Create a bivariate standard normal.
                                % First create a grid for the domain.
                                [x,y] = meshgrid(-3:.1:3,-3:.1:3);
                                % Evaluate using the bivariate standard normal.
                                z = (1/(2*pi))*exp(-0.5*(x.^2+y.^2));
                                % Do the plot as a surface.
                                surf(x,y,z)


                              Special effects can be achieved by changing color maps and using lighting.
                             For example, lighting and color can help highlight structure or features on
                             functions that have many bumps or a jagged surface. We will see some exam-
                             ples of how to use these techniques in the next section and in the exercises at
                             the end of the chapter.




                             Contour ContourContour Contour  Plot Plots PlotPlot  s ss
                             We can also use contour plots to view our surface. Contour plots show lines
                             of constant surface values, similar to topographical maps. Two functions are
                             available in MATLAB for creating 2-D and 3-D contour plots. These are called
                             contour and contour3.
                              The pcolor function shows the same information that is in a contour plot
                             by mapping the surface height to a set of colors. It is sometimes useful to com-
                             bine the two on the same plot. MATLAB provides the contourf function


                            © 2002 by Chapman & Hall/CRC
   146   147   148   149   150   151   152   153   154   155   156