Advertisement
Guest User

Untitled

a guest
May 30th, 2015
309
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 3.27 KB | None | 0 0
  1. module lab1
  2.  
  3. open System
  4. open System.Net
  5. open System.IO
  6. open System.Collections.Specialized
  7.  
  8. // почтовый адрес
  9. let email = "annafedyushkina@gmail.com"
  10. // общий тип для возвращаемых вашими функциями значений, где первая часть кортежа это само значение функции, вторая - кол-во операций
  11. type Result = float * int
  12. let delta = 1e-10
  13.  
  14. // *** Первая часть
  15.  
  16. let fTailor x : float = (1 + x ** 2) / 2 * atan x - x / 2 // функция, которую раскладываем
  17. let n, a, b = 20., 0.1, 0.6 // интервал
  18.  
  19. let tailor x : Result =
  20.     let rec tailor_function x result i =
  21.         let current = ((-1)**(float(n) + 1))*(x**(2*(float(n)) + 1))/(4*((float(n))**2) - 1)
  22.         if abs (current) < delta
  23.             then (result, i)
  24.         else
  25.             tailor_function x (result + current) (i + 1)
  26.     tailor_function x 0. 1
  27.    
  28. let tailorA x : Result =
  29.     let rec tailor_function x result prev i =
  30.         let current = prev*(-1)*(x**2)*(4*((i-1)**2) - 1)/(4*(n**2 - 1)
  31.         if abs (current) < delta
  32.             then (result, i)
  33.         else
  34.             tailor_function x (result + current) current (i + 1)
  35.     tailor_function x 0. x 1
  36.            
  37.  
  38. let printTailor () =
  39.     [a .. (b-a)/n .. b]
  40.     |> List.map (fun x -> let (firstRes, firstCou), (secondRes, secondCou) = tailor x, tailorA x in (x, firstRes, firstCou, secondRes, secondCou, fTailor x))
  41.     |> 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 )
  42.  
  43. // *** Вторая часть
  44.  
  45. let fSolve = [ (fun x -> x + (x ** (1./2.)) + (x ** (1./3.)) - 2.5); // функции, решения которых ищем
  46.                 (fun x -> x - 1. / (3. + sin(3.6*x)));
  47.                 fun x -> 0.1*(x**2) - x*(log(x))]
  48.  
  49. let diap = [(0.4, 1.); (0., 0.85); (1., 2.)]
  50.  
  51. let iter f a b : Result = (42., 0)
  52.     let touch = (b-a) / (f b - f a)
  53.     let rec iterA i f x =
  54.         let x1 = x - touch * (f x)
  55.         if abs (x1 - x) < delta
  56.             then (x1, i)
  57.         else
  58.             iterA (i + 1) f x1
  59.     iterA 0 f ((a + b) / 2.)
  60.    
  61. let newton f a b : Result =
  62.     let rec newtonA i f x xprev =
  63.         if abs (x - xprev) < delta then
  64.             (x, i)
  65.         else
  66.             newtonA (i + 1) f (x - (f x) * (x - xprev) / ((f x) - (f xprev))) x
  67.     newtonA 0 f ((a + b) / 2.) a
  68.  
  69. let dichotomy =
  70.     let rec dichotomyA i f a b : Result =
  71.         let t = (a + b) / 2.
  72.         if b - a < delta
  73.             then (t, i)
  74.         else
  75.             if (f t) * (f a) < 0.
  76.                 then dichotomyA (i+1) f a t
  77.             else dichotomyA (i+1) f t b
  78.     dichotomyA 0
  79.  
  80. let printSolve () =
  81.     [iter; newton; dichotomy]
  82.     |> List.map (fun f -> f fSolve a b)
  83.     |> List.iter (fun (res, cou) -> printf "%f\t%d\n" res cou)
  84.  
  85. let main () =
  86.   let values = new NameValueCollection()
  87.   values.Add("email", email)
  88.   values.Add("content", File.ReadAllText(__SOURCE_DIRECTORY__ + @"/" + __SOURCE_FILE__))
  89.  
  90.   let client = new WebClient()
  91.   let response = client.UploadValues(new Uri("http://mipt.eu01.aws.af.cm/lab1"), values)
  92.   let responseString = Text.Encoding.Default.GetString(response)
  93.  
  94.   printf "%A\n" responseString
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement