Page 35 - Applied Numerical Methods Using MATLAB
P. 35

24    MATLAB USAGE AND COMPUTATIONAL ERRORS
            >>u_noise1 = 2*u_noise-1 %a 1000x1 noise vector with U(-1,1)
            >>subplot(222), hist(u_noise1,20) %histogram

           2. Random Number with Normal (Gaussian) Distribution
           The numbers in a matrix generated by the MATLAB function randn(M,N) have
                                                                       2
           normal (Gaussian) distribution with average m = 0 and variance σ = 1, as
           described by N(0,1). The random number x generated by rand() has the prob-
           ability density function
                                              1   −x /2
                                                   2
                                    f X (x) = √  e                       (1.1.5)
                                              2π
              If you want another Gaussian number y with a general normal distribution
                  2
           N(m, σ ), transform the standard Gaussian number x as follows:
                                       y = σx + m                        (1.1.6)

           The probability density function of the new Gaussian number generated by this
           transformation is obtained by substituting x = (y − m)/σ into Eq. (1.1.5) and
           dividing the result by the scale factor σ (which can be seen in dx = dy/σ)
           so that the integral of the density function over the whole interval (−∞, +∞)
           amounts to 1.
                                           1    −(y−m) /2σ  2
                                                     2
                                 f Y (y) = √   e                         (1.1.7)
                                           2πσ
              For practice, we make a vector consisting of 1000 standard Gaussian numbers,
           transform it to make a vector of numbers having normal distribution N(1,1/4),
                                        2
           with mean m = 1 and variance σ = 1/4, and then draw the histograms for the
           two Gaussian number vectors (Fig. 1.7c,d).

            >>g_noise = randn(1000,1) %a 1000x1 noise vector with N(0,1)
            >>subplot(223), hist(g_noise,20) %histogram having 20 divisions
            >>g_noise1 = g_noise/2+1 %a 1000x1 noise vector with N(1,1/4)
            >>subplot(224), hist(g_noise1,20) %histogram

           1.1.9  Flow Control

           1. if-end and switch-case-end Statements
           An if-end block basically consists of an if statement, a sequel part, and an end
           statement categorizing the block. An if statement, having a condition usually
           based on the relational/logical operator (Table 1.4), is used to control the program
           flow—that is, to adjust the order in which statements are executed according to
           whether or not the condition is met, mostly depending on unpredictable situa-
           tions. The sequel part consisting of one or more statements may contain else or
           elseif statements, possibly in a nested structure containing another if statement
           inside it.
              The switch-case-end block might replace a multiple if-elseif-..-end
           statement in a neat manner.
   30   31   32   33   34   35   36   37   38   39   40