Page 272 - Applied Numerical Methods Using MATLAB
P. 272

PROBLEMS   261
                    (ii) Can we say that the numerical error becomes smaller as we increase
                       the numbers (M,N) of segments along the x-axis and y-axis for the
                       routine “int2s()”? If this is contrary to the case of (a), can you
                       blame the weird shape of the integrand function in Eq. (P5.14.2)
                       for such a mess-up?
                (cf) Note that the computation times to be listed in Tables P5.14.1 to P5.14.3
                    may vary with the speed of CPU as well as the computational jobs which
                    are concurrently processed by the CPU. Therefore, the time measured by the
                    ‘tic/toc’ commands cannot be an exact estimate of the computational load
                    taken by each routine.
            5.15 Area of a Triangle

                Consider how to find the area between the graph (curve) of a function f(x)
                and the x-axis. For example, let f(x) = x for 0 ≤ x ≤ 1in order to find
                the area of a right-angled triangle with two equal sides of unit length. We
                might use either the 1-D integration or the 2-D integration—that is, the
                double integral for this job.
                 (a) Use any integration method that you like best to evaluate the integral

                                                        1
                                                 1
                                          I 1 =  xdx =                 (P5.15.1)
                                               0        2
                (b) Use any double integration routine that you like best to evaluate the
                    integral
                                   1                1
                                       f(x)             x
                            I 2 =        1 dy dx =      1 dy dx        (P5.15.2)
                                  0  0             0  0
                    You may get puzzled with some problem when applying the routine
                    “int2s()” if you define the integrand function as
                     >>fp515b = inline(’1’,’x’,’y’);

                    It is because this function, being called inside the routine
                    “smpsns_fxy()”, yields just a scalar output even for the vector-valued
                    input argument. There are two remedies for this problem. One is to
                    define the integrand function in such a way that it can generate the
                    output of the same dimension as the input.
                     >>fp515b = inline(’1+0*(x+y)’,’x’,’y’);

                    But, this will cause a waste of computation time due to the dead multi-
                    plication for each element of the input arguments x and y. The other is
                    to modify the routine “smpsns_fxy()” in such a way that it can avoid
                    the vector operation. More specifically, you can replace some part of
                    the routine with the following. But, this remedy also increases the com-
                    putation time due to the abandonment of vector operation taking less
                    time than scalar operation (see Section 1.3).
   267   268   269   270   271   272   273   274   275   276   277