Page 59 - A Guide to MATLAB for Beginners and Experienced Users
P. 59

40        Chapter 3: Interacting with MATLAB


                     statement, which identifies the file as a function M-file. The first line specifies
                     the name of the function and describes both its input arguments (or parame-
                     ters) and its output values. In this example, the function is called sinelimit.
                     The file name and the function name should match.
                       The function sinelimit takes one input argument and returns one out-
                     put value, called c and y (respectively) inside the M-file. When the function
                     finishes executing, its output will be assigned to ans (by default) or to any other
                     variable you choose, just as with a built-in function. The remaining lines of
                     the M-file define the function. In this example, b is a row vector consisting
                     of the integers from 1 to c. The vector y contains the results of computing
                                          −b
                     sin(x)/x where x = 10 ; the prime makes y a column vector. Notice that the
                     output of the lines defining b, x, and y is suppressed witha semicolon. In
                     general, the output of intermediate calculations in a function M-file should be
                     suppressed.
                      Of course, when we run the M-file above, we do want to see the results of
                       the last line of the file, so a natural impulse would be to avoid putting a
                       semicolon on this last line. But because this is a function M-file, running it
                       will automatically display the contents of the designated output variable y.
                       Thus if we did not put a semicolon at the end of the last line, we would see
                       the same numbers twice when we run the function!

                      Note that the variables used in a function M-file, such as b, x, and y in
                       sinelimit.m, are local variables. This means that, unlike the variables that
                       are defined in a script M-file, these variables are completely unrelated to any
                       variables with the same names that you may have used in the Command
                       Window, and MATLABdoes not remember their values after the function
                       M-file is executed. For further information, see the section Variables in
                       Function M-files in Chapter 4.
                     Here is an example that shows how to use the function sinelimit:

                       >> sinelimit(5)
                       ans =

                           0.99833416646828
                           0.99998333341667
                           0.99999983333334
                           0.99999999833333
                           0.99999999998333

                     None of the values of b from 1 to 5 yields the desired answer, 1, to 15 digits.
   54   55   56   57   58   59   60   61   62   63   64