Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2020
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.96 KB | None | 0 0
  1. #include <iostream>
  2. #include <math.h>
  3.  
  4.  
  5. double functionFirst(double x)
  6. {
  7. return 2.0 * x * x * x - 27.0 * x * x + 48.0 * x + 5.0;
  8. }
  9.  
  10. double functionSecond(double x)
  11. {
  12. return 6.0 * x * x - 54.0 * x + 48.0;
  13. }
  14.  
  15.  
  16. void gridSearch(double x_start, double x_end, long double epsilon, int& counter, double& ekstremum)
  17. {
  18. double x_before = 0;
  19. double x_current = 0;
  20.  
  21. x_before = functionFirst(x_start);
  22. x_current = functionFirst(x_start + epsilon);
  23.  
  24. counter += 2;
  25.  
  26. while (x_start + epsilon * counter < x_end)
  27. {
  28. x_before = x_current;
  29. x_current = functionFirst(x_start + counter * epsilon);
  30.  
  31. counter++;
  32.  
  33. if (x_before < x_current)
  34. {
  35. ekstremum = x_start + counter * epsilon;
  36. break;
  37. }
  38. }
  39.  
  40. }
  41.  
  42.  
  43. double gridSearchRecurrent(double x_start, double x_end, long double epsilon, int& counter, double& ekstremum)
  44. {
  45. int k = 8;
  46.  
  47. double delta = (x_end - x_start) / k;
  48.  
  49. if (delta < epsilon)
  50. {
  51. return (x_start + delta);
  52. }
  53.  
  54. double x_before = functionFirst(x_start);
  55. double x_current = functionFirst(x_start + delta);
  56.  
  57. counter += 2;
  58.  
  59. int check_last;
  60. int check_current = 0;
  61.  
  62. if (x_before < x_current)
  63. {
  64. check_current = 1;
  65. }
  66.  
  67. int i;
  68. for (i = 2; i < k; i++)
  69. {
  70. x_before = x_current;
  71. x_current = functionFirst(x_start + delta * (double)i);
  72.  
  73. counter++;
  74.  
  75. check_last = check_current;
  76.  
  77. if (x_before < x_current)
  78. {
  79. check_current = 1;
  80. }
  81. if (check_last != check_current)
  82. {
  83. break;
  84. }
  85. ekstremum = x_start;
  86. }
  87.  
  88. return gridSearchRecurrent(x_start + (delta * ((double)i - 2)), x_start + (delta * (double)i), epsilon, counter, ekstremum);
  89. }
  90.  
  91.  
  92. void goldenRatio(double x_start, double x_end, double epsilon, int& counter, double& ekstremum)
  93. {
  94. double z = 0.61803398874989490;
  95. double x_left = 0.0;
  96. double x_right = 0.0;
  97. double f_x_left = 0.0;
  98. double f_x_right = 0.0;
  99.  
  100. x_left = x_end - (x_end - x_start) * z;
  101. x_right = x_start + (x_end - x_start) * z;
  102.  
  103. f_x_left = functionFirst(x_left);
  104. f_x_right = functionFirst(x_right);
  105.  
  106. counter += 2;
  107.  
  108. while (true)
  109. {
  110. counter++;
  111.  
  112. if (f_x_right <= f_x_left)
  113. {
  114. x_start = x_left;
  115. x_left = x_right;
  116. f_x_left = f_x_right;
  117. x_right = x_start + (x_end - x_start) * z;
  118. f_x_right = functionFirst(x_right);
  119. }
  120. else
  121. {
  122. x_end = x_right;
  123. x_right = x_left;
  124. f_x_right = f_x_left;
  125. x_left = x_end - (x_end - x_start) * z;
  126. f_x_left = functionFirst(x_left);
  127. }
  128.  
  129.  
  130. if (abs(x_end - x_start) > epsilon) {
  131. break;
  132. }
  133.  
  134. ekstremum = (x_end + x_start) / 2.;
  135.  
  136. }
  137. }
  138.  
  139. void falsi(double x_start, double x_end, double epsilon, int& counter, double& ekstremum)
  140. {
  141. double ekstremum_last = 0.0;
  142. double x1 = x_start;
  143. double x2 = x_end;
  144. double value = 0.0;
  145.  
  146. ekstremum = 0.0;
  147. counter = 0;
  148.  
  149. if (functionSecond(x_start) < 0.0)
  150. while (true)
  151. {
  152. ekstremum_last = ekstremum;
  153.  
  154. ekstremum = x1 - (functionSecond(x1) * ((x1 - x2) / (functionSecond(x1) - functionSecond(x2))));
  155.  
  156. value = functionSecond(ekstremum);
  157.  
  158. counter++;
  159.  
  160. if (value < 0.0)
  161. x1 = ekstremum;
  162. else if (value > 0.0)
  163. x2 = ekstremum;
  164.  
  165. if (value == 0.0 || fabs(ekstremum_last - ekstremum) < epsilon)
  166. break;
  167.  
  168. }
  169.  
  170. else if (functionSecond(x_start) > 0.0)
  171. while (true)
  172. {
  173. ekstremum_last = ekstremum;
  174.  
  175. ekstremum = x1 - (functionSecond(x1) * ((x1 - x2) / (functionSecond(x1) - functionSecond(x2))));
  176.  
  177. value = functionSecond(ekstremum);
  178.  
  179. counter++;
  180.  
  181. if (value > 0.0)
  182. x_start = ekstremum;
  183. else if (value < 0.0)
  184. x_end = ekstremum;
  185.  
  186. if (value == 0.0 || fabs(ekstremum_last - ekstremum) < epsilon)
  187. break;
  188.  
  189. }
  190. }
  191.  
  192. int main()
  193. {
  194.  
  195. double epsilon = 0.1;
  196. double x_start = 3.0;
  197. double x_end = 10.0;
  198. int counter_gridSearch = 0;
  199. int counter_gridSearchRecurrent = 0;
  200. int counter_goldenRatio = 0;
  201. int counter_falsi = 0;
  202. double ekstremum = 0.0;
  203.  
  204. for (int i = 1; i < 8; i++)
  205. {
  206. counter_gridSearch = 0;
  207. counter_gridSearchRecurrent = 0;
  208. counter_goldenRatio = 0;
  209. counter_falsi = 0;
  210.  
  211. std::cout << "==================================================" << std::endl;
  212. std::cout << "EPSILON: " << epsilon << std::endl;
  213. std::cout << std::endl;
  214.  
  215. gridSearch(x_start, x_end, epsilon, counter_gridSearch, ekstremum);
  216. std::cout << "GRID SEARCH: " << counter_gridSearch << " | " << ekstremum << std::endl;
  217.  
  218. gridSearchRecurrent(x_start, x_end, epsilon, counter_gridSearchRecurrent, ekstremum);
  219. std::cout << "GRID SEARCH RECURRENT: " << counter_gridSearchRecurrent << " | " << ekstremum << std::endl;
  220.  
  221. goldenRatio(x_start, x_end, epsilon, counter_goldenRatio, ekstremum);
  222. std::cout << "GOLDEN RATIO: " << counter_goldenRatio << " | " << ekstremum << std::endl;
  223.  
  224. falsi(x_start, x_end, epsilon, counter_falsi, ekstremum);
  225. std::cout << "FALSI: " << counter_falsi << " | " << ekstremum << std::endl;
  226. std::cout << "==================================================" << std::endl;
  227.  
  228. epsilon *= 0.1;
  229. }
  230.  
  231. system("pause");
  232. return 0;
  233. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement