You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
38 lines
877 B
Haskell
38 lines
877 B
Haskell
module Main where
|
|
|
|
import Graphics.EasyPlot
|
|
|
|
-- Шаг сетки
|
|
delta = 0.001
|
|
|
|
-- Функция
|
|
u = sin
|
|
|
|
-- Точка
|
|
t = 0.5
|
|
|
|
-- Абсолютная погрешность
|
|
err = 10 ** (-3)
|
|
|
|
main :: IO ()
|
|
main = do
|
|
let res = map (\(x,y) -> (fromIntegral x,y)) $ calc u t
|
|
plot X11 $ Data2D [Style Lines] [] res
|
|
print res
|
|
|
|
|
|
calc u t = calc' 0 [] u t where
|
|
calc' n stat u t | err' < err = (n, err') : stat
|
|
| otherwise = (n, err') : calc' (n + 1) stat u t
|
|
where err' = abs (u t - maclaurin u t n)
|
|
|
|
factorial n | n < 2 = 1
|
|
| otherwise = n * factorial (n - 1)
|
|
|
|
diff u 0 h x = u x
|
|
diff u n h x = (u' (x + h) - u' (x - h)) / (2 * h) where
|
|
u' = diff u (n - 1) h
|
|
|
|
maclaurin u x 0 = u 0
|
|
maclaurin u x n = (x ** (fromIntegral n)) * (diff u n delta 0) / (fromIntegral $ factorial n) + maclaurin u x (n - 1)
|