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

The Bisection Method for Root Approximation


               We will illustrate the Bisection Method with examples using both MATLAB and Excel.



               Example 2.7
               Use the Bisection Method with MATLAB to approximate one of the roots of

                                                           5
                                                                3
                                             y =  f x() =  3x –  2x +  6x – 8                         (2.19)
               by
               a. by specifying 16 iterations, and using a for end loop MATLAB program

               b. by specifying 0.00001 tolerance for fx() , and using a while end loop MATLAB program
               Solution:

               This is the same polynomial as in Example 2.4.
               a. The for end loop allows a group of functions to be repeated a fixed and predetermined num-
                  ber of times. The syntax is:

                  for x = array
                  commands...
                  end
                   Before we write the program script, we must define a function assigned to the given polyno-
                  mial and save it as a function m−file. We will define this function as funcbisect01 and will save
                  it as funcbisect01.m.


                  function y= funcbisect01(x);
                  y = 3 .* x .^ 5 − 2 .* x .^ 3 + 6 .* x − 8;
                  %  We must not forget to type the semicolon at the end of the line above;
                  %  otherwise our script will fill the screen with values of y
                                                                              ,
                                                                                     ,
                                                                                        ,
                  On the script below, the statement for k = 1:16 says for k =  1 k =  2 … k =  16 , evaluate all
                  commands down to the end command. After the k =        16  iteration, the loop ends and any
                  commands after the end are computed and displayed as commanded.
                  Let us also review the meaning of the fprintf('%9.6f %13.6f \n', xm,fm) line. Here, %9.6f and
                  %13.6f are referred to as format specifiers or format scripts; the first specifies that the value of
                  xm must be expressed in decimal format also called fixed point format, with a total of 9 digits, 6
                  of which will be to the right of the decimal point. Likewise, fm must be expressed in decimal
                  format with a total of 13 digits, 6 of which will be to the right of the decimal point. Some other
                  specifiers are %e for scientific format, %s for string format, and %d for integer format. For
                  more information, we can type help fprintf. The special format \n specifies a linefeed, that is, it
                  prints everything specified up to that point and starts a new line. We will discuss other special
                  formats as they appear in subsequent examples.




               Numerical Analysis Using MATLAB® and Excel®, Third Edition                              2−21
               Copyright © Orchard Publications
   69   70   71   72   73   74   75   76   77   78   79