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 --------------------------------- ...

Get Mastering Algorithms with C now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.