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

Implements the zeroIntHalve function for finding zeros of a function using interval halving. More...

#include <math.h>
#include <stdio.h>

Go to the source code of this file.

Macros

#define sign(x)
 

Functions

double zeroIntHalve (double(*fn)(), double value, double x_i, double x_f, double dx, double _zero)
 Finds the zero of a function within a specified interval using interval halving.
 

Detailed Description

Implements the zeroIntHalve function for finding zeros of a function using interval halving.

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

Macro Definition Documentation

◆ sign

#define sign ( x)
Value:
((x) > 0 ? 1 : ((x) == 0 ? 0 : -1))

Definition at line 19 of file zeroIH.c.

Function Documentation

◆ zeroIntHalve()

double zeroIntHalve ( double(* fn )(),
double value,
double x_i,
double x_f,
double dx,
double _zero )

Finds the zero of a function within a specified interval using interval halving.

This function attempts to find a value x such that fn(x) is approximately equal to the given value. It employs the bisection method to iteratively narrow down the interval where the zero lies within [x_i, x_f].

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 value of the independent variable (start of the interval).
x_fFinal value of the independent variable (end of the interval).
dxStep size to use when searching the interval.
_zeroAcceptable tolerance for the zero, determining when to stop the halving process.
Returns
The x value where fn(x) is approximately equal to value, or x_f + dx if no zero is found within the interval.

Definition at line 36 of file zeroIH.c.

43{
44 double xa, xb, xm, x_b;
45 double fa, fb, fm;
46 double f_abs, f_bdd;
47 long s_fa, s_fb, s_fm;
48 /* double zeroIH();*/
49
50 if (dx > (x_f - x_i))
51 dx = (x_f - x_i) / 2;
52
53 xa = x_i;
54 xb = xa + dx;
55
56 if (xb > x_f)
57 xb = x_f;
58 if (xa == xb)
59 xa = xb - dx;
60
61 fa = (*fn)(xa)-value;
62 s_fa = sign(fa);
63
64 while (xb <= x_f) {
65 fb = (*fn)(xb)-value;
66 s_fb = sign(fb);
67 if (s_fb == s_fa) {
68 fa = fb;
69 xa = xb;
70 s_fa = s_fb;
71 xb = xb + dx;
72 } else {
73 /* function has passed through zero */
74 /* so halve the interval, etc... */
75 f_bdd = 1000 * fabs(fa);
76 fm = (*fn)(xm = (xa + xb) / 2) - value;
77 s_fm = sign(fm);
78 x_b = xb;
79 do {
80 if (s_fm == 0)
81 return (xm);
82 else if (s_fm != s_fa) {
83 xb = xm;
84 fb = fm;
85 s_fb = s_fm;
86 } else {
87 xa = xm;
88 fa = fm;
89 s_fa = s_fm;
90 }
91 fm = (*fn)(xm = (xa + xb) / 2) - value;
92 s_fm = sign(fm);
93 f_abs = fabs(fm);
94 } while (f_abs > _zero && f_abs < f_bdd);
95 if (f_abs < _zero)
96 return (xm);
97 /* Function had a tan(x)-like singularity, which
98 * looked like a zero. So step beyond singularity
99 * and continue search.
100 */
101 /*return(zeroIH(fn, x_b, x_f, dx, _zero));*/
102 return (zeroIntHalve(fn, value, x_b, x_f, dx, _zero));
103 }
104 }
105 return (x_f + dx);
106}
double zeroIntHalve(double(*fn)(), double value, double x_i, double x_f, double dx, double _zero)
Finds the zero of a function within a specified interval using interval halving.
Definition zeroIH.c:36