I'm learning F#. I wrote a function to minimise a function with a single minimum and no maximums. At the bottom I test it on Math.Cos.
What do you think? I've wrote it pretty much as I would in an imperative language. Could it be done more functionally?
/// <summary>Assuming f is a continuous function with a single minimum and
/// no maximums in (lower,upper), find the minimum point.</summary>
let rec minimise f (lower:float) (upper:float) =
let lmid = (2.0*lower+upper)/3.0
let umid = (lower+2.0*upper)/3.0
if lmid = lower && umid = upper then
Seq.minBy f [lower; upper]
else if f lmid < f umid then
minimise f lower umid
else
minimise f lmid upper
let solution = minimise Math.Cos 0.0 6.0
printf "solution: %g" solution
printf "absolute error: %g" (solution - Math.PI)