changeset 16:198994b8f05d

Add newton.hs
author Jordi Gutiérrez Hermoso <jordigh@octave.org>
date Sun, 17 Aug 2014 11:22:55 -0400
parents f55bfa72951f
children 2b5230f69ebf
files haskell/newton.hs
diffstat 1 files changed, 29 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
new file mode 100644
--- /dev/null
+++ b/haskell/newton.hs
@@ -0,0 +1,29 @@
+module Main where
+
+import Data.Complex
+
+absc :: (RealFloat a) => Complex a -> a
+absc z = sqrt $ (realPart z)^2 + (imagPart z)^2
+
+type ComplexFunction a = Complex a -> Complex a
+
+newton :: (RealFloat a) => ComplexFunction a 
+          -> ComplexFunction a
+          -> a 
+          -> Int 
+          -> Complex a 
+          -> Int
+newton f f' tol iter xn
+  | absc (1 - xn_1/xn) < tol = iter
+  | iter > 200 = iter                             
+  | otherwise = newton f f' tol (iter+1) xn_1
+  where xn_1 = xn - (f xn)/(f' xn)
+        
+f z = z^10 - 1
+f' z = 10*z^9
+
+fractal = [map newtoniter [x :+ y | x <- l] | y <- l]
+  where newtoniter = newton f f' 1e-14 0
+        l = [-1, -0.99.. 1]
+
+main = putStrLn $ show $ newton f f' 1e-15 0 1
\ No newline at end of file