23template<std::invocable<
double> T>
double ridders(
const T& func,
double x1,
double f1,
double x2,
double f2,
const double tolerance) {
30 const auto x3 = .5 * (x1 + x2);
31 const auto f3 = func(x3);
32 if(std::fabs(f3) < tolerance || fabs(x2 - x1) < tolerance) {
37 const auto dx = (x3 - x1) * f3 / std::sqrt(f3 * f3 - f1 * f2);
39 const auto x4 = f1 > f2 ? x3 + dx : x3 - dx;
40 const auto f4 = func(x4);
41 if(std::fabs(f4) < tolerance) {
52 else if(f4 * f2 < 0.) {
61 suanpan_debug(
"Ridders' method initial guess {:.5E} with {} iterations.\n", target, counter);
double ridders(const T &func, double x1, double f1, double x2, double f2, const double tolerance)
Definition: ridders.hpp:23
#define suanpan_debug(...)
Definition: suanPan.h:307