Created
September 18, 2016 18:53
-
-
Save punksta/d3c2c1cd56b301e527f6d6f8a272ac48 to your computer and use it in GitHub Desktop.
integral method on haskell
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 | |
-- Реализуйте функцию, находящую значение определённого интеграла от заданной функции f на заданном интервале [a,b] | |
-- | |
-- методом трапеций. (Используйте равномерную сетку; достаточно 1000 элементарных отрезков.) | |
-- | |
-- integration :: (Double -> Double) -> Double -> Double -> Double | |
-- integration f a b = undefined | |
-- | |
-- GHCi> integration sin pi 0 | |
-- -2.0 | |
-- | |
-- Результат может отличаться от -2.0, но не более чем на 1e-4. | |
integration :: (Double -> Double) -> Double -> Double -> Double | |
integration f a b | a == b = 0.0 | |
| a > b = - integration f b a | |
| a < b = (*) h ((f begin) + (f end) + (2.0 * (fsum 0.0 0))) / 2.0 | |
where { | |
begin = min a b; | |
end = max a b; | |
x_first = begin + h; | |
eps = 1e-4; | |
h = (end - begin) / 1000.0; | |
fsum acc n | n == 999 = acc | |
| otherwise = fsum (acc + (f (x_first + n * h))) (n + 1) | |
} | |
main = print (integration sin 0 pi) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment