1 #include "../icedb/zeros.hpp" 7 double findzero(
double a,
double b,
const std::function<
double(
double) > &f )
17 double convint = 1E-7;
19 double fa, fb, fc, fd, fs = 1.0;
27 .add<std::string>(
"Reason",
28 "Bad selection of a,b. f(a) and f(b) need to have opposite signs.")
29 .add<double>(
"a",a).add<
double>(
"b",b)
30 .add<double>(
"fa",fa).add<
double>(
"fb",fb);
32 if ( abs(fa) < abs(fb) )
46 while (fb != 0 || fs != 0 || abs(b-a) > convint)
48 if (fa != fc && fb != fc)
52 i = a * fb * fc / ( (fa - fb)*(fa - fc));
53 j = b * fa * fc / ( (fb - fa)*(fb - fc));
54 k = c * fa * fb / ( (fc - fa)*(fc - fb));
58 s = b - fb * (b - a) / ( fb - fa );
62 ( (3.0*a+b)/4.0 < s && s < b) ==
false ||
63 (mflag && abs(s-b) >= (abs(b-c)/2.0) ) ||
64 (!mflag && abs(s-b) >= (abs(c-d)/2.0) ) ||
65 (mflag && abs(b-c) < convint) ||
66 (!mflag && abs(c-d) < convint)
94 if ( abs(fa) < abs(fb) )
105 if ( abs(b-a) < convint && abs(fb) < fcint && abs(fs) < fcint )
break;
double findzero(double a, double b, const std::function< double(double) > &f)
Zero-finding implementation - Brent's method.