-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathzero_rc.cpp
407 lines (371 loc) · 7.33 KB
/
zero_rc.cpp
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
# include <cmath>
# include <cstdlib>
# include <ctime>
# include <iostream>
using namespace std;
# include "zero_rc.hpp"
//****************************************************************************80
double r8_epsilon ( )
//****************************************************************************80
//
// Purpose:
//
// R8_EPSILON returns the R8 roundoff unit.
//
// Discussion:
//
// The roundoff unit is a number R which is a power of 2 with the
// property that, to the precision of the computer's arithmetic,
// 1 < 1 + R
// but
// 1 = ( 1 + R / 2 )
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 01 September 2012
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Output, double R8_EPSILON, the R8 round-off unit.
//
{
const double value = 2.220446049250313E-016;
return value;
}
//****************************************************************************80
double r8_max ( double x, double y )
//****************************************************************************80
//
// Purpose:
//
// R8_MAX returns the maximum of two R8's.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 18 August 2004
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Input, double X, Y, the quantities to compare.
//
// Output, double R8_MAX, the maximum of X and Y.
//
{
double value;
if ( y < x )
{
value = x;
}
else
{
value = y;
}
return value;
}
//****************************************************************************80
double r8_sign ( double x )
//****************************************************************************80
//
// Purpose:
//
// R8_SIGN returns the sign of an R8.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 18 October 2004
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Input, double X, the number whose sign is desired.
//
// Output, double R8_SIGN, the sign of X.
//
{
double value;
if ( x < 0.0 )
{
value = -1.0;
}
else
{
value = 1.0;
}
return value;
}
//****************************************************************************80
void timestamp ( )
//****************************************************************************80
//
// Purpose:
//
// TIMESTAMP prints the current YMDHMS date as a time stamp.
//
// Example:
//
// 31 May 2001 09:45:54 AM
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 24 September 2003
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// None
//
{
const int TIME_SIZE(40);
static char time_buffer[TIME_SIZE];
const struct tm *tm;
time_t now;
now = time ( NULL );
tm = localtime ( &now );
strftime ( time_buffer, TIME_SIZE, "%d %B %Y %I:%M:%S %p", tm );
cout << time_buffer << "\n";
return;
}
//****************************************************************************80
void zero_rc ( double a, double b, double t, double &arg, int &status,
double value )
//****************************************************************************80
//
// Purpose:
//
// ZERO_RC seeks the root of a function F(X) using reverse communication.
//
// 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 * r8_abs ( C ) + 2 * T.
//
// The routine is a revised version of the Brent zero finder
// algorithm, using reverse communication.
//
// 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:
//
// 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.
//
// Output, double &ARG, the currently considered point. The user
// does not need to initialize this value. On return with STATUS positive,
// the user is requested to evaluate the function at ARG, and return
// the value in VALUE. On return with STATUS zero, ARG is the routine's
// estimate for the function's zero.
//
// Input/output, int &STATUS, used to communicate between
// the user and the routine. The user only sets STATUS to zero on the first
// call, to indicate that this is a startup call. The routine returns STATUS
// positive to request that the function be evaluated at ARG, or returns
// STATUS as 0, to indicate that the iteration is complete and that
// ARG is the estimated zero
//
// Input, double VALUE, the function value at ARG, as requested
// by the routine on the previous call.
//
{
static double c;
static double d;
static double e;
static double fa;
static double fb;
static double fc;
double m;
static double macheps;
double p;
double q;
double r;
double s;
static double sa;
static double sb;
double tol;
//
// Input STATUS = 0.
// Initialize, request F(A).
//
if ( status == 0 )
{
macheps = r8_epsilon ( );
sa = a;
sb = b;
e = sb - sa;
d = e;
status = 1;
arg = a;
return;
}
//
// Input STATUS = 1.
// Receive F(A), request F(B).
//
else if ( status == 1 )
{
fa = value;
status = 2;
arg = sb;
return;
}
//
// Input STATUS = 2
// Receive F(B).
//
else if ( status == 2 )
{
fb = value;
if ( 0.0 < fa * fb )
{
status = -1;
return;
}
c = sa;
fc = fa;
}
else
{
fb = value;
if ( ( 0.0 < fb && 0.0 < fc ) || ( fb <= 0.0 && fc <= 0.0 ) )
{
c = sa;
fc = fa;
e = sb - sa;
d = e;
}
}
//
// Compute the next point at which a function value is requested.
//
if ( fabs ( fc ) < fabs ( fb ) )
{
sa = sb;
sb = c;
c = sa;
fa = fb;
fb = fc;
fc = fa;
}
tol = 2.0 * macheps * fabs ( sb ) + t;
m = 0.5 * ( c - sb );
if ( fabs ( m ) <= tol || fb == 0.0 )
{
status = 0;
arg = sb;
return;
}
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;
}
arg = sb;
status = status + 1;
return;
}