public class BrentRoot extends Object implements Uniroot
NewtonRoot and HalleyRoot, it does not need the
derivatives of the function.
Brent's algorithm is the preferred method of choice for root-finding.
Supply a continuous function and a finite interval bracketing a root
(opposite signs at the endpoints, or an endpoint equal to zero). The
three-argument solve overload does not need an initial guess.
The tolerance is used in a function-residual stopping test. The algorithm
also stops for a sufficiently small bracket or the iteration limit; verify
the returned residual when accuracy matters.
import dev.nm.analysis.function.rn2r1.univariate.AbstractUnivariateRealFunction;
import dev.nm.analysis.root.univariate.BrentRoot;
AbstractUnivariateRealFunction f = new AbstractUnivariateRealFunction() {
public double evaluate(double x) { return x * x - 2.0; }
};
double root = new BrentRoot(1e-12, 100).solve(f, 0.0, 2.0);
assert Math.abs(root - Math.sqrt(2.0)) < 1e-10;
assert Math.abs(f.evaluate(root)) < 1e-10;
| Constructor and Description |
|---|
BrentRoot(double tol,
int maxIterations)
Construct an instance of Brent's root finding algorithm.
|
| Modifier and Type | Method and Description |
|---|---|
double |
solve(UnivariateRealFunction f,
double lower,
double upper)
Finds a root in a bracketing interval.
|
double |
solve(UnivariateRealFunction f,
double lower,
double upper,
double... guess)
Search for a root, x, in the interval [lower, upper] such
that f(x) = 0.
|
public BrentRoot(double tol,
int maxIterations)
tol - the convergence tolerancemaxIterations - the maximum number of iterationspublic double solve(UnivariateRealFunction f, double lower, double upper, double... guess)
Unirootsolve in interface Unirootf - a univariate functionlower - the lower bound of the bracketing intervalupper - the upper bound of the bracketing intervalguess - an initial guess of the root within [lower, upper].
Note that guess is a double[].
This signature allows multiple initial guesses for certain types of
uniroot algorithms, e.g., Brent's algorithm.public double solve(UnivariateRealFunction f, double lower, double upper)
f - continuous function to evaluatelower - lower endpoint of the bracketupper - upper endpoint of the bracketRuntimeException - if both endpoint values have the same strict signCopyright © 2010-2026 NM Group. All Rights Reserved.