Page 63 - Applied Numerical Methods Using MATLAB
P. 63

52    MATLAB USAGE AND COMPUTATIONAL ERRORS
                    Surprisingly, MATLAB gives us the function values without any com-
                    plaint and presents a nice graph of the sinc function. What is the
                    difference between (a) and (b)?
                (cf) Actually, we would have no problem if we used the MATLAB built-in function
                    sinc().
            1.7 Termwise (Element-by-Element) Operation in In-Line Functions
                (a) Let the function f 1 (x) be defined without one or both of the dot(.)
                   operators in Section 1.1.6. Could we still get the output vector consist-
                   ing of the function values for the several values in the input vector?
                   You can type the following statements into the MATLAB command
                   window and see the results.

                    >>f1 = inline(’1./(1+8*x^2)’,’x’);  f1([0 1])
                    >>f1 = inline(’1/(1+8*x.^2)’,’x’);  f1([0 1])

                (b) Let the function f 1 (x) be defined with both of the dot(.) operators as in
                   Section 1.1.6. What would we get by typing the following statements
                   into the MATLAB command window?

                     >>f1 = inline(’1./(1+8*x.^2)’,’x’);  f1([0 1]’)
            1.8 In-Line Function and M-file Function with the Integral Routine ‘quad()’
                As will be seen in Section 5.8, one of the MATLAB built-in functions for
                computing the integral is ‘quad()’, the usual usage of which is

                                                           b
                   quad(f,a,b,tol,trace,p1,p2, ..)  for    f(x, p1,p2,. ..)dx
                                                         a
                                                                        (P1.8.1)
                where
                 f is the name of the integrand function (M-file name should be categorized
                 by ’’)
                 a,b are the lower/upper bound of the integration interval
                 tol is the error tolerance (10 −6  by default [])
                 trace set to1(on)/0(off)(0 bydefault []) for subintervals
                 p1,p2,.. are additional parameters to be passed directly to function f

                Let’s use this quad() routine with an in-line function and an M-file function
                to obtain
                                        m+10

                                           (x − x 0 )f (x)dx           (P1.8.2a)
                                       m−10
                and
                                        m+10
                                                  2
                                           (x − x 0 ) f(x)dx           (P1.8.2b)
                                      m−10
   58   59   60   61   62   63   64   65   66   67   68