Skip to content

Instantly share code, notes, and snippets.

@madebyjeffrey
Created October 27, 2011 14:43
Show Gist options
  • Save madebyjeffrey/1319734 to your computer and use it in GitHub Desktop.
Save madebyjeffrey/1319734 to your computer and use it in GitHub Desktop.
#!/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