# Brent's method

In numerical analysis, Brent's method is a hybrid root-finding algorithm combining the bisection method, the secant method and inverse quadratic interpolation. It has the reliability of bisection but it can be as quick as some of the less-reliable methods. The algorithm tries to use the potentially fast-converging secant method or inverse quadratic interpolation if possible, but it falls back to the more robust bisection method if necessary. Brent's method is due to Richard Brent and builds on an earlier algorithm by Theodorus Dekker. Consequently, the method is also known as the Brent–Dekker method.
Modern improvements on Brent's method include Chandrupatla's method, which is simpler and faster for functions that are flat around their roots; Ridders' method, which performs exponential interpolations instead of quadratic providing a simpler closed formula for the iterations; and the ITP method which is a hybrid between regula-falsi and bisection that achieves optimal worst-case and asymptotic guarantees.

## Dekker's method

The idea to combine the bisection method with the secant method goes back to Dekker (1969).
Suppose that we want to solve the equation f(x) = 0. As with the bisection method, we need to initialize Dekker's method with two points, say a$_0$ and b$_0$, such that f(a$_0$) and f(b$_0$) have opposite signs. If f is continuous on [a$_0$, b$_0$], the intermediate value theorem guarantees the existence of a solution between a$_0$ and b$_0$.
Three points are involved in every iteration:
* b$_k$ is the current iterate, i.e., the current guess for the root of f.
* a$_k$ is the "contrapoint," i.e., a point such that f(a$_k$) and f(b$_k$) have opposite signs, so the interval [a$_k$, b$_k$] contains the solution. Furthermore, |f(b$_k$)| should be less than or equal to |f(a$_k$)|, so that b$_k$ is a better guess for the unknown solution than a$_k$.
* b$_{k−1}$ is the previous iterate (for the first iteration, we set b$_{k−1}$ = a$_0$).
Two provisional values for the next iterate are computed. The first one is given by linear interpolation, also known as the secant method:
       $s={\begin{cases}b_{k}-{\frac {b_{k}-b_{k-1}}{f(b_{k})-f(b_{k-1})}}f(b_{k}),&{\mbox{if }}f(b_{k})\neq f(b_{k-1})\\m&{\mbox{otherwise }}\end{cases}}$
and the second one is given by the bisection method
       $m={\frac {a_{k}+b_{k}}{2}}.$
If the result of the secant method, s, lies strictly between b$_k$ and m, then it becomes the next iterate (b$_{k+1}$ = s), otherwise the midpoint is used (b$_{k+1}$ = m).
Then, the value of the new contrapoint is chosen such that f(a$_{k+1}$) and f(b$_{k+1}$) have opposite signs. If f(a$_k$) and f(b$_{k+1}$) have opposite signs, then the contrapoint remains the same: a$_{k+1}$ = a$_k$. Otherwise, f(b$_{k+1}$) and f(b$_k$) have opposite signs, so the new contrapoint becomes a$_{k+1}$ = b$_k$.
Finally, if |f(a$_{k+1}$)| < |f(b$_{k+1}$)|, then a$_{k+1}$ is probably a better guess for the solution than b$_{k+1}$, and hence the values of a$_{k+1}$ and b$_{k+1}$ are exchanged.
This ends the description of a single iteration of Dekker's method.
Dekker's method performs well if the function f is reasonably well-behaved. However, there are circumstances in which every iteration employs the secant method, but the iterates b$_k$ converge very slowly (in particular, |b$_k$ − b$_{k−1}$| may be arbitrarily small). Dekker's method requires far more iterations than the bisection method in this case.

## Brent's method

Brent (1973) proposed a small modification to avoid the problem with Dekker's method. He inserts an additional test which must be satisfied before the result of the secant method is accepted as the next iterate. Two inequalities must be simultaneously satisfied:
Given a specific numerical tolerance $\delta$, if the previous step used the bisection method, the inequality |δ|<|b$_k$−b$_{k−1}$|{\textstyle |\delta |<|b_{k}-b_{k-1}|} must hold to perform interpolation, otherwise the bisection method is performed and its result used for the next iteration.
If the previous step performed interpolation, then the inequality |δ|<|b$_{k−1}$−b$_{k−2}$|{\textstyle |\delta |<|b_{k-1}-b_{k-2}|} is used instead to perform the next action (to choose) interpolation (when inequality is true) or bisection method (when inequality is not true).
Also, if the previous step used the bisection method, the inequality |s−b$_k$|< 1/2 |b$_k$−b$_{k−1}$|{\textstyle |s-b_{k}|<{\begin{matrix}{\frac {1}{2}}\end{matrix}}|b_{k}-b_{k-1}|} must hold, otherwise the bisection method is performed and its result used for the next iteration. If the previous step performed interpolation, then the inequality |s−b$_k$|< 1/2 |b$_{k−1}$−b$_{k−2}$|{\textstyle |s-b_{k}|<{\begin{matrix}{\frac {1}{2}}\end{matrix}}|b_{k-1}-b_{k-2}|} is used instead.
This modification ensures that at the kth iteration, a bisection step will be performed in at most $2\log _{2}(|b_{k-1}-b_{k-2}|/\delta )$ additional iterations, because the above conditions force consecutive interpolation step sizes to halve every two iterations, and after at most $2\log _{2}(|b_{k-1}-b_{k-2}|/\delta )$ iterations, the step size will be smaller than $\delta$, which invokes a bisection step. Brent proved that his method requires at most N$^2$ iterations, where N denotes the number of iterations for the bisection method. If the function f is well-behaved, then Brent's method will usually proceed by either inverse quadratic or linear interpolation, in which case it will converge superlinearly.
Furthermore, Brent's method uses inverse quadratic interpolation instead of linear interpolation (as used by the secant method). If f(b$_k$), f(a$_k$) and f(b$_{k−1}$) are distinct, it slightly increases the efficiency. As a consequence, the condition for accepting s (the value proposed by either linear interpolation or inverse quadratic interpolation) has to be changed: s has to lie between (3a$_k$ + b$_k$) / 4 and b$_k$.

## Example

Suppose that we are seeking a zero of the function defined by f(x) = (x + 3)(x − 1)$^2$.
We take [a$_0$, b$_0$] = [−4, 4/3] as our initial interval.
We have f(a$_0$) = −25 and f(b$_0$) = 0.48148 (all numbers in this section are rounded), so the conditions f(a$_0$) f(b$_0$) < 0 and |f(b$_0$)| ≤ |f(a$_0$)| are satisfied.
* In the first iteration, we use linear interpolation between (b$_{−1}$, f(b$_{−1}$)) = (a$_0$, f(a$_0$)) = (−4, −25) and (b$_0$, f(b$_0$)) = (1.33333, 0.48148), which yields s = 1.23256. This lies between (3a$_0$ + b$_0$) / 4 and b$_0$, so this value is accepted. Furthermore, f(1.23256) = 0.22891, so we set a$_1$ = a$_0$ and b$_1$ = s = 1.23256.
* In the second iteration, we use inverse quadratic interpolation between (a$_1$, f(a$_1$)) = (−4, −25) and (b$_0$, f(b$_0$)) = (1.33333, 0.48148) and (b$_1$, f(b$_1$)) = (1.23256, 0.22891). This yields 1.14205, which lies between (3a$_1$ + b$_1$) / 4 and b$_1$. Furthermore, the inequality |1.14205 − b$_1$| ≤ |b$_0$ − b$_{−1}$| / 2 is satisfied, so this value is accepted. Furthermore, f(1.14205) = 0.083582, so we set a$_2$ = a$_1$ and b$_2$ = 1.14205.
* In the third iteration, we use inverse quadratic interpolation between (a$_2$, f(a$_2$)) = (−4, −25) and (b$_1$, f(b$_1$)) = (1.23256, 0.22891) and (b$_2$, f(b$_2$)) = (1.14205, 0.083582). This yields 1.09032, which lies between (3a$_2$ + b$_2$) / 4 and b$_2$. But here Brent's additional condition kicks in: the inequality |1.09032 − b$_2$| ≤ |b$_1$ − b$_0$| / 2 is not satisfied, so this value is rejected. Instead, the midpoint m = −1.42897 of the interval [a$_2$, b$_2$] is computed. We have f(m) = 9.26891, so we set a$_3$ = a$_2$ and b$_3$ = −1.42897.
* In the fourth iteration, we use inverse quadratic interpolation between (a$_3$, f(a$_3$)) = (−4, −25) and (b$_2$, f(b$_2$)) = (1.14205, 0.083582) and (b$_3$, f(b$_3$)) = (−1.42897, 9.26891). This yields 1.15448, which is not in the interval between (3a$_3$ + b$_3$) / 4 and b$_3$). Hence, it is replaced by the midpoint m = −2.71449. We have f(m) = 3.93934, so we set a$_4$ = a$_3$ and b$_4$ = −2.71449.
* In the fifth iteration, inverse quadratic interpolation yields −3.45500, which lies in the required interval. However, the previous iteration was a bisection step, so the inequality |−3.45500 − b$_4$| ≤ |b$_4$ − b$_3$| / 2 need to be satisfied. This inequality is false, so we use the midpoint m = −3.35724. We have f(m) = −6.78239, so m becomes the new contrapoint (a$_5$ = −3.35724) and the iterate remains the same (b$_5$ = b$_4$).
* In the sixth iteration, we cannot use inverse quadratic interpolation because b$_5$ = b$_4$. Hence, we use linear interpolation between (a$_5$, f(a$_5$)) = (−3.35724, −6.78239) and (b$_5$, f(b$_5$)) = (−2.71449, 3.93934). The result is s = −2.95064, which satisfies all the conditions. But since the iterate did not change in the previous step, we reject this result and fall back to bisection. We update s = -3.03587, and f(s) = -0.58418.
* In the seventh iteration, we can again use inverse quadratic interpolation. The result is s = −3.00219, which satisfies all the conditions. Now, f(s) = −0.03515, so we set a$_7$ = b$_6$ and b$_7$ = −3.00219 (a$_7$ and b$_7$ are exchanged so that the condition |f(b$_7$)| ≤ |f(a$_7$)| is satisfied). (Correct : linear interpolation ⁠$s=-2.99436,f(s)=0.089961$⁠)
* In the eighth iteration, we cannot use inverse quadratic interpolation because a$_7$ = b$_6$. Linear interpolation yields s = −2.99994, which is accepted. (Correct : ⁠$s=-2.9999,f(s)=0.0016$⁠)
* In the following iterations, the root x = −3 is approached rapidly: b$_9$ = −3 + 6·10$^{−8}$ and b$_{10}$ = −3 − 3·10$^{−15}$. (Correct : Iter 9 : f(s) = −1.4 × 10$^{−7}$, Iter 10 : f(s) = 6.96 × 10$^{−12}$)

## Implementations

* Brent (1973) published an Algol 60 implementation.
* Netlib contains a Fortran translation of this implementation with slight modifications.
* The PARI/GP method solve implements the method.
* Other implementations of the algorithm (in C++, C, and Fortran) can be found in the Numerical Recipes books.
* The Apache Commons Math library implements the algorithm in Java.
* The SciPy optimize module implements the algorithm in Python (programming language)
* The Modelica Standard Library implements the algorithm in Modelica.
* The uniroot function implements the algorithm in R (software).
* The fzero function implements the algorithm in MATLAB.
* The Boost (C++ libraries) implements two algorithms based on Brent's method in C++ in the Math toolkit:
** Function minimization at minima.hpp with an example locating function minima.
** Root finding implements the newer TOMS748, a more modern and efficient algorithm than Brent's original, at TOMS748, and Boost.Math rooting finding that uses TOMS748 internally with examples.
* The Optim.jl package implements the algorithm in Julia (programming language)
* The Emmy computer algebra system (written in Clojure (programming language)) implements a variant of the algorithm designed for univariate function minimization.
* Root-Finding in C# library hosted in Code Project.