Online Book Reader

Home Category

Mastering Algorithms With C - Kyle Loudon [154]

By Root 1452 0
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

#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;

}

Questions and Answers


Q: In the discussion of polynomial interpolation, we stated that we need to choose enough points to give an accurate impression of the function we are interpolating. What happens if we do not use enough points?

A: Interpolating a function with not enough points, or poorly placed points, leads to an interpolating polynomial that does not accurately reflect the function we think we are interpolating. A simple example is interpolating a quadratic polynomial (a parabola when plotted) with only two points. Interpolation with two points results in a line, which is far from a parabola!

Q: Using the guidelines presented in this chapter, how many interpolation points should we use to interpolate the function f (x) = x 5 + 2.8x 3 - 3.3x 2 - x + 4.1?

A: When interpolating a function that we know is a polynomial itself, we can get a good impression of the function by using n + 1 well-placed points, where n is the degree

Return Main Page Previous Page Next Page

®Online Book Reader