Page 57 - Applied Numerical Methods Using MATLAB
P. 57

46    MATLAB USAGE AND COMPUTATIONAL ERRORS
              Now, let us see the routine “ez_plot()”, which does the same plotting job
           as “ez_plot1()”. Note that it has a MATLAB keyword varargin (variable
           length argument list) as its last input argument and passes it into the MATLAB
           built-in function feval() as its last input argument. Since varargin can repre-
           sent comma-separated multiple parameters including expression/strings, it paves
           the highway for passing the parameters in relays. As the number of parame-
           ters increases, it becomes much more convenient to use varargin for passing
           the parameters than to deal with the parameters one-by-one as in ez_plot1().
           This technique will be widely used later in Chapter 4 (on nonlinear equations),
           Chapter 5 (on numerical integration), Chapter 6 (on ordinary differential equa-
           tions), and Chapter 7 (on optimization).
           (cf) Note that MATLAB has a built-in graphic function ezplot(), which is much more
               powerful and convenient to use than ez_plot(). You can type ‘help ezplot’tosee
               its function and usage.

           1.3.7  Adaptive Input Argument List
           A MATLAB function/routine is said to be “adaptive” to users in terms of input
           arguments if it accepts different number/type of input arguments and makes a
           reasonable interpretation. For example, let us see the nonlinear equation solver
           routine ‘newton()’ in Section 4.4. Its input argument list is

            (f,df,x0,tol,kmax)
           where f, df, x0, tol and kmax denote the filename (string) of function (to
           be solved), the filename (string) of its derivative function, the initial guess (for
           solution), the error tolerance and the maximum number of iterations, respectively.
           Suppose the user, not knowing the derivative, tries to use the routine with just
           four input arguments as follows.
            >>newton(f,x0,tol,kmax)
              At first, these four input arguments will be accepted as f,df,x0, and tol,
           respectively. But, when the second line of the program body is executed, the
           routine will notice something wrong from that df is not any filename but a
           number and then interprets the input arguments as f,x0,tol, and kmax to the
           idea of the user. This allows the user to use the routine in two ways, depending
           on whether he is going to supply the routine with the derivative function or not.
           This scheme is conceptually quite similar to function overloading of C++, but
           C++ requires us to have several functions having the same name, with different
           argument list.


           PROBLEMS

            1.1 Creating a Data File and Retrieving/Plotting Data Saved in a Data File
                (a) Using the MATLAB editor, make a program “nm1p01a”, which lets its
                    user input data pairs of heights [ft] and weights [lb] of as many persons
   52   53   54   55   56   57   58   59   60   61   62