-- | Solve \(f(x) = 0\) for \(x\).
module Numerics.FindRoot (newtonStep) where

-- | Single step for Newton’s method for root finding.
--
-- https://en.wikipedia.org/wiki/Newton%27s_method
--
-- \[
-- t_{n+1} = t_n - \frac{f(t_n)}{f'(t_n)}
-- \]
--
-- Useful as a parameter to 'Numerics.ConvergentRecursion.recurseUntilPrecision'.
newtonStep
    :: Double             -- ^ \(h\) to calculate the derivative, as in \(\frac{f(x+h)-f(x)}h\)
    -> (Double -> Double) -- ^ \( f(t_n)  \)
    -> Double             -- ^ \( t_n     \)
    -> Double             -- ^ \( t_{n+1} \)
newtonStep :: Double -> (Double -> Double) -> Double -> Double
newtonStep Double
h Double -> Double
f Double
t = Double
t Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double -> Double
f Double
t Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Double -> (Double -> Double) -> Double -> Double
d Double
h Double -> Double
f Double
t

-- | Derivative.
d :: Double               -- ^ \(h\) as in \(\frac{f(x+h)-f(x)}h\)
    -> (Double -> Double) -- ^ \(f\)
    -> Double             -- ^ \(x\)
    -> Double             -- ^ \(\partial_xf(x)\)
d :: Double -> (Double -> Double) -> Double -> Double
d Double
h Double -> Double
f Double
t = (Double -> Double
f (Double
tDouble -> Double -> Double
forall a. Num a => a -> a -> a
+Double
h) Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double -> Double
f Double
t) Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Double
h