From 50d1cef5522a2465539045677ea8537c9a3347e5 Mon Sep 17 00:00:00 2001 From: Christoph Ruegg Date: Sun, 5 May 2013 09:58:55 +0200 Subject: [PATCH] RootFinding: proper zero comparison early at boundaries; xml doc --- src/Numerics/RootFinding/Algorithms/Bisection.cs | 10 ++++++++-- .../RootFinding/Algorithms/HybridNewtonRaphson.cs | 14 ++++++++------ 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/src/Numerics/RootFinding/Algorithms/Bisection.cs b/src/Numerics/RootFinding/Algorithms/Bisection.cs index 71b109d27..5ad802d12 100644 --- a/src/Numerics/RootFinding/Algorithms/Bisection.cs +++ b/src/Numerics/RootFinding/Algorithms/Bisection.cs @@ -50,8 +50,14 @@ public static double FindRoot(Func f, double lowerBound, double double fmin = f(lowerBound); double fmax = f(upperBound); - if (fmin == 0.0) return lowerBound; - if (fmax == 0.0) return upperBound; + if (Math.Abs(fmin) < accuracy) + { + return lowerBound; + } + if (Math.Abs(fmax) < accuracy) + { + return upperBound; + } if (Math.Sign(fmin) == Math.Sign(fmax)) { diff --git a/src/Numerics/RootFinding/Algorithms/HybridNewtonRaphson.cs b/src/Numerics/RootFinding/Algorithms/HybridNewtonRaphson.cs index 11093b1a7..0e05b4fea 100644 --- a/src/Numerics/RootFinding/Algorithms/HybridNewtonRaphson.cs +++ b/src/Numerics/RootFinding/Algorithms/HybridNewtonRaphson.cs @@ -39,8 +39,9 @@ public static class HybridNewtonRaphson /// The first derivative of the function to find roots from. /// The low value of the range where the root is supposed to be. /// The high value of the range where the root is supposed to be. - /// Desired accuracy. The root will be refined until the accuracy or the maximum number of iterations is reached. - /// Maximum number of iterations. + /// Desired accuracy. The root will be refined until the accuracy or the maximum number of iterations is reached. Example: 1e-14. + /// Maximum number of iterations. Example: 100. + /// How many parts an interval should be split into for zero crossing scanning in case of lacking bracketing. Example: 20. /// Returns the root with the specified accuracy. /// Hybrid Newton-Raphson that falls back to bisection when overshooting or converging too slow, or to subdivision on lacking bracketing. /// @@ -59,8 +60,9 @@ public static double FindRoot(Func f, Func df, d /// The first derivative of the function to find roots from. /// The low value of the range where the root is supposed to be. /// The high value of the range where the root is supposed to be. - /// Desired accuracy. The root will be refined until the accuracy or the maximum number of iterations is reached. - /// Maximum number of iterations. + /// Desired accuracy. The root will be refined until the accuracy or the maximum number of iterations is reached. Example: 1e-14. + /// Maximum number of iterations. Example: 100. + /// How many parts an interval should be split into for zero crossing scanning in case of lacking bracketing. Example: 20. /// The root that was found, if any. Undefined if the function returns false. /// True if a root with the specified accuracy was found, else false. /// Hybrid Newton-Raphson that falls back to bisection when overshooting or converging too slow, or to subdivision on lacking bracketing. @@ -69,12 +71,12 @@ public static bool TryFindRoot(Func f, Func df, double fmin = f(lowerBound); double fmax = f(upperBound); - if (fmin == 0.0) + if (Math.Abs(fmin) < accuracy) { root = lowerBound; return true; } - if (fmax == 0.0) + if (Math.Abs(fmax) < accuracy) { root = upperBound; return true;