Browse Source

RootFinding: update tests

pull/121/head
Christoph Ruegg 13 years ago
parent
commit
fe870ce899
  1. 6
      src/UnitTests/RootFindingTests/BisectionTest.cs
  2. 14
      src/UnitTests/RootFindingTests/BrentTest.cs

6
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<double, double>((x) => (x - 3)*(x - 4));
var f1 = new Func<double, double>(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<double, double>((x) => (x - 3)*(x - 4));
var f2 = new Func<double, double>(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));

14
src/UnitTests/RootFindingTests/BrentTest.cs

@ -28,6 +28,7 @@
// OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
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<double, double> 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<double, double> 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));
}
}
}

Loading…
Cancel
Save