From 4d26a5974af50e4a756f6c7a2b7029c3dc97be12 Mon Sep 17 00:00:00 2001 From: Aappo Pulkkinen Date: Wed, 3 Jan 2018 17:25:07 +0200 Subject: [PATCH] Added F# function for Broyden method with Jacobian step size and a unit test for that. --- src/FSharp/FindRoots.fs | 5 +++++ src/FSharpUnitTests/FindRootsTests.fs | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/src/FSharp/FindRoots.fs b/src/FSharp/FindRoots.fs index fbcacd20..41966dca 100644 --- a/src/FSharp/FindRoots.fs +++ b/src/FSharp/FindRoots.fs @@ -70,6 +70,11 @@ module FindRoots = | true, root -> Some root | false, _ -> None + let broydenWithJacobianStep jacobianStepSize maxIterations accuracy guess (f:float[]->float[]) = + match Broyden.TryFindRootWithJacobianStep(tobcl f, guess, accuracy, maxIterations, jacobianStepSize) with + | true, root -> Some root + | false, _ -> None + // simple usage let ofFunction lowerBound upperBound (f:float->float) = diff --git a/src/FSharpUnitTests/FindRootsTests.fs b/src/FSharpUnitTests/FindRootsTests.fs index d6ac588a..e612b1ca 100644 --- a/src/FSharpUnitTests/FindRootsTests.fs +++ b/src/FSharpUnitTests/FindRootsTests.fs @@ -59,6 +59,11 @@ module FindRootsTests = | Some x -> should (equalWithin 1e-1) [|0.9638680512795; 346.16369814640|] | None -> failwith "The element in array is not equal.") |> ignore + [] + let ``Bryoden with Jacobian step size should find both roots of (x - 3) * (x - 4)``() = + f |> (fun g (x:float[]) -> [|g x.[0]|]) |> FindRoots.broydenWithJacobianStep 1e-6 100 1e-14 [|1.0;|] |> shouldEqual (Some [|3.0|]) + f |> (fun g (x:float[]) -> [|g x.[0]|]) |> FindRoots.broydenWithJacobianStep 1e-6 100 1e-14 [|9.0;|] |> shouldEqual (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