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

The Bisection Method for Root Approximation


                  end

                  The commands between while and end are executed as long as all elements in expression are
                  true. The script should be written so that eventually a false condition is reached and the loop
                  then terminates.

                  There is no need to create another function m−file; we will use the same as in part a. Now we
                  type and execute the following while end loop program.



                   x1=1; x2=2; tol=0.00001;
                   disp('    xm            fm'); disp('-------------------------')
                   while (abs(x1-x2)>2*tol);
                      f1=funcbisect01(x1); f2=funcbisect01(x2); xm=(x1+x2)/2;
                      fm=funcbisect01(xm);
                      fprintf('%9.6f %13.6f \n', xm,fm);
                      if (f1*fm<0);
                         x2=xm;
                      else
                        x1=xm;
                      end
                   end
                  When this program is executed, MATLAB displays the following:


                    xm            fm
                  -------------------------
                   1.500000     17.031250
                   1.250000      4.749023
                   1.125000      1.308441
                   1.062500      0.038318
                   1.031250     -0.506944
                   1.046875     -0.241184
                   1.054688     -0.103195
                   1.058594     -0.032885
                   1.060547      0.002604
                   1.059570     -0.015168
                   1.060059     -0.006289
                   1.060303     -0.001844
                   1.060425      0.000380
                   1.060364     -0.000732
                   1.060394     -0.000176
                   1.060410      0.000102
                   1.060402     -0.000037
                   1.060406      0.000032
                   1.060404     -0.000003

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