Page 255 - Applied Numerical Methods Using MATLAB
P. 255

244    NUMERICAL DIFFERENTIATION/ INTEGRATION
           PROBLEMS

            5.1 Numerical Differentiation of Basic Functions
                If we want to find the derivative of a polynomial/trigonometric/exponential
                function, it would be more convenient and accurate to use an analytical
                computation (by hand) than to use a numerical computation (by computer).
                But, in order to test the accuracy of the numerical derivative formulas,
                consider the three basic functions as

                              3
                      f 1 (x) = x − 2x,  f 2 (x) = sin x,  f 3 (x) = e x  (P5.1.1)
                (a) To find the first derivatives of these functions by using the formulas
                    (5.1.8) and (5.1.9) listed in Table 5.3 (Section 5.3), modify the program
                    “nm5p01.m”, which uses the MATLAB routine “difapx()” (Section
                    5.3) for generating the coefficients of the numerical derivative formulas.
                    Fill in the following table with the error results obtained from running
                    the program.



                                            f 1 − f −1  −f 2 + 8f 1 − 8f −1 + f −2
                     First Derivatives  h
                                               2h               12h
                      3
                     (x − 2x) | x=1  0.1   1.0000e-02
                     = 1.00000000
                                     0.01                    9.1038e-15

                     (sin x) | x=π/3  0.1  8.3292e-04
                     = 0.50000000
                                     0.01  8.3333e-06

                      x
                     (e ) | x=0      0.1                     3.3373e-06
                     = 1.00000000
                                     0.01  1.6667e-05



                     %nm5p01
                     f = inline(’x.*(x.*x-2)’, ’x’);
                     n = [1 -1];  x0 = 1;  h = 0.1;  DT = 1;
                     c = difapx(1,n);  i = 1:length(c);
                     num = c*feval(f,x0 + (n(1)+1- i)*h)’; drv = num/h;
                     fprintf(’with h = %6.4f, %12.6f %12.4e\n’, h,drv,drv - DT);


                (b) Likewise in (a), modify the program “nm5p01.m”insuchawaythat
                    the formulas (5.3.1) and (5.3.2) in Table 5.3 are generated and used to
                    find the second numerical derivatives. Fill in the following table with
                    the error results obtained from running the program.
   250   251   252   253   254   255   256   257   258   259   260