29 throw MooseException(
"Bad initial range (0) used in BrentsMethod::bracket");
36 unsigned int iter = 0;
37 std::stringstream debug_ss;
41 debug_ss <<
" iteration " << iter <<
": (x1,x2) = (" << x1 <<
"," << x2 <<
"), (f1,f2) = (" 42 << f1 <<
"," << f2 <<
")\n";
44 if (std::abs(f1) < std::abs(f2))
46 x1 += factor * (x1 - x2);
47 x1 = (x1 <
eps ?
eps : x1);
52 x2 += factor * (x2 - x1);
53 x2 = (x2 <
eps ?
eps : x2);
59 throw MooseException(
"No bracketing interval found by BrentsMethod::bracket after " +
66 root(std::function<
Real(Real)>
const &
f, Real x1, Real x2, Real
tol)
68 Real a = x1,
b = x2,
c = x2,
d = 0.0, e = 0.0, min1, min2;
71 Real fc, p, q, r, s, tol1, xm = 0;
72 unsigned int iter_max = 100;
76 throw MooseException(
"Root must be bracketed in BrentsMethod::root");
79 std::stringstream debug_ss;
80 for (
unsigned int i = 1; i <= iter_max; ++i)
83 debug_ss <<
" iteration " << i <<
": dx = " << xm <<
", x = " <<
b <<
", f(x) = " << fb
94 if (std::abs(fc) < std::abs(fb))
104 tol1 = 2.0 *
eps * std::abs(
b) + 0.5 *
tol;
107 if (std::abs(xm) <= tol1 || fb == 0.0)
110 if (std::abs(e) >= tol1 && std::abs(fa) > std::abs(fb))
123 p = s * (2.0 * xm * q * (q - r) - (
b -
a) * (r - 1.0));
124 q = (q - 1.0) * (r - 1.0) * (s - 1.0);
130 min1 = 3.0 * xm * q - std::abs(tol1 * q);
131 min2 = std::abs(e * q);
133 if (2.0 * p < (min1 < min2 ? min1 : min2))
156 if (std::abs(
d) > tol1)
160 Real sgn = (xm >= 0.0 ? std::abs(tol1) : -std::abs(tol1));
167 throw MooseException(
"Maximum number of iterations exceeded in BrentsMethod::root.\n" +
int sgn(T val)
The sign function.
Real f(Real x)
Test function for Brents method.
Real root(std::function< Real(Real)> const &f, Real x1, Real x2, Real tol=1.0e-12)
Finds the root of a function using Brent's method.
std::string stringify(const T &t)
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
Brent's method is used to find the root of a function f(x), i.e., find x such that f(x) = 0...
void bracket(std::function< Real(Real)> const &f, Real &x1, Real &x2)
Function to bracket a root of a given function.