I have forgotten

COST (GBP)
2.98
0.00
0

# Brent

Calculates the zeros of a function using Brent's method.
Controller: CodeCogs
Contents

C++

## Brent

 doublebrent( double (*f)(double)[function pointer] double x1 = -1E+7 double x2 = 1E+7 double eps = 1E-10 int maxit = 1000 )
Brent's method is a root finding algorithm which combines root bracketing, interval bisection and inverse quadratic interpolation. It is sometimes known as the van Wijngaarden-Deker-Brent method.

The main algorithm uses a Lagrange interpolating polynomial of degree 2. Brent (1973) claims that this method will always converge as long as the values of the function are computable within a given region containing a root. Given three points $\inline&space;x_1$, $\inline&space;x_2$ and $\inline&space;x_3$, Brent's method fits x as a quadratic function of y, then uses the interpolation formula
$x&space;=&space;\sum_{k&space;=&space;1}^3&space;x_k&space;\prod_{1&space;\leq&space;i&space;\leq&space;3&space;\quad&space;i&space;\neq&space;k}&space;\frac&space;{y&space;-&space;f(x_i)}&space;{f(x_k)&space;-&space;f(x_i)}$

Subsequent root estimates are obtained by setting $\inline&space;y&space;=&space;0$, giving
$x&space;=&space;x_2&space;+&space;\frac&space;{P}&space;{Q}$
where
$P&space;=&space;S[T(R&space;-&space;T)(x_3&space;-&space;x_2)&space;-&space;(1&space;-&space;R)(x_2&space;-&space;x_1)]&space;\f]&space;\f[&space;Q&space;=&space;(T&space;-&space;1)(R&space;-&space;1)(S&space;-&space;1)$
with
$R&space;=&space;\frac&space;{f(x_2)}&space;{f(x_3)}&space;\qquad&space;S&space;=&space;\frac&space;{f(x_2)}&space;{f(x_1)}&space;\qquad&space;T&space;=&space;\frac&space;{f(x_1)}&space;{f(x_3)}$

This algorithm finds the roots of the user-defined function f starting with an initial interval [x1, x2] and iterating the sequence above until either the accuracy eps is achieved or the maximum number of iterations maxit is exceeded.

## References:

• MathWorld, http://mathworld.wolfram.com/BrentsMethod.html

### Example 1

#include <codecogs/maths/rootfinding/brent.h>

#include <iostream>
#include <iomanip>

// user-defined function
double f(double x) {
return (x + 1) * (x + 1) * (x + 1);
}

int main()
{
double x = Maths::RootFinding::brent(f, -3, 0);

std::cout << "The calculated zero is X = " << std::setprecision(12) << x << std::endl;
std::cout << "The associated ordinate value is Y = " << f(x) << std::endl;
return 0;
}
Output:
The calculated zero is X = -0.999999878599
The associated ordinate value is Y = 1.78923630993e-021

### Parameters

 f the user-defined function x1 Default value = -1E+7 x2 Default value = 1E+7 eps Default value = 1E-10 maxit Default value = 1000

### Authors

Lucian Bentea (August 2005)
##### Source Code

Source code is available when you buy a Commercial licence.

Not a member, then Register with CodeCogs. Already a Member, then Login.