module Numeric.Tools.Equation (
solveBisection
) where
import Numeric.IEEE (epsilon)
solveBisection :: Double
-> (Double,Double)
-> (Double -> Double)
-> Maybe Double
solveBisection eps (a,b) f
| a >= b = Nothing
| fa * fb > 0 = Nothing
| otherwise = Just $ bisectionWorker (abs eps) f a b fa fb
where
fa = f a
fb = f b
bisectionWorker :: Double -> (Double -> Double) -> Double -> Double -> Double -> Double -> Double
bisectionWorker eps f a b fa fb
| (b a) <= eps = c
| (b a) / b <= epsilon = c
| fa * fc < 0 = bisectionWorker eps f a c fa fc
| otherwise = bisectionWorker eps f c b fc fb
where
c = 0.5 * (a + b)
fc = f c