SDDSlib
Loading...
Searching...
No Matches
zeroNewton.c File Reference

Implements the zeroNewton function for finding zeros of a function using Newton's method with numerical derivatives. More...

#include "mdb.h"

Go to the source code of this file.

Functions

double zeroNewton (double(*fn)(), double value, double x_i, double dx, long n_passes, double _zero)
 Finds the zero of a function using Newton's method with numerical derivative computation.
 

Detailed Description

Implements the zeroNewton function for finding zeros of a function using Newton's method with numerical derivatives.

License
This file is distributed under the terms of the Software License Agreement found in the file LICENSE included with this distribution.
Author
M. Borland, C. Saunders, R. Soliday

Definition in file zeroNewton.c.

Function Documentation

◆ zeroNewton()

double zeroNewton ( double(* fn )(),
double value,
double x_i,
double dx,
long n_passes,
double _zero )

Finds the zero of a function using Newton's method with numerical derivative computation.

This function attempts to find a value x such that fn(x) is approximately equal to the given value. It employs Newton's iterative method, where the derivative is estimated numerically using finite differences.

Parameters
fnPointer to the function for which the zero is to be found.
valueThe target value to solve for, i.e., find x such that fn(x) = value.
x_iInitial guess for the independent variable.
dxIncrement used for numerical derivative computation.
n_passesNumber of iterations to perform.
_zeroAcceptable tolerance for the zero, determining when to stop the iterations.
Returns
The x value where fn(x) is approximately equal to value, or DBL_MAX if no zero is found within the iteration limit.

Definition at line 33 of file zeroNewton.c.

40 {
41 double f1, f2;
42 double x1, x2;
43 double dfdx;
44 long i;
45
46 x1 = x2 = x_i;
47 for (i = 0; i < n_passes; i++) {
48 if (fabs(f1 = (*fn)(x1)-value) < _zero)
49 return (x1);
50 if (i == n_passes - 1)
51 return ((x1 + x2) / 2);
52 f2 = (*fn)(x2 = x1 + dx) - value;
53 dfdx = ((f2 - f1) / dx);
54 x1 += -f1 / dfdx;
55 }
56 return (DBL_MAX); /* shouldn't happen */
57}