Implementation and Analysis of the Solution of Equations

Recall that solving an equation of the form f (x) = means finding its roots. The root operation locates the real root to which Newton’s method converges given an initial iteration point.

The root operation revolves around a single loop (see Example 13-3), which calculates successive approximations using the Newton iteration formula. In the implementation presented here, f is the function for which we are approximating the root, and g is the derivative of f. After each iteration, we determine whether the current approximation of the root is satisfactory. An approximation is deemed satisfactory when the difference between it and that of the previous iteration is less than delta. If after n iterations a satisfactory root still has not been found, root terminates.

The runtime complexity of root is O (n), where n is the maximum number of iterations the caller wishes to perform. The worst case occurs when we do not find the root we are looking for.

Example 13.3. Implementation for the Solution of Equations
/*****************************************************************************
*                                                                            *
*  -------------------------------- root.c --------------------------------  *
*                                                                            *
*****************************************************************************/

#include <math.h>

#include "nummeths.h"

/*****************************************************************************
*                                                                            *
*  --------------------------------- root ---------------------------------  *
*                                                                            *
*****************************************************************************/

int root(double (*f)(double x), double (*g)(double x), double *x, int *n, 
   double delta) {

int                satisfied,
                   i;

/*****************************************************************************
*                                                                            *
*  Use Newton's method to find a root of f.                                  *
*                                                                            *
*****************************************************************************/

i = 0;
satisfied = 0;

while (!satisfied && i + 1 < *n) {

   /**************************************************************************
   *                                                                         *
   *  Determine the next iteration of x.                                     *
   *                                                                         *
   **************************************************************************/

   x[i + 1] = x[i] - (f(x[i]) / g(x[i]));

   /**************************************************************************
   *                                                                         *
   *  Determine whether the desired approximation has been obtained.         *
   *                                                                         *
   **************************************************************************/

   if (fabs(x[i + 1] - x[i]) < delta)
      satisfied = 1;

   /**************************************************************************
   *                                                                         *
   *  Prepare for the next iteration.                                        *
   *                                                                         *
   **************************************************************************/

   i++;

}

/*****************************************************************************
*                                                                            *
*  Even without iterating, indicate that one value has been stored in x.     *
*                                                                            *
*****************************************************************************/

if (i == 0)
   *n = 1;
else
   *n = i + 1;

/*****************************************************************************
*                                                                            *
*  Return whether a root was found or the maximum iterations were reached.   *
*                                                                            *
*****************************************************************************/

if (satisfied)
   return 0;
else
   return -1;

}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.16.48.181