Skip to content

Instantly share code, notes, and snippets.

@msakai
Last active August 8, 2022 12:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save msakai/7cdaac4fbdd5cb56f10d60c35d114c90 to your computer and use it in GitHub Desktop.
Save msakai/7cdaac4fbdd5cb56f10d60c35d114c90 to your computer and use it in GitHub Desktop.
-- |
--
-- * [Secant method](https://en.wikipedia.org/wiki/Secant_method)
--
-- * [割線法](https://ja.wikipedia.org/wiki/%E5%89%B2%E7%B7%9A%E6%B3%95)
module SecantMethod where
secantMethod :: (Eq a, Fractional a) => (a -> a) -> a -> a -> [a]
secantMethod f x0 x1 = map fst xs
where
xs = (x0, f x0) : (x1, f x1) : zipWith step xs (tail xs)
step (x_prev, fx_prev) (x_curr, fx_curr)
| fx_curr == fx_prev = (x_curr, fx_curr) -- not necessary converged to a root
| otherwise = (x_next, fx_next)
where
x_next = x_curr - fx_curr * (x_curr - x_prev) / (fx_curr - fx_prev)
fx_next = f x_next
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment