Browse Source

Root Finding: Newton-Raphson to check for zero after each eval to avoid NaN. Closes #774.

pull/782/head
Christoph Ruegg 5 years ago
parent
commit
114f0eff27
  1. 5
      src/Numerics/RootFinding/NewtonRaphson.cs
  2. 15
      src/Numerics/RootFinding/RobustNewtonRaphson.cs

5
src/Numerics/RootFinding/NewtonRaphson.cs

@ -99,6 +99,11 @@ namespace MathNet.Numerics.RootFinding
{
// Evaluation
double fx = f(root);
if (fx == 0.0)
{
return true;
}
double dfx = df(root);
// Netwon-Raphson step

15
src/Numerics/RootFinding/RobustNewtonRaphson.cs

@ -91,6 +91,11 @@ namespace MathNet.Numerics.RootFinding
root = 0.5*(lowerBound + upperBound);
double fx = f(root);
if (fx == 0.0)
{
return true;
}
double lastStep = Math.Abs(upperBound - lowerBound);
for (int i = 0; i < maxIterations; i++)
{
@ -119,6 +124,11 @@ namespace MathNet.Numerics.RootFinding
// Bisection
root = 0.5*(upperBound + lowerBound);
fx = f(root);
if (fx == 0.0)
{
return true;
}
lastStep = 0.5*Math.Abs(upperBound - lowerBound);
if (Math.Sign(fx) == Math.Sign(fmin))
{
@ -146,6 +156,11 @@ namespace MathNet.Numerics.RootFinding
// Evaluation
fx = f(root);
if (fx == 0.0)
{
return true;
}
lastStep = step;
// Update bounds

Loading…
Cancel
Save