Skip to content

Commit

Permalink
RootFinding: update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cdrnet committed May 3, 2013
1 parent e523675 commit fe870ce
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
6 changes: 3 additions & 3 deletions src/UnitTests/RootFindingTests/BisectionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
14 changes: 12 additions & 2 deletions src/UnitTests/RootFindingTests/BrentTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
// OTHER DEALINGS IN THE SOFTWARE.
// </copyright>

using System;
using MathNet.Numerics.RootFinding.Algorithms;
using NUnit.Framework;

Expand All @@ -39,8 +40,17 @@ public class BrentTest
[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));
}
}
}

0 comments on commit fe870ce

Please sign in to comment.