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

Chapter 2  Root Approximations




                   % Finding roots by Newton's method
                   % The following is the first derivative of
                   % the function defined as exercise3
                   y=−2.*sin(2.*x)+2.*cos(2.*x)+1;
                  Now, we write and execute the following program and we find that the second root is

                  x =  2.2295  and this is consistent with the value shown on the plot.

                   x = input('Enter starting value: ');
                   fx = exercise3(x);
                   fprimex = exercise3der(x);
                   xnext = x−fx/fprimex;
                      x = xnext;
                      fx = exercise3(x);
                      fprimex = exercise3der(x);
                   disp(sprintf('First approximation is x =  %9.6f \n', x))
                   while input('Next approximation? (<enter>=no,1=yes)');
                      xnext=x−fx/fprimex;
                      x=xnext;
                      fx=exercise3(x);
                      fprimex=exercise3der(x);
                   disp(sprintf('Next approximation is x =  %9.6f \n', x))
                   end;
                   disp(sprintf('%9.6f \n', x))
                   Enter starting value: 3

                   First approximation is x = 2.229485






























               2−34                             Numerical Analysis Using MATLAB® and Excel®, Third Edition
                                                                             Copyright © Orchard Publications
   82   83   84   85   86   87   88   89   90   91   92