Advertisement
Guest User

Untitled

a guest
Nov 16th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.98 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <math.h>
  4. #include <random>
  5.  
  6. #define function(X) X / (1.2 - sin(2 * X))
  7.  
  8. using namespace std;
  9.  
  10.  
  11. double functionA(double X)
  12. {
  13. return X / (1.2 - sin(2*X));
  14.  
  15. }
  16.  
  17. double functionB(double X)
  18. {
  19. return ((pow(X, 3) - pow(X, 2) + 4 * X) / (2 * pow(X, 3) + 5 * pow(X, 2) - 2 * X + 1));
  20.  
  21. }
  22.  
  23. double functionC(double X)
  24. {
  25. return (tan(sin(2 * X + 1)));
  26.  
  27. }
  28.  
  29. double functionD(double X)
  30. {
  31. return (1 / (1 + exp(sin(2 * X + 1) + 2)));
  32.  
  33. }
  34.  
  35.  
  36. double randomFloatRange(double a, double b) {
  37. static std::default_random_engine e;
  38. static std::uniform_real_distribution<> dis(a, b);
  39. return dis(e);
  40.  
  41. }
  42.  
  43.  
  44. int fibonacciNumbers(int sequenceTerm)
  45. {
  46. const int firstTerm = 1;
  47. const int secondTerm = 1;
  48. int F[sequenceTerm];
  49. F[0] = firstTerm;
  50. F[1] = secondTerm;
  51.  
  52. for(int i = 2; i <= sequenceTerm; i++)
  53. {
  54. F[i] = F[i - 1] + F[i - 2];
  55. }
  56.  
  57. return F[sequenceTerm];
  58. }
  59.  
  60.  
  61. int lucasNumbers(int sequenceTerm)
  62. {
  63. const int firstTerm = 2;
  64. const int secondTerm = 1;
  65. int F[sequenceTerm];
  66. F[0] = firstTerm;
  67. F[1] = secondTerm;
  68.  
  69. for(int i = 2; i <= sequenceTerm; i++)
  70. {
  71. F[i] = F[i - 1] + F[i - 2];
  72. }
  73.  
  74. return F[sequenceTerm];
  75.  
  76. }
  77.  
  78.  
  79. void fibonnaciSearch(double initA, double initB, int iteration)
  80. {
  81. double A[iteration];
  82. double B[iteration];
  83. double pointX1[iteration];
  84. double pointX2[iteration];
  85. double functionX1;
  86. double functionX2;
  87.  
  88. A[0] = initA;
  89. B[0] = initB;
  90.  
  91. std::cout << "Initial values (a, b) = " << "(" << initA << ", " << initB << ")" <<std::endl;
  92.  
  93. for(int k = 1; k <= iteration; k++)
  94. {
  95. pointX1[k - 1] = (A[k - 1]) + (((B[k - 1]) - (A[k - 1]))
  96. * lucasNumbers(iteration - k + 1) / lucasNumbers(iteration - k + 3));
  97.  
  98. pointX2[k - 1] = A[k - 1] + ((B[k - 1] - A[k - 1])
  99. * lucasNumbers(iteration - k + 2) / lucasNumbers(iteration - k + 3));
  100. functionX1 = function(pointX1[k - 1]);
  101. functionX2 = function(pointX2[k - 1]);
  102. if(functionX1 <= functionX2)
  103. {
  104. A[k] = A[k - 1];
  105. B[k] = pointX2[k - 1];
  106. }
  107. else if(functionX1 > functionX2)
  108. {
  109. A[k] = pointX1[k - 1];
  110. B[k] = B[k - 1];
  111.  
  112. }
  113.  
  114. std::cout << "\nIteration: " << k << " Inner Points (a, b): " << "(" <<A[k] << ", " << B[k] << ")" << std::endl;
  115. std::cout << "MidPoint: " << (A[k] + B[k]) / 2 << std::endl;
  116.  
  117.  
  118. }
  119.  
  120. }
  121.  
  122.  
  123.  
  124. void twoRandfromX(double A, double B, int points_amount, double accuracy)
  125. {
  126. std::vector<double> points;
  127. std::vector<double> pointsValues;
  128. int iterations = 0;
  129. points.clear();
  130. double pointA = A;
  131. double pointB = B;
  132. points.push_back(pointA);
  133. points.push_back(pointB);
  134. double firstLowestValue = MAXFLOAT;
  135. double secondLowestValue = MAXFLOAT;
  136. double buffer;
  137. int whichA = 1;
  138. int whichB = 1;
  139. while((pointB - pointA) > accuracy) {
  140. for (int i = 0; i < points_amount; i++) {
  141. if(i >= 2)
  142. {
  143. points.push_back(randomFloatRange(pointA, pointB));
  144. }
  145. pointsValues.push_back(functionD(points[i]));
  146.  
  147. buffer = pointsValues[i];
  148. if (buffer < firstLowestValue) {
  149. whichA = i;
  150. firstLowestValue = buffer;
  151. } else if (buffer < secondLowestValue) {
  152. whichB = i;
  153. secondLowestValue = buffer;
  154. }
  155. iterations++;
  156. }
  157. pointA = points[whichA];
  158. pointB = points[whichB];
  159. std::cout << "After " << iterations << " loops of points creation" << std::endl;
  160. std::cout << pointA << " " << pointB << std::endl;
  161. }
  162.  
  163. }
  164.  
  165. void xEvenlySpaced(double A, double B, int points_amount, double accuracy)
  166. {
  167. std::vector<double> points;
  168. std::vector<double> pointsValues;
  169. double firstPoint = A;
  170. double secondPoint = B;
  171. double difference;
  172. double space = MAXFLOAT;
  173. double currentPoint;
  174. double buffer;
  175. int whichA = 1;
  176. int whichB = 1;
  177. double firstLowestValue = MAXFLOAT;
  178. double secondLowestValue = MAXFLOAT;
  179. std::cout << firstPoint << " " << secondPoint << std::endl;
  180.  
  181. difference = secondPoint - firstPoint;
  182. space = difference / points_amount;
  183.  
  184.  
  185. while(space > accuracy)
  186. {
  187.  
  188. currentPoint = firstPoint;
  189. std::cout << space << std::endl;
  190. for (int i = 0; i < points_amount; i++)
  191. {
  192.  
  193.  
  194. points.push_back(currentPoint);
  195. //std::cout << points[i] << std::endl;
  196.  
  197. pointsValues.push_back(functionB(points[i]));
  198. buffer = pointsValues[i];
  199. currentPoint += space;
  200. if(buffer < firstLowestValue)
  201. {
  202. whichA = i;
  203. firstLowestValue = buffer;
  204.  
  205. }
  206. else if(buffer < secondLowestValue)
  207. {
  208. whichB = i;
  209. secondLowestValue = buffer;
  210. }
  211. //std::cout << firstLowestValue << " " << secondLowestValue << std::endl;
  212.  
  213.  
  214. }
  215. if(points[whichA] < points[whichB])
  216. {
  217. firstPoint = points[whichA];
  218. secondPoint = points[whichB];
  219. }else
  220. {
  221. firstPoint = points[whichB];
  222. secondPoint = points[whichA];
  223. }
  224.  
  225.  
  226.  
  227. difference = secondPoint - firstPoint;
  228. space = difference / points_amount;
  229.  
  230. std::cout << firstPoint << " " << secondPoint << std::endl;
  231.  
  232.  
  233. }
  234. }
  235.  
  236.  
  237.  
  238.  
  239.  
  240. int main() {
  241. int chosenNumber = 2;
  242. srand(time(NULL));
  243. int i = 0;
  244. //std::cout << fibonacciNumbers(chosenNumber) << std::endl;
  245. //std::cout << lucasNumbers(chosenNumber) << std::endl;
  246.  
  247. //fibonnaciSearchMAX(-1, 1, 20);
  248.  
  249. //twoRandfromX(-1, 1, 100, 0.0001);
  250.  
  251. xEvenlySpaced(-1, 1, 100, 0.0001);
  252.  
  253. return 0;
  254. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement