Skip to content

Commit

Permalink
RootFinding: proper zero comparison early at boundaries; xml doc
Browse files Browse the repository at this point in the history
  • Loading branch information
cdrnet committed May 5, 2013
1 parent ca033cb commit 50d1cef
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
10 changes: 8 additions & 2 deletions src/Numerics/RootFinding/Algorithms/Bisection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,14 @@ public static double FindRoot(Func<double, double> 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))
{
Expand Down
14 changes: 8 additions & 6 deletions src/Numerics/RootFinding/Algorithms/HybridNewtonRaphson.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ public static class HybridNewtonRaphson
/// <param name="df">The first derivative of the function to find roots from.</param>
/// <param name="lowerBound">The low value of the range where the root is supposed to be.</param>
/// <param name="upperBound">The high value of the range where the root is supposed to be.</param>
/// <param name="accuracy">Desired accuracy. The root will be refined until the accuracy or the maximum number of iterations is reached.</param>
/// <param name="maxIterations">Maximum number of iterations.</param>
/// <param name="accuracy">Desired accuracy. The root will be refined until the accuracy or the maximum number of iterations is reached. Example: 1e-14.</param>
/// <param name="maxIterations">Maximum number of iterations. Example: 100.</param>
/// <param name="subdivision">How many parts an interval should be split into for zero crossing scanning in case of lacking bracketing. Example: 20.</param>
/// <returns>Returns the root with the specified accuracy.</returns>
/// <remarks>Hybrid Newton-Raphson that falls back to bisection when overshooting or converging too slow, or to subdivision on lacking bracketing.</remarks>
/// <exception cref="NonConvergenceException"></exception>
Expand All @@ -59,8 +60,9 @@ public static double FindRoot(Func<double, double> f, Func<double, double> df, d
/// <param name="df">The first derivative of the function to find roots from.</param>
/// <param name="lowerBound">The low value of the range where the root is supposed to be.</param>
/// <param name="upperBound">The high value of the range where the root is supposed to be.</param>
/// <param name="accuracy">Desired accuracy. The root will be refined until the accuracy or the maximum number of iterations is reached.</param>
/// <param name="maxIterations">Maximum number of iterations.</param>
/// <param name="accuracy">Desired accuracy. The root will be refined until the accuracy or the maximum number of iterations is reached. Example: 1e-14.</param>
/// <param name="maxIterations">Maximum number of iterations. Example: 100.</param>
/// <param name="subdivision">How many parts an interval should be split into for zero crossing scanning in case of lacking bracketing. Example: 20.</param>
/// <param name="root">The root that was found, if any. Undefined if the function returns false.</param>
/// <returns>True if a root with the specified accuracy was found, else false.</returns>
/// <remarks>Hybrid Newton-Raphson that falls back to bisection when overshooting or converging too slow, or to subdivision on lacking bracketing.</remarks>
Expand All @@ -69,12 +71,12 @@ public static bool TryFindRoot(Func<double, double> f, Func<double, double> 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;
Expand Down

0 comments on commit 50d1cef

Please sign in to comment.