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.
|
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.
|
|
Implements the zeroIntHalve function for finding zeros of a function using interval halving.
- Copyright
- (c) 2002 The University of Chicago, as Operator of Argonne National Laboratory.
- (c) 2002 The Regents of the University of California, as Operator of Los Alamos National Laboratory.
- 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.
◆ sign
Value:((x) > 0 ? 1 : ((x) == 0 ? 0 : -1))
Definition at line 19 of file zeroIH.c.
◆ 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
-
fn | Pointer to the function for which the zero is to be found. |
value | The target value to solve for, i.e., find x such that fn(x) = value . |
x_i | Initial value of the independent variable (start of the interval). |
x_f | Final value of the independent variable (end of the interval). |
dx | Step size to use when searching the interval. |
_zero | Acceptable 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
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
74
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
98
99
100
101
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.