diff --git a/src/FSharpPortableUnitTests/FSharpPortableUnitTests.fsproj b/src/FSharpPortableUnitTests/FSharpPortableUnitTests.fsproj index e19772c8..95ed6be1 100644 --- a/src/FSharpPortableUnitTests/FSharpPortableUnitTests.fsproj +++ b/src/FSharpPortableUnitTests/FSharpPortableUnitTests.fsproj @@ -95,6 +95,9 @@ FitTests.fs + + FindRootsTests.fs + diff --git a/src/FSharpUnitTests/FSharpUnitTests.fsproj b/src/FSharpUnitTests/FSharpUnitTests.fsproj index ce9162a6..dddc6c1c 100644 --- a/src/FSharpUnitTests/FSharpUnitTests.fsproj +++ b/src/FSharpUnitTests/FSharpUnitTests.fsproj @@ -80,6 +80,7 @@ + diff --git a/src/FSharpUnitTests/FindRootsTests.fs b/src/FSharpUnitTests/FindRootsTests.fs new file mode 100644 index 00000000..d336a52b --- /dev/null +++ b/src/FSharpUnitTests/FindRootsTests.fs @@ -0,0 +1,43 @@ +namespace MathNet.Numerics.Tests + +open System +open MathNet.Numerics +open NUnit.Framework +open FsUnit + +module FindRootsTests = + + let f x = (x - 3.0)*(x - 4.0) + let df x = 2.0*x - 7.0 + + [] + let ``Bisection should find both roots of (x - 3) * (x - 4)``() = + f |> FindRoots.bisection 100 1e-14 -5.0 3.5 |> should equal (Some 3.0) + f |> FindRoots.bisection 100 1e-14 3.2 5.0 |> should equal (Some 4.0) + + [] + let ``Brent should find both roots of (x - 3) * (x - 4)``() = + f |> FindRoots.brent 100 1e-14 -5.0 3.5 |> should equal (Some 3.0) + f |> FindRoots.brent 100 1e-14 3.2 5.0 |> should equal (Some 4.0) + + [] + let ``Newton-Raphson should find both roots of (x - 3) * (x - 4)``() = + (f, df) ||> FindRoots.newtonRaphson 100 1e-14 -5.0 3.5 |> should equal (Some 3.0) + (f, df) ||> FindRoots.newtonRaphson 100 1e-14 3.2 5.0 |> should equal (Some 4.0) + + [] + let ``Newton-Raphson by Guess should find both roots of (x - 3) * (x - 4)``() = + (f, df) ||> FindRoots.newtonRaphsonGuess 100 1e-14 2.8 |> should equal (Some 3.0) + (f, df) ||> FindRoots.newtonRaphsonGuess 100 1e-14 3.7 |> should equal (Some 4.0) + + [] + let ``Robust Newton-Raphson should find both roots of (x - 3) * (x - 4)``() = + (f, df) ||> FindRoots.newtonRaphsonRobust 100 20 1e-14 -5.0 3.5 |> should equal (Some 3.0) + (f, df) ||> FindRoots.newtonRaphsonRobust 100 20 1e-14 3.2 5.0 |> should equal (Some 4.0) + + [] + let ``Simple method should find both roots of (x - 3) * (x - 4)``() = + f |> FindRoots.ofFunction -5.0 3.5 |> Option.get |> should (equalWithin 1e-8) 3.0 + f |> FindRoots.ofFunction 3.2 5.0 |> Option.get |> should (equalWithin 1e-8) 4.0 + (f, df) ||> FindRoots.ofFunctionAndDerivative -5.0 3.5 |> Option.get |> should (equalWithin 1e-8) 3.0 + (f, df) ||> FindRoots.ofFunctionAndDerivative 3.2 5.0 |> Option.get |> should (equalWithin 1e-8) 4.0