dashzt

LAB 4 Algo

Apr 28th, 2017
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.76 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. //y = x^2
  12.  
  13. static float getFunc(float x)
  14. {
  15. return x * x;
  16. }
  17.  
  18.  
  19.  
  20.  
  21. static void Main(string[] args)
  22. {
  23.  
  24. bool inputFlag = true;
  25. float a = 0, b = 0;
  26.  
  27. Console.WriteLine("Vvedite a");
  28. while (inputFlag)
  29. {
  30. if (!float.TryParse(Console.ReadLine(), out a))
  31. {
  32. Console.WriteLine("Error");
  33. inputFlag = true;
  34. }
  35. else
  36. inputFlag = false;
  37. }
  38. inputFlag = true;
  39. Console.WriteLine("Vvedite b");
  40.  
  41. while (inputFlag)
  42. {
  43. if (!float.TryParse(Console.ReadLine(), out b))
  44. {
  45. Console.WriteLine("Error");
  46. inputFlag = true;
  47. }
  48. else
  49. inputFlag = false;
  50. }
  51.  
  52. Console.WriteLine("Интегралы от {0} до {1}:", a, b);
  53. Console.WriteLine("Метод прямоугольников: {0}", rectangleMethod(a, b));
  54. Console.WriteLine("Метод Монте-Карло: {0}", monteCarloMethod(a, b));
  55. Console.WriteLine("Метод трапеций: {0}", trapeziumMethod(a, b));
  56. Console.WriteLine("Метод Симпсона: {0}", simpsonMethod(a, b));
  57.  
  58.  
  59. }
  60.  
  61. static float rectangleMethod(float a, float b)
  62. {
  63. float integral = 0;
  64. float step = 0.005f;
  65. for (float i = a; i <= b; i += step)
  66. {
  67. integral += getFunc(i);
  68. }
  69. integral *= step;
  70. return integral;
  71. }
  72.  
  73. static float monteCarloMethod(float a, float b)
  74. {
  75.  
  76. int isIn = 0;
  77. float maxYValue = getFunc(b), minYValue = getFunc(a);
  78.  
  79. Random random = new Random(DateTime.Now.Millisecond);
  80.  
  81.  
  82. for (int i = 0; i < 300000; i++)
  83. {
  84. float pointX = GetRandomFloat(a, b, random);
  85. float pointY = GetRandomFloat(minYValue, maxYValue, random);
  86. if (getFunc(pointX) > pointY)
  87. isIn++;
  88. }
  89. return (maxYValue * b * isIn)/300000;
  90. }
  91.  
  92. static float trapeziumMethod(float a, float b)
  93. {
  94. float integral = 0;
  95. float step = 0.005f;
  96. for (float i = a; i <= b; i += step)
  97. {
  98. integral += 2 * getFunc(i);
  99. }
  100. integral = (integral + getFunc(a) + getFunc(b)) * step/2;
  101. return integral;
  102. }
  103.  
  104. static float simpsonMethod(float a, float b)
  105. {
  106. int n = 1;
  107. float step = 0.005f;
  108.  
  109. float integral = 0;
  110. float integral1 = 0, integral2 = 0;
  111. for (float i = a; i <= b; i += step)
  112. {
  113. if (n % 2 == 0)
  114. {
  115. integral1 += getFunc(i);
  116. }
  117. else
  118. {
  119. integral2 += getFunc(i);
  120. }
  121. n++;
  122. }
  123. integral = (integral + getFunc(a) + getFunc(b) + 4 * integral2 + 2*integral1) * step / 3;
  124. return integral;
  125. }
  126.  
  127. public static float GetRandomFloat(float minimum, float maximum, Random random)
  128. {
  129. return (float) random.NextDouble() * (maximum - minimum) + minimum;
  130. }
  131.  
  132. }
  133. }
Add Comment
Please, Sign In to add comment