Class RootFinder
Provides static methods for bisection, Brent's method, Newton-Raphson, and the secant method. All methods are stateless and thread-safe.
提供二分法、Brent 法、牛顿-拉弗森法和割线法的静态方法。 所有方法无状态且线程安全。
- Since:
- JDK 25, opencode-base-math V1.0.3
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic doublebisection(DoubleUnaryOperator f, double a, double b, double tolerance) Finds a root offin the interval[a, b]using the bisection method.static doublebrent(DoubleUnaryOperator f, double a, double b, double tolerance) Finds a root offin the interval[a, b]using Brent's method.static doublenewton(DoubleUnaryOperator f, DoubleUnaryOperator df, double x0, double tolerance) Finds a root offusing the Newton-Raphson method.static doublesecant(DoubleUnaryOperator f, double x0, double x1, double tolerance) Finds a root offusing the secant method.
-
Method Details
-
bisection
Finds a root offin the interval[a, b]using the bisection method. 使用二分法在区间 [a, b] 内查找函数 f 的根Requires
f(a)andf(b)to have opposite signs (or one is zero).要求 f(a) 与 f(b) 异号(或其中一个为零)。
- Parameters:
f- the function / 函数a- left endpoint of the interval / 区间左端点b- right endpoint of the interval / 区间右端点tolerance- convergence tolerance on|b - a|/ 收敛容差,基于 |b - a|- Returns:
- an approximate root / 近似根
- Throws:
MathException- if inputs are invalid or the method does not converge
-
brent
Finds a root offin the interval[a, b]using Brent's method. 使用 Brent 法在区间 [a, b] 内查找函数 f 的根Combines bisection, secant, and inverse quadratic interpolation for robust and fast convergence. This is the recommended general-purpose root finder.
结合二分法、割线法和逆二次插值以实现稳健且快速的收敛。 这是推荐的通用求根方法。
- Parameters:
f- the function / 函数a- left endpoint of the interval / 区间左端点b- right endpoint of the interval / 区间右端点tolerance- convergence tolerance / 收敛容差- Returns:
- an approximate root / 近似根
- Throws:
MathException- if inputs are invalid or the method does not converge
-
newton
public static double newton(DoubleUnaryOperator f, DoubleUnaryOperator df, double x0, double tolerance) Finds a root offusing the Newton-Raphson method. 使用牛顿-拉弗森法查找函数 f 的根Requires both the function and its derivative. Convergence is quadratic near simple roots but may diverge for poor initial guesses.
需要提供函数及其导数。在简单根附近为二次收敛, 但初始猜测不佳时可能发散。
- Parameters:
f- the function / 函数df- the derivative of f / f 的导数x0- initial guess / 初始猜测值tolerance- convergence tolerance on|f(x)|/ 收敛容差,基于 |f(x)|- Returns:
- an approximate root / 近似根
- Throws:
MathException- if the derivative is too close to zero or the method does not converge
-
secant
Finds a root offusing the secant method. 使用割线法查找函数 f 的根Similar to Newton's method but approximates the derivative using two function evaluations. Does not require an explicit derivative.
类似牛顿法,但使用两个函数值近似导数。无需提供显式导数。
- Parameters:
f- the function / 函数x0- first initial point / 第一个初始点x1- second initial point / 第二个初始点tolerance- convergence tolerance on|f(x)|/ 收敛容差,基于 |f(x)|- Returns:
- an approximate root / 近似根
- Throws:
MathException- if the method does not converge
-