Browse Source

Added F# function for Broyden method with Jacobian step size and a unit test for that.

build
Aappo Pulkkinen 9 years ago
parent
commit
4d26a5974a
  1. 5
      src/FSharp/FindRoots.fs
  2. 5
      src/FSharpUnitTests/FindRootsTests.fs

5
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) =

5
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
[<Test>]
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|])
[<Test>]
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

Loading…
Cancel
Save