forked from tsai/mathnet-numerics
13 changed files with 275 additions and 21 deletions
@ -0,0 +1,107 @@ |
|||
// <copyright file="NewtonRaphson.cs" company="Math.NET">
|
|||
// Math.NET Numerics, part of the Math.NET Project
|
|||
// http://numerics.mathdotnet.com
|
|||
// http://github.com/mathnet/mathnet-numerics
|
|||
// http://mathnetnumerics.codeplex.com
|
|||
//
|
|||
// Copyright (c) 2009-2013 Math.NET
|
|||
//
|
|||
// Permission is hereby granted, free of charge, to any person
|
|||
// obtaining a copy of this software and associated documentation
|
|||
// files (the "Software"), to deal in the Software without
|
|||
// restriction, including without limitation the rights to use,
|
|||
// copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|||
// copies of the Software, and to permit persons to whom the
|
|||
// Software is furnished to do so, subject to the following
|
|||
// conditions:
|
|||
//
|
|||
// The above copyright notice and this permission notice shall be
|
|||
// included in all copies or substantial portions of the Software.
|
|||
//
|
|||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|||
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|||
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|||
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|||
// OTHER DEALINGS IN THE SOFTWARE.
|
|||
// </copyright>
|
|||
|
|||
using System; |
|||
|
|||
namespace MathNet.Numerics.RootFinding.Algorithms |
|||
{ |
|||
public static class NewtonRaphson |
|||
{ |
|||
/// <summary>Find a solution of the equation f(x)=0.</summary>
|
|||
/// <remarks>Hybrid Newton-Raphson that when failing falls back to bisection.</remarks>
|
|||
/// <exception cref="NonConvergenceException"></exception>
|
|||
public static double FindRoot(Func<double, double> f, Func<double, double> df, double lowerBound, double upperBound, double accuracy = 1e-8, int maxIterations = 100) |
|||
{ |
|||
double fmin = f(lowerBound); |
|||
double fmax = f(upperBound); |
|||
|
|||
if (fmin == 0.0) return lowerBound; |
|||
if (fmax == 0.0) return upperBound; |
|||
|
|||
double root = 0.5*(lowerBound + upperBound); |
|||
double fx = f(root); |
|||
double lastStep = Math.Abs(upperBound - lowerBound); |
|||
for (int i = 0; i < maxIterations; i++) |
|||
{ |
|||
double dfx = df(root); |
|||
|
|||
// Netwon-Raphson step
|
|||
double step = fx/dfx; |
|||
root -= step; |
|||
|
|||
if (root < lowerBound || root > upperBound || Math.Abs(2*fx) > Math.Abs(lastStep*dfx)) |
|||
{ |
|||
// Newton-Raphson step failed -> bisect instead
|
|||
root = 0.5*(upperBound + lowerBound); |
|||
fx = f(root); |
|||
lastStep = 0.5*Math.Abs(upperBound - lowerBound); |
|||
if (Math.Sign(fx) == Math.Sign(fmin)) |
|||
{ |
|||
lowerBound = root; |
|||
fmin = fx; |
|||
} |
|||
else |
|||
{ |
|||
upperBound = root; |
|||
fmax = fx; |
|||
} |
|||
continue; |
|||
} |
|||
|
|||
if (Math.Abs(step) < accuracy) |
|||
{ |
|||
return root; |
|||
} |
|||
|
|||
// Evaluation
|
|||
fx = f(root); |
|||
lastStep = step; |
|||
|
|||
// Update Bounds
|
|||
if (Math.Sign(fx) != Math.Sign(fmin)) |
|||
{ |
|||
upperBound = root; |
|||
fmax = fx; |
|||
} |
|||
else if (Math.Sign(fx) != Math.Sign(fmax)) |
|||
{ |
|||
lowerBound = root; |
|||
fmin = fx; |
|||
} |
|||
else if (Math.Sign(fmin) != Math.Sign(fmax)) |
|||
{ |
|||
return root; |
|||
} |
|||
} |
|||
|
|||
throw new NonConvergenceException("The algorithm has exceeded the number of iterations allowed"); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,80 @@ |
|||
// <copyright file="BrentTest.cs" company="Math.NET">
|
|||
// Math.NET Numerics, part of the Math.NET Project
|
|||
// http://numerics.mathdotnet.com
|
|||
// http://github.com/mathnet/mathnet-numerics
|
|||
// http://mathnetnumerics.codeplex.com
|
|||
//
|
|||
// Copyright (c) 2009-2013 Math.NET
|
|||
//
|
|||
// Permission is hereby granted, free of charge, to any person
|
|||
// obtaining a copy of this software and associated documentation
|
|||
// files (the "Software"), to deal in the Software without
|
|||
// restriction, including without limitation the rights to use,
|
|||
// copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|||
// copies of the Software, and to permit persons to whom the
|
|||
// Software is furnished to do so, subject to the following
|
|||
// conditions:
|
|||
//
|
|||
// The above copyright notice and this permission notice shall be
|
|||
// included in all copies or substantial portions of the Software.
|
|||
//
|
|||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|||
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|||
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|||
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|||
// OTHER DEALINGS IN THE SOFTWARE.
|
|||
// </copyright>
|
|||
|
|||
using System; |
|||
using MathNet.Numerics.RootFinding.Algorithms; |
|||
using NUnit.Framework; |
|||
|
|||
namespace MathNet.Numerics.UnitTests.RootFindingTests |
|||
{ |
|||
[TestFixture] |
|||
public class NewtonRaphsonTest |
|||
{ |
|||
[Test] |
|||
public void MultipleRoots() |
|||
{ |
|||
// Roots at -2, 2
|
|||
Func<double, double> f1 = x => x * x - 4; |
|||
Func<double, double> df1 = x => 2 * x; |
|||
Assert.AreEqual(0, f1(NewtonRaphson.FindRoot(f1, df1, -5, 5, 1e-14, 100))); |
|||
Assert.AreEqual(-2, NewtonRaphson.FindRoot(f1, df1, -5, -1, 1e-14, 100)); |
|||
Assert.AreEqual(2, NewtonRaphson.FindRoot(f1, df1, 1, 4, 1e-14, 100)); |
|||
Assert.AreEqual(0, f1(NewtonRaphson.FindRoot(x => -f1(x), x => -df1(x), -5, 5, 1e-14, 100))); |
|||
Assert.AreEqual(-2, NewtonRaphson.FindRoot(x => -f1(x), x => -df1(x), -5, -1, 1e-14, 100)); |
|||
Assert.AreEqual(2, NewtonRaphson.FindRoot(x => -f1(x), x => -df1(x), 1, 4, 1e-14, 100)); |
|||
|
|||
// Roots at 3, 4
|
|||
Func<double, double> f2 = x => (x - 3) * (x - 4); |
|||
Func<double, double> df2 = x => 2 * x - 7; |
|||
Assert.AreEqual(0, f2(NewtonRaphson.FindRoot(f2, df2, -5, 5, 1e-14, 100))); |
|||
Assert.AreEqual(3, NewtonRaphson.FindRoot(f2, df2, -5, 3.5, 1e-14, 100)); |
|||
Assert.AreEqual(4, NewtonRaphson.FindRoot(f2, df2, 3.2, 5, 1e-14, 100)); |
|||
Assert.AreEqual(3, NewtonRaphson.FindRoot(f2, df2, 2.1, 3.9, 0.001, 50), 0.001); |
|||
Assert.AreEqual(3, NewtonRaphson.FindRoot(f2, df2, 2.1, 3.4, 0.001, 50), 0.001); |
|||
} |
|||
|
|||
[Test] |
|||
public void LocalMinima() |
|||
{ |
|||
Func<double, double> f1 = x => x * x * x - 2 * x + 2; |
|||
Func<double, double> df1 = x => 3 * x * x - 2; |
|||
Assert.AreEqual(0, f1(NewtonRaphson.FindRoot(f1, df1, -5, 5, 1e-14, 100))); |
|||
Assert.AreEqual(0, f1(NewtonRaphson.FindRoot(f1, df1, -2, 4, 1e-14, 100))); |
|||
} |
|||
|
|||
[Test] |
|||
public void NoRoot() |
|||
{ |
|||
Func<double, double> f1 = x => x * x + 4; |
|||
Func<double, double> df1 = x => 2 * x; |
|||
Assert.Throws<NonConvergenceException>(() => NewtonRaphson.FindRoot(f1, df1, -5, 5, 1e-14, 50)); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue