Page 83 - A Guide to MATLAB for Beginners and Experienced Users
P. 83

64        Chapter 4: Beyond the Basics


           Sums and Products
                     Finite numerical sums and products can be computed easily using the vector
                     capabilities of MATLAB and the commands sum and prod. For example,
                       >> X = 1:7;
                       >> sum(X)

                       ans =
                             28
                       >> prod(X)


                       ans =
                             5040

                       You can do finite and infinite symbolic sums using the command symsum.
                     To illustrate, here is the telescoping sum
                             n
                                 1     1
                                   −        :
                                 k   1 + k
                            k=1
                       >> syms k n; symsum(1/k - 1/(k + 1), 1, n)

                       ans =
                       -1/(n+1)+1
                     And here is the well-known infinite sum
                             ∞
                                1
                                  :
                                n 2
                            n=1
                       >> symsum(1/nˆ2, 1, Inf)

                       ans =
                       1/6*pi^2
                     Another familiar example is the sum of the infinite geometric series:

                       >> syms a k; symsum(aˆk, 0, Inf)

                       ans =
                       -1/(a-1)

                     Note, however, that the answer is only valid for |a| < 1.
   78   79   80   81   82   83   84   85   86   87   88