Page 38 -
P. 38

The data (i.e., the array) can be organized into a number of bins (n ) and
                                                                                           b
                             exhibited through the command [n,y]=hist(y,nb); the array n in the
                             output will be the number of elements in each of the bins.

                             Example 1.18
                             Find the mean and the standard deviation and draw the histogram, with 20
                             bins, for an array whose 10,000 elements are chosen from the MATLAB built-
                             in normal distribution with zero mean and standard deviation 1.

                             Solution: Edit and execute the following script M-file:

                                y=randn(1,10000);
                                meany=mean(y)
                                stdy=std(y)
                                nb=20;
                                hist(y,nb)
                             You will notice that the results obtained for the mean and the standard devi-
                             ation vary slightly from the theoretical results. This is due to the finite num-
                             ber of elements chosen for the array and the intrinsic limit in the built-in
                             algorithm used for generating random numbers.
                             NOTE The MATLAB command for generating an N-elements array of ran-
                             dom numbers generated uniformly from the interval [0, 1] is rand(1,N).






                             1.11 Printing and Saving Work in MATLAB
                             Printing a figure: Use the MATLAB print function to print a displayed figure
                             directly to your printer. Notice that the printed figure does not take up the
                             entire page. This is because the default orientation of the graph is in portrait
                             mode. To change these settings, try the following commands on an already
                             generated graphic window:

                                orient('landscape') %full horizontal layout
                                orient('tall')           %full vertical layout

                             Printing a program file (script M-file): For both the Mac and PC, open the M-file
                             that you want to print. Go to the File pull-down menu, and select Print.


                             Saving and loading variables (data): You can use the MATLAB save function to
                             either save a particular variable or the entire MATLAB workspace. To do this,
                             follow the following example:


                             © 2001 by CRC Press LLC
   33   34   35   36   37   38   39   40   41   42   43