Created
October 27, 2011 14:43
-
-
Save madebyjeffrey/1319734 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env runhaskell | |
ε = 1.0e-7 | |
-- sqrt(7) | |
f x = x * x - 7 | |
-- secant :: (a -> a) -> a -> a -> a | |
secant f x = secant' f xLast x | |
where xLast = x - 0.01 | |
secant' f xLast x | abs (xNext - x) <= ε = xNext | |
| otherwise = secant' f x xNext | |
xNext = x - (f x) * (xLast - x) / ((f xLast) - (f x)) | |
--if abs (xNext - x) <= ε | |
-- then xNext | |
-- else secant f x xNext | |
-- where | |
main = do | |
let fx = secant f 2.0 | |
putStrLn ("Roots of sqrt(7) are " ++ (show fx)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment