-
-
Save bheklilr/7fabf893b51a955986e4 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
module Main where | |
derivation :: (Fractional a) => (a -> a) -> a -> a | |
derivation f = \ x -> ( f (x + dx) - f x ) / dx where dx = 0.1 | |
evalA :: (Fractional a) => Int -> (a -> a) -> a -> a | |
evalA 0 _ = id | |
evalA k f = \x -> derivation (evalA (k-1) f) x / derivation f x | |
evalA2 :: (Fractional a) => Int -> (a -> a) -> a -> a | |
evalA2 k f = iterate (\prev x -> derivation prev x / derivation f x) id !! k | |
inverseFun :: (Ord a, Fractional a) => (a -> a) -> a -> a | |
inverseFun f x = iter 0 1.0 0.0 | |
where | |
x0 = 3.0 | |
eps = 0.001 | |
iter k prev sum' = | |
let elemB = prev * (x - f x0) / fromIntegral (if k == 0 then 1 :: Int else k) | |
newItem = evalA k f x0 * elemB | |
in if abs newItem < eps | |
then sum' | |
else iter (k + 1) elemB (sum' + newItem) | |
inverseFun2 :: (Ord a, Fractional a) => (a -> a) -> a -> a | |
inverseFun2 f x = iter 0 1.0 0.0 | |
where | |
x0 = 3.0 | |
eps = 0.001 | |
iter k prev sum' = | |
let elemB = prev * (x - f x0) / fromIntegral (if k == 0 then 1 :: Int else k) | |
newItem = evalA2 k f x0 * elemB | |
in if abs newItem < eps | |
then sum' | |
else iter (k + 1) elemB (sum' + newItem) | |
f1 :: Fractional a => a -> a | |
f1 x = 1.0 * x * x | |
main :: IO () | |
main = print (inverseFun f1 2.5 :: Double) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment