Go to the source code of this file.
|
| template<std::floating_point T, std::invocable< T > F> |
| T | brent (F &&func, const T x1, const T x2, const T tol) |
| | Implements Brent's method for finding a root of a function within a given interval.
|
| |
◆ brent()
template<std::floating_point T, std::invocable<
T > F>
| T brent |
( |
F && |
func, |
|
|
const T |
x1, |
|
|
const T |
x2, |
|
|
const T |
tol |
|
) |
| |
Implements Brent's method for finding a root of a function within a given interval.
Brent's method combines the bisection method, the secant method, and inverse quadratic interpolation to efficiently find a root of a function. It is robust and guarantees convergence as long as the function changes sign over the interval [x1, x2].
- Template Parameters
-
| T | The floating-point type (e.g., float, double, long double). |
| F | The callable type representing the function to find the root of. Must accept a single argument of type T and return a value of type T. |
- Parameters
-
| func | The function for which the root is to be found. It must be continuous and change sign over the interval [x1, x2]. |
| x1 | The lower bound of the interval. |
| x2 | The upper bound of the interval. |
| tol | The desired tolerance for the root. The algorithm stops when the root is found to this precision. |
- Returns
- The approximate root of the function within the given interval and tolerance.
- Note
- The function assumes that the initial interval [x1, x2] contains a root (i.e., func(x1) and func(x2) have opposite signs). If this condition is not met, the behavior is undefined.