Page 40 - Numerical Analysis Using MATLAB and Excel
P. 40

Script and Function Files

               Editor/Debugger.

               A function file is a user−defined function using MATLAB. We use function files for repetitive
               tasks. The first line of a function file must contain the word function, followed by the output argu-
               ment, the equal sign ( = ), and the input argument enclosed in parentheses. The function name
               and file name must be the same, but the file name must have the extension .m. For example, the
               function file consisting of the two lines below


               function y = myfunction(x)
               y=x .^ 3 + cos(3 .* x)
               is a function file and must be saved. To save it, from the File menu of the command window, we
               choose New and click on M−File. This takes us to the Editor Window where we type these two
               lines and we save it as myfunction.m.
               We will use the following MATLAB functions with the next example.

               The function fzero(f,x) tries to find a zero of a function of one variable, where f is a string con-
               taining the name of a real−valued function of a single real variable. MATLAB searches for a value
               near a point where the function f changes sign, and returns that value, or returns NaN if the
               search fails.

               Important: We must remember that we use roots(p) to find the roots of polynomials only, such as
               those in Examples 1.1 and 1.2.
               fplot(fcn,lims) − plots the function specified by the string fcn between the x−axis limits specified
               by lims = [xmin xmax]. Using lims = [xmin xmax ymin ymax] also controls the y−axis limits.
               The string fcn must be the name of an m−file function or a string with variable  .
                                                                                           x
               NaN (Not−a−Number) is not a function; it is MATLAB’s response to an undefined expression
                             ∞
                         ⁄
               such as 00   ,  ⁄  ∞  , or inability to produce a result as described on the next paragraph. We can
               avoid division by zero using the eps number, which we mentioned earlier.


               Example 1.16
               Find the zeros, maxima and minima of the function

                                                  1
                                                                      1
                                  fx() =   ------------------------------------------ +  ------------------------------------------ –  10  (1.20)
                                           (  x0.1 )  2  +  0.01  (  x1.2 )  2  +  0.04
                                                                 –
                                             –
               in the interval  1.5 ≤≤–  x  1.5
               Solution:

               We first plot this function to observe the approximate zeros, maxima, and minima using the fol-
               lowing script:




               Numerical Analysis Using MATLAB® and Excel®, Third Edition                              1−27
               Copyright © Orchard Publications
   35   36   37   38   39   40   41   42   43   44   45