Page 30 - Numerical Analysis Using MATLAB and Excel
P. 30

Using MATLAB to Make Plots

               The command plot3(x,y,z,'bd−') will display the plot in blue diamonds, connected with a solid
               line.

               In a three−dimensional plot, we can use the zlabel(‘string’) command in addition to the xla-
               bel(‘string’) and ylabel(‘string’).

                                                                       x
                                                                                y
               In a two−dimensional plot, we can set the limits of the  − and  − axes with the axis([xmin
               xmax  ymin  ymax]) command. Likewise, in a three−dimensional plot we can set the limits of all
               three axes with the axis([xmin  xmax  ymin  ymax  zmin  zmax]) command. It must be placed
               after the plot(x,y) or plot3(x,y,z) commands, or on the same line without first executing the plot
               command. This must be done for each plot. The three−dimensional text(x,y,z,’string’) command
               will place string beginning at the co−ordinate (xy z,,  ) on the plot.

               For three−dimensional plots, grid on and box off are the default states.
               The mesh(x,y,z) command displays a three−dimensional plot. Another command, contour(Z,n),
               draws contour lines for n levels. We can also use the mesh(x,y,z) command with two vector argu-
               ments. These must be defined as  length x() =              n  and  length y() =    m  where
                     ]
                  ,
                [  mn =   size Z() . In this case, the vertices of the mesh lines are the triples  x j() yi() Zi j,(,,{  )  . }
               We observe that x corresponds to the columns of  , and y corresponds to the rows of  . Z
                                                               Z
                                                                               ,
                                                                             (
               To produce a mesh plot of a function of two variables, say z =  f x y )  , we must first generate the
                       Y
                X  and   matrices which consist of repeated rows and columns over the range of the variables x
                                                            Y
                    y
                                                     X
               and  . We can generate the matrices   and   with the [X,Y]=meshgrid(x,y) function which
                                  X
               creates the matrix   whose rows are copies of the vector x, and the matrix   whose columns are
                                                                                        Y
               copies of the vector y.
               Example 1.12
                                                              r
                                                                           h
               The volume   of a right circular cone of radius   and height   is given by
                            V
                                                           --πr h
                                                     V =   1   2                                       (1.14)
                                                           3
                                                                            ≤≤
               Plot the volume of the cone as   and   vary on the intervals 0 r 4   and 0h6≤  ≤   meters.
                                             r
                                                    h
               Solution:
                                                                                                       (
                                                                                     h
                                                                                                         ,
                                                                     r
               The volume of the cone is a function of both the radius   and the height  , that is, V =  f r h )
               The three−dimensional plot is created with the following MATLAB script where, as in the previ-
               ous example, in the second line we have used the dot multiplication, division, and exponentia-
               tion. As mentioned in the footnote of the previous page, this topic will be explained in Section
               1.8, Page 1−19.



               Numerical Analysis Using MATLAB® and Excel®, Third Edition                              1−17
               Copyright © Orchard Publications
   25   26   27   28   29   30   31   32   33   34   35