forked from caiorss/Functional-Programming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bisection_state.hs
32 lines (25 loc) · 929 Bytes
/
bisection_state.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
{-
From: http://en.wikipedia.org/wiki/Bisection_method
-}
import OldState
bisecStateFactory f = do
(a, b) <- get
let c = (a + b) / 2.0
-- If sign(f(c)) = sign(f(a)) else b ← c
if (f c) * (f a) >= 0
then put (c, b) -- then a ← c
else put (a, c) -- else b ← c
return c
findRoot :: Double -> Double -> (Double -> Double) -> [Double] -> Maybe Double
findRoot eps itmax f serie =
case (itmax, serie) of
(_, []) -> Nothing
(0, (x:xs)) -> if abs(f x) < eps then Just x else Nothing
(i, (x:xs)) -> if abs(f x) < eps
then Just x
else
findRoot eps (i - 1) f xs
bisecSolver eps itmax f x0 x1 =
findRoot eps itmax f (evalStateLoop (bisecStateFactory f) (x0, x1))
f :: Double -> Double
f x = x ** 3.0 - x - 2.0