Browse Source

RootFinding: proper zero comparison early at boundaries; xml doc

v2
Christoph Ruegg 14 years ago
parent
commit
50d1cef552
  1. 10
      src/Numerics/RootFinding/Algorithms/Bisection.cs
  2. 14
      src/Numerics/RootFinding/Algorithms/HybridNewtonRaphson.cs

10
src/Numerics/RootFinding/Algorithms/Bisection.cs

@ -50,8 +50,14 @@ namespace MathNet.Numerics.RootFinding.Algorithms
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))
{

14
src/Numerics/RootFinding/Algorithms/HybridNewtonRaphson.cs

@ -39,8 +39,9 @@ namespace MathNet.Numerics.RootFinding.Algorithms
/// <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>
@ -59,8 +60,9 @@ namespace MathNet.Numerics.RootFinding.Algorithms
/// <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>
@ -69,12 +71,12 @@ namespace MathNet.Numerics.RootFinding.Algorithms
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;

Loading…
Cancel
Save