Page 34 - MATLAB Recipes for Earth Sciences
P. 34

2.7 Basic Visualization Tools                                    25

           indicates that all variables used in the function do not appear in the work-

           space. Only the input and output as defined by the user are stored in the

           workspace. The M-files can therefore be applied to data like real functions,
           whereas scripts contain sequences of commands are applied to the variables
           in workspace.




           2.7 Basic Visualization Tools


           MATLAB provides numerous routines for  displaying your data as graphs.
           This chapter introduces the most important  graphics functions. The graphs

           will be modified, printed and exported to be edited with graphics software
           other than MATLAB. The simplest function producing a graph of a variable

           y versus another variable x is plot. First we define two vectors x and y,
           where y is the sine of x. The vector x contains values between 0 and 2› with
           ›/10 increments, whereas y is defi ned as element-by-element sine of x.
             x = 0 : pi/10 : 2*pi;
             y = sin(x);
           These two commands result in two vectors with 21 elements each, i.e., two
           1-by-21 arrays. Since the two vectors x and y have the same length, we can
           use plot to produce a linear 2D graph y against x.
             plot(x,y)

           This command opens a Figure Window named Figure 1 with a gray back-
           ground, an x-axis ranging from 0 to 7, a y-axis ranging from -1 to +1 and a
           blue line. You may wish to plot two different curves in one single plot, for
           example, the sine and the cosine of x in different colors. The command
             x = 0 : pi/10 : 2*pi;
             y1 = sin(x);
             y2 = cos(x);
             plot(x,y1,'r--',x,y2,'b-')

           creates a dashed red line displaying the sine of x and a solid blue line
           representing the cosine of this vector (Fig. 2.4). If you create another plot,
           the window Figure 1 is cleared and a new graph is displayed. The com-
           mand figure, however, can be used to create a new fi gure object in a new
           window.
   29   30   31   32   33   34   35   36   37   38   39