diff --git a/src/UnitTests/RootFindingTests/BisectionTest.cs b/src/UnitTests/RootFindingTests/BisectionTest.cs index 11d910d8..7f2cf8db 100644 --- a/src/UnitTests/RootFindingTests/BisectionTest.cs +++ b/src/UnitTests/RootFindingTests/BisectionTest.cs @@ -38,15 +38,15 @@ namespace MathNet.Numerics.UnitTests.RootFindingTests internal class BisectionTest { [Test] - public void FindRoot_Works() + public void MultipleRoots() { var algorithm = new Bisection(0.001, 0.001); - var f1 = new Func((x) => (x - 3)*(x - 4)); + var f1 = new Func(x => (x - 3)*(x - 4)); double r1 = algorithm.FindRoot(f1, 2.1, 3.9); Assert.That(Math.Abs(f1(r1)), Is.LessThan(0.001)); Assert.That(Math.Abs(r1 - 3.0), Is.LessThan(0.001)); - var f2 = new Func((x) => (x - 3)*(x - 4)); + var f2 = new Func(x => (x - 3)*(x - 4)); double r2 = algorithm.FindRoot(f1, 2.1, 3.4); Assert.That(Math.Abs(f2(r2)), Is.LessThan(0.001)); Assert.That(Math.Abs(r2 - 3.0), Is.LessThan(0.001)); diff --git a/src/UnitTests/RootFindingTests/BrentTest.cs b/src/UnitTests/RootFindingTests/BrentTest.cs index d5bc1d17..d150a9b1 100644 --- a/src/UnitTests/RootFindingTests/BrentTest.cs +++ b/src/UnitTests/RootFindingTests/BrentTest.cs @@ -28,6 +28,7 @@ // OTHER DEALINGS IN THE SOFTWARE. // +using System; using MathNet.Numerics.RootFinding.Algorithms; using NUnit.Framework; @@ -39,8 +40,17 @@ namespace MathNet.Numerics.UnitTests.RootFindingTests [Test] public void MultipleRoots() { - double root = Brent.FindRoot(x => x*x - 4, -5, 5, 1e-14, 100); - Assert.AreEqual(0, root*root - 4); + // Roots at -2, 2 + Func f1 = x => x*x - 4; + Assert.AreEqual(0, f1(Brent.FindRoot(f1, -5, 5, 1e-14, 100))); + Assert.AreEqual(-2, Brent.FindRoot(f1, -5, -1, 1e-14, 100)); + Assert.AreEqual(2, Brent.FindRoot(f1, 1, 4, 1e-14, 100)); + + // Roots at 3, 4 + Func f2 = x => (x - 3)*(x - 4); + Assert.AreEqual(0, f2(Brent.FindRoot(f2, -5, 5, 1e-14, 100))); + Assert.AreEqual(3, Brent.FindRoot(f2, -5, 3.5, 1e-14, 100)); + Assert.AreEqual(4, Brent.FindRoot(f2, 3.2, 5, 1e-14, 100)); } } }