Page 496 - Applied Numerical Methods Using MATLAB
P. 496

CALCULUS   485

            >>int(exp(-(x - a)^2),a,inf) %equivalently int(exp(-(x - a)^2),x,0,inf)
                                ∞  − (x − a) 2    ∞  − x 2  1 √
              ans = 1/2*pi^(1/2) %  e   dx =   e   dx =   π
                               a             0
                                                        2
            G.2.5  Taylor Series Expansion
            We can use the taylor() function to find the Taylor series expansion of a
            function or an expression with respect to the variable given as its second or
            third input argument or its free variable that might be determined by using the
            findsym function.
              One may put ‘help taylor’ into the MATLAB command window to see its
            usage, which is restated below. Let us try applying it.

            >>syms x t;N=3;
                                               1
                                           N
            >>Tx0 = taylor(exp(-x),N + 1) %f (x) ˜ =  n = 0  n! f  (n) (0) x  n
              Tx0 = 1-x + 1/2*x^2 - 1/6*x^3
            >>sym2poly(Tx0) %extract the coefficients of Taylor series polynomial
              ans = -0.1667  0.5000  -1.0000  1.0000
                                                        1
                                                   N
            >>xo = 1; Tx1 = taylor(exp(-x),N + 1,xo) %f (x) ˜ =  f  (n) (x 0 )(x − x 0 ) n
                                                   n = 0
                                                       n !
              Tx1 = exp(-1) - exp(-1)*(x - 1) + 1/2*exp(-1)*(x - 1)^2 - 1/6*exp(-1)*(x - 1)^3
            >>pretty(Tx1)
                                               2              3
              exp(-1) -exp(-1)(x - 1) +1/2 exp(-1)(x - 1) -1/6 exp(-1)(x - 1)
            >>f = exp(-x)*sin(t);
                                           1
                                        N
            >>Tt = taylor(f,N + 1,t) %f (x) ˜ =  n = 0  f  (n) (0)t  n
             Tt = exp(-x)*t - 1/6*exp(-x)*t^3  n!

             ž taylor(f) gives the fifth-order Maclaurin series expansion of f.
             ž taylor(f,n+1) with an integer n>0 gives the nth-order Maclaurin series
               expansion of f.
             ž taylor(f,a) with a real number (a) gives the fifth-order Taylor series
               expansion of f about a.
             ž taylor(f,n + 1,a) gives the nth-order Taylor series expansion of f about
               default variable=a.
             ž taylor(f,n + 1,a,y) gives the nth-order Taylor series expansion of f(y)
               about y=a.

             (cf) The target function f must be a legitimate expression given directly as the first
                 input argument.
             (cf) Before using the command “taylor()”, one should declare the arguments of the
                 function as symbols by putting, say, “syms x t”.
             (cf) In case the function has several arguments, it is a good practice to put the inde-
                 pendent variable as the last input argument of “taylor()”, though taylor() takes
   491   492   493   494   495   496   497   498   499   500   501