Advertisement
Guest User

Untitled

a guest
Nov 15th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.87 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 fibonnaciSearchMIN(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. points.clear();
  129. double pointA = A;
  130. double pointB = B;
  131. double firstLowestValue = MAXFLOAT;
  132. double secondLowestValue = MAXFLOAT;
  133. double buffer;
  134. int whichA = 1;
  135. int whichB = 1;
  136. while((pointB - pointA) > accuracy) {
  137. for (int i = 0; i < points_amount; i++) {
  138. points.push_back(randomFloatRange(pointA, pointB));
  139. pointsValues.push_back(functionD(points[i]));
  140.  
  141. buffer = pointsValues[i];
  142. if (buffer < firstLowestValue) {
  143. whichA = i;
  144. firstLowestValue = buffer;
  145. } else if (buffer < secondLowestValue) {
  146. whichB = i;
  147. secondLowestValue = buffer;
  148. }
  149. }
  150. pointA = points[whichA];
  151. pointB = points[whichB];
  152.  
  153. std::cout << pointA << " " << pointB << std::endl;
  154. }
  155.  
  156. }
  157.  
  158.  
  159.  
  160.  
  161.  
  162. int main() {
  163. int chosenNumber = 2;
  164. srand(time(NULL));
  165. int i = 0;
  166. //std::cout << fibonacciNumbers(chosenNumber) << std::endl;
  167. //std::cout << lucasNumbers(chosenNumber) << std::endl;
  168.  
  169. //fibonnaciSearchMAX(-1, 1, 20);
  170.  
  171. twoRandfromX(-1, 1, 100, 0.0001);
  172.  
  173. return 0;
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement