loulett

tailor1

Apr 8th, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 1.01 KB | None | 0 0
  1. type Result = float * int
  2. let delta = 1e-10
  3.  
  4. // *** Первая часть
  5.  
  6. let fTailor x : float = cosh x // функция, которую раскладываем
  7. let n, a, b = 20., 0., 1. // интервал
  8.  
  9. let rec factorial n =
  10.     match n with
  11.     | 0 -> 1
  12.     | n -> n * factorial (n-1)
  13.  
  14. let tailorone' x n = x ** (float) n /  (float)(factorial n)
  15.  
  16. tailorone' (1., 0) 2
  17.  
  18. let tailor x : Result = (x,0)
  19.  
  20. let tailorA x : Result =
  21.     let rec tailorA' (y : Result) n one =
  22.        if one < delta then y else
  23.            let ans = tailorone' x n
  24.             tailorA' ((fst y) + ans, (snd y) + n + 1) (n + 2) ans
  25.    tailorA' (x, 0) 3 x
  26.  
  27. tailorA 0.5
  28. abs(0. - (fTailor 0.)) < delta
  29.  
  30.  
  31. let printTailor () =
  32.     [a .. (b-a)/n .. b]
  33.     |> List.map (fun x -> let (firstRes, firstCou), (secondRes, secondCou) = tailor x, tailorA x in (x, firstRes, firstCou, secondRes, secondCou, fTailor x))
  34.     |> List.iter (fun (a,b,c,d,e,f) -> printf "%f\t%f\t%d\t%f\t%d\t%f\n" a b c d e f )
  35.  
  36. printTailor ()
Advertisement
Add Comment
Please, Sign In to add comment