Advertisement
dashzt

LAB 4 Algo v 1.1 (GOVNO)

Apr 28th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.14 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace zad1
  7. {
  8. class Program
  9. {
  10.  
  11.  
  12.  
  13. static float getFunc(float x)
  14. {
  15. //Set func there
  16. return x * x;
  17. }
  18.  
  19.  
  20.  
  21.  
  22. static void Main(string[] args)
  23. {
  24.  
  25. bool inputFlag = true;
  26. float a = 0, b = 0;
  27.  
  28. Console.WriteLine("Vvedite a");
  29. while (inputFlag)
  30. {
  31. if (!float.TryParse(Console.ReadLine(), out a))
  32. {
  33. Console.WriteLine("Error");
  34. inputFlag = true;
  35. }
  36. else
  37. inputFlag = false;
  38. }
  39. inputFlag = true;
  40. Console.WriteLine("Vvedite b");
  41.  
  42. while (inputFlag)
  43. {
  44. if (!float.TryParse(Console.ReadLine(), out b))
  45. {
  46. Console.WriteLine("Error");
  47. inputFlag = true;
  48. }
  49. else
  50. inputFlag = false;
  51. }
  52.  
  53. Console.WriteLine("Интегралы от {0} до {1}:", a, b);
  54. Console.WriteLine("Метод прямоугольников: {0}", rectangleMethod(a, b));
  55. Console.WriteLine("Метод Монте-Карло: {0}", monteCarloMethod(a, b));
  56. Console.WriteLine("Метод трапеций: {0}", trapeziumMethod(a, b));
  57. Console.WriteLine("Метод Симпсона: {0}", simpsonMethod(a, b));
  58. MonteCarlosMethod();
  59.  
  60. }
  61.  
  62. static float rectangleMethod(float a, float b)
  63. {
  64. float integral = 0;
  65. float step = 0.005f;
  66. for (float i = a; i <= b; i += step)
  67. {
  68. integral += getFunc(i);
  69. }
  70. integral *= step;
  71. return integral;
  72. }
  73.  
  74. static float monteCarloMethod(float a, float b)
  75. {
  76.  
  77. int isIn = 0;
  78. float maxYValue = getFunc(b), minYValue = getFunc(a);
  79. int n = 10000;
  80. Random random = new Random(DateTime.Now.Millisecond);
  81. Random rnd = new Random(DateTime.Now.Hour);
  82.  
  83. for (int i = 0; i < n; i++)
  84. {
  85. float pointX = GetRandomFloat(a, b, random);
  86. float pointY = GetRandomFloat(minYValue, maxYValue, rnd);
  87. Console.WriteLine("x = {0}, y = {1}", pointX, pointY);
  88. if (getFunc(pointX) > pointY)
  89. isIn++;
  90. }
  91. return (maxYValue * b * isIn)/n;
  92. }
  93.  
  94. static float trapeziumMethod(float a, float b)
  95. {
  96. float integral = 0;
  97. float step = 0.005f;
  98. for (float i = a; i <= b; i += step)
  99. {
  100. integral += 2 * getFunc(i);
  101. }
  102. integral = (integral + getFunc(a) + getFunc(b)) * step/2;
  103. return integral;
  104. }
  105.  
  106. static float simpsonMethod(float a, float b)
  107. {
  108. int n = 1;
  109. float step = 0.005f;
  110.  
  111. float integral = 0;
  112. float integral1 = 0, integral2 = 0;
  113. for (float i = a; i <= b; i += step)
  114. {
  115. if (!(n % 2 == 0))
  116. {
  117. integral1 += getFunc(i);
  118. }
  119. else
  120. {
  121. integral2 += getFunc(i);
  122. }
  123. n++;
  124. }
  125. integral = (integral + getFunc(a) + getFunc(b) + 4 * integral2 + 2*integral1) * step / 3;
  126. return integral;
  127. }
  128.  
  129. public static float GetRandomFloat(float minimum, float maximum, Random random)
  130. {
  131. return (float) random.NextDouble() * (maximum - minimum) + minimum;
  132. }
  133.  
  134. public static void MonteCarlosMethod()
  135. {
  136. double leftEdge, rightEdge, step;
  137. Console.WriteLine("enter left edge");
  138. leftEdge = double.Parse(Console.ReadLine());
  139. Console.WriteLine("enter right edge");
  140. rightEdge = double.Parse(Console.ReadLine());
  141. Console.Write("enter number of dots to count on\nn: ");
  142. int n = int.Parse(Console.ReadLine());
  143. double height = Math.Pow(rightEdge, 2);
  144. Random xRandom = new Random(DateTime.Now.Millisecond);
  145. Random yRandom = new Random(DateTime.Now.Hour);
  146. double xTemp, yTemp;
  147. double isInFunction = 0;
  148. for (int i = 0; i < n; i++)
  149. {
  150. xTemp = xRandom.NextDouble() * (rightEdge - leftEdge) + leftEdge;
  151. yTemp = yRandom.NextDouble() * height;
  152. if (yTemp < Math.Pow(xTemp, 2))
  153. isInFunction++;
  154. }
  155. double result = ((rightEdge - leftEdge) * height) * (isInFunction / n);
  156. Console.WriteLine("result: " + result);
  157. Console.WriteLine("press any key to continue");
  158. Console.ReadKey();
  159.  
  160.  
  161. }
  162.  
  163. }
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement