X-Git-Url: http://gitweb.michael.orlitzky.com/?a=blobdiff_plain;f=src%2FRoots%2FSimple.hs;h=101b7e24c65610b381b9ff75d56137bc6f9b519f;hb=587fda23c4c1e3f2f1a46063a9e6766d002ea356;hp=64124a876bfc24d3ff55b15eca29cc9d76dfe613;hpb=61dcb661ee35ff693a9d64d4d21a65791a480a52;p=numerical-analysis.git diff --git a/src/Roots/Simple.hs b/src/Roots/Simple.hs index 64124a8..101b7e2 100644 --- a/src/Roots/Simple.hs +++ b/src/Roots/Simple.hs @@ -9,8 +9,9 @@ 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 @@ -71,3 +72,31 @@ bisect :: (Fractional a, Ord a, Num b, Ord b) -> 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