numerics 0.1.0
Loading...
Searching...
No Matches
roots.hpp
Go to the documentation of this file.
1/// @file roots.hpp
2/// @brief Root-finding methods for scalar equations f(x) = 0
3#pragma once
4
5#include "analysis/types.hpp"
6
7namespace num {
8
9struct RootResult {
12 real residual; ///< |f(root)|
14};
15
16/// @brief Bisection method
17/// @param f Continuous function
18/// @param a,b Bracket: f(a) and f(b) must have opposite signs
19/// @param tol Convergence tolerance on |f(root)| and interval width
20/// @param max_iter Maximum iterations
21RootResult bisection(ScalarFn f, real a, real b, real tol = 1e-10, idx max_iter = 1000);
22
23/// @brief Newton-Raphson method
24/// @param f Function
25/// @param df Derivative of f
26/// @param x0 Initial guess
27/// @param tol Convergence tolerance
28/// @param max_iter Maximum iterations
30 ScalarFn df,
31 real x0,
32 real tol = 1e-10,
33 idx max_iter = 1000);
34
35/// @brief Secant method (Newton without derivative)
36/// @param f Function
37/// @param x0,x1 Two distinct initial guesses
38/// @param tol Convergence tolerance
39/// @param max_iter Maximum iterations
40RootResult secant(ScalarFn f, real x0, real x1, real tol = 1e-10, idx max_iter = 1000);
41
42/// @brief Brent's method (bisection + secant + inverse quadratic interpolation)
43///
44/// Guaranteed to converge if f(a) and f(b) have opposite signs.
45/// Superlinear convergence near smooth roots.
46/// Preferred method when a reliable bracket is available.
47///
48/// @param f Function
49/// @param a,b Bracket: f(a) and f(b) must have opposite signs
50/// @param tol Convergence tolerance
51/// @param max_iter Maximum iterations
52RootResult brent(ScalarFn f, real a, real b, real tol = 1e-10, idx max_iter = 1000);
53
54} // namespace num
Common function type aliases for the analysis module.
RootResult secant(ScalarFn f, real x0, real x1, real tol=1e-10, idx max_iter=1000)
Secant method (Newton without derivative)
Definition roots.cpp:44
double real
Definition types.hpp:10
RootResult bisection(ScalarFn f, real a, real b, real tol=1e-10, idx max_iter=1000)
Bisection method.
Definition roots.cpp:8
RootResult brent(ScalarFn f, real a, real b, real tol=1e-10, idx max_iter=1000)
Brent's method (bisection + secant + inverse quadratic interpolation)
Definition roots.cpp:61
std::size_t idx
Definition types.hpp:11
RootResult newton(ScalarFn f, ScalarFn df, real x0, real tol=1e-10, idx max_iter=1000)
Newton-Raphson method.
Definition roots.cpp:30
constexpr real e
Definition math.hpp:44
std::function< real(real)> ScalarFn
Real-valued scalar function f(x)
Definition types.hpp:11
real residual
|f(root)|
Definition roots.hpp:12