-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzero.c
180 lines (165 loc) · 3.75 KB
/
zero.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include "zero.h"
#include <math.h>
static const double r8_epsilon = 2.220446049250313E-016;
double liknorm_zero(const double a, const double b, const double t,
liknorm_func_base *f, void *args)
// ****************************************************************************80
//
// Purpose:
//
// ZERO seeks the root of a function F(X) in an interval [A,B].
//
// Discussion:
//
// The interval [A,B] must be a change of sign interval for F.
// That is, F(A) and F(B) must be of opposite signs. Then
// assuming that F is continuous implies the existence of at least
// one value C between A and B for which F(C) = 0.
//
// The location of the zero is determined to within an accuracy
// of 6 * MACHEPS * fabs ( C ) + 2 * T.
//
// Thanks to Thomas Secretin for pointing out a transcription error in the
// setting of the value of P, 11 February 2013.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 11 February 2013
//
// Author:
//
// Original FORTRAN77 version by Richard Brent.
// C++ version by John Burkardt.
//
// Reference:
//
// Richard Brent,
// Algorithms for Minimization Without Derivatives,
// Dover, 2002,
// ISBN: 0-486-41998-3,
// LC: QA402.5.B74.
//
// Parameters:
//
// Input, double A, B, the endpoints of the change of sign interval.
//
// Input, double T, a positive error tolerance.
//
// Input, func_base& F, the name of a user-supplied c++ functor
// whose zero is being sought. The input and output
// of F() are of type double.
//
// Output, double ZERO, the estimated value of a zero of
// the function F.
//
{
double c;
double d;
double e;
double fa;
double fb;
double fc;
double p;
double q;
double r;
double s;
double sa;
double sb;
//
// Make local copies of A and B.
//
sa = a;
sb = b;
fa = f(sa, args);
fb = f(sb, args);
c = sa;
fc = fa;
e = sb - sa;
d = e;
for (;;)
{
if (fabs(fc) < fabs(fb))
{
sa = sb;
sb = c;
c = sa;
fa = fb;
fb = fc;
fc = fa;
}
double tol = 2.0 * r8_epsilon * fabs(sb) + t;
double m = 0.5 * (c - sb);
if ((fabs(m) <= tol) || (fb == 0.0))
{
break;
}
if ((fabs(e) < tol) || (fabs(fa) <= fabs(fb)))
{
e = m;
d = e;
}
else
{
s = fb / fa;
if (sa == c)
{
p = 2.0 * m * s;
q = 1.0 - s;
}
else
{
q = fa / fc;
r = fb / fc;
p = s * (2.0 * m * q * (q - r) - (sb - sa) * (r - 1.0));
q = (q - 1.0) * (r - 1.0) * (s - 1.0);
}
if (0.0 < p)
{
q = -q;
}
else
{
p = -p;
}
s = e;
e = d;
if ((2.0 * p < 3.0 * m * q - fabs(tol * q)) &&
(p < fabs(0.5 * s * q)))
{
d = p / q;
}
else
{
e = m;
d = e;
}
}
sa = sb;
fa = fb;
if (tol < fabs(d))
{
sb = sb + d;
}
else if (0.0 < m)
{
sb = sb + tol;
}
else
{
sb = sb - tol;
}
fb = f(sb, args);
if (((0.0 < fb) && (0.0 < fc)) || ((fb <= 0.0) && (fc <= 0.0)))
{
c = sa;
fc = fa;
e = sb - sa;
d = e;
}
}
return sb;
}