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.
#include <math.h>
#include <stdio.h>
#include "mdb.h"
Go to the source code of this file.
|
| double | zeroIntHalve (double(*fn)(double x), 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.
|
| |
◆ zeroIntHalve()
| double zeroIntHalve |
( |
double(* | fn )(double x), |
|
|
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 37 of file zeroIH.c.
44{
45 double xa, xb, xm, x_b;
46 double fa, fb, fm;
47 double f_abs, f_bdd;
48 long s_fa, s_fb, s_fm;
49
50
51 if (dx > (x_f - x_i))
52 dx = (x_f - x_i) / 2;
53
54 xa = x_i;
55 xb = xa + dx;
56
57 if (xb > x_f)
58 xb = x_f;
59 if (xa == xb)
60 xa = xb - dx;
61
62 fa = (*fn)(xa)-value;
63 s_fa = sign(fa);
64
65 while (xb <= x_f) {
66 fb = (*fn)(xb)-value;
67 s_fb = sign(fb);
68 if (s_fb == s_fa) {
69 fa = fb;
70 xa = xb;
71 s_fa = s_fb;
72 xb = xb + dx;
73 } else {
74
75
76 f_bdd = 1000 * fabs(fa);
77 fm = (*fn)(xm = (xa + xb) / 2) - value;
78 s_fm = sign(fm);
79 x_b = xb;
80 do {
81 if (s_fm == 0)
82 return (xm);
83 else if (s_fm != s_fa) {
84 xb = xm;
85 fb = fm;
86 s_fb = s_fm;
87 } else {
88 xa = xm;
89 fa = fm;
90 s_fa = s_fm;
91 }
92 fm = (*fn)(xm = (xa + xb) / 2) - value;
93 s_fm = sign(fm);
94 f_abs = fabs(fm);
95 } while (f_abs > _zero && f_abs < f_bdd);
96 if (f_abs < _zero)
97 return (xm);
98
99
100
101
102
104 }
105 }
106 return (x_f + dx);
107}
double zeroIntHalve(double(*fn)(double x), 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.