suanPan
Loading...
Searching...
No Matches
brent.hpp File Reference
#include <suanPan.h>
Include dependency graph for brent.hpp:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

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.
 

Function Documentation

◆ 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
TThe floating-point type (e.g., float, double, long double).
FThe 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
funcThe function for which the root is to be found. It must be continuous and change sign over the interval [x1, x2].
x1The lower bound of the interval.
x2The upper bound of the interval.
tolThe 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.
Here is the caller graph for this function: