module Roots.Simple
where
-import qualified Roots.Fast as F
+import Data.List (find)
+import qualified Roots.Fast as F
-- | Does the (continuous) function @f@ have a root on the interval
-- [a,b]? If f(a) <] 0 and f(b) ]> 0, we know that there's a root in
-> Maybe a
bisect f a b epsilon =
F.bisect f a b epsilon Nothing Nothing
+
+
+
+-- | The sequence x_{n} of values obtained by applying Newton's method
+-- on the function @f@ and initial guess @x0@.
+newton_iterations :: (Fractional a, Ord a)
+ => (a -> a) -- ^ The function @f@ whose root we seek
+ -> (a -> a) -- ^ The derivative of @f@
+ -> a -- ^ Initial guess, x-naught
+ -> [a]
+newton_iterations f f' x0 =
+ iterate next x0
+ where
+ next xn =
+ xn - ( (f xn) / (f' xn) )
+
+
+
+newtons_method :: (Fractional a, Ord a)
+ => (a -> a) -- ^ The function @f@ whose root we seek
+ -> (a -> a) -- ^ The derivative of @f@
+ -> a -- ^ The tolerance epsilon
+ -> a -- ^ Initial guess, x-naught
+ -> Maybe a
+newtons_method f f' epsilon x0
+ = find (\x -> abs (f x) < epsilon) x_n
+ where
+ x_n = newton_iterations f f' x0