Advertisement
Tetriss

Untitled

Feb 17th, 2020
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.10 KB | None | 0 0
  1. #include <iostream>
  2. #include <math.h>
  3. #include <iomanip>
  4.  
  5. using namespace std;
  6.  
  7. double function1(const double& x)
  8. {
  9. double y = cos(x)+log10(x);
  10. return y;
  11. }
  12. double function2(const double& x)
  13. {
  14. double y = (cos(x)+log10(x)) * sin(5 * x);
  15. return y;
  16. }
  17.  
  18. bool probability(const double& P)
  19. {
  20. double value = ((double)(rand() % 1000)) / 1000.0;
  21. if (value <= P) return true;
  22. else return false;
  23. }
  24. double Rand(double Min, double Max)
  25. {
  26. double f = (double)rand() / RAND_MAX;
  27. return Min + f * (Max - Min);
  28. }
  29.  
  30. int main()
  31. {
  32. double a = 7, b = 10;
  33. double Tmax = 10000, Tmin = 0.01, Ti = Tmax;
  34. double x2, x1 = Rand(a, b);
  35. double y2, y1 = function1(x1);
  36. double f_delta;
  37. double P;
  38. int n = 0;
  39. string change;
  40. cout << "f(x) = cos(x)+lg(x)" << endl << endl;
  41. cout << " N x f(x) P change Ti" << endl << endl;
  42. while (Ti > Tmin)
  43. {
  44. x2 = Rand(a, b);
  45. y2 = function1(x2);
  46. f_delta = y2 - y1;
  47. if (f_delta <= 0)
  48. {
  49. x1 = x2;
  50. y1 = y2;
  51. P = 1;
  52. change = "yes";
  53. }
  54. else
  55. {
  56. P = exp(-(f_delta / Ti));
  57. if (probability(P))
  58. {
  59. x1 = x2;
  60. y1 = y2;
  61. change = "yes";
  62. }
  63. else change = "no";
  64. }
  65. cout << setw(3) << ++n << fixed << " " << x1 << " " << y1 << " " << P << " " << setw(3) << change << " " << Ti << endl;
  66. Ti = Ti * 0.95;
  67. }
  68.  
  69. x1 = Rand(a, b);
  70. y1 = function2(x1);
  71. Ti = Tmax;
  72. n = 0;
  73. cout << endl << endl << "f(x) = [cos(x)+lg(x)] * sin(5x)" << endl << endl;
  74. cout << " N x f(x) P change Ti" << endl << endl;
  75. while (Ti > Tmin)
  76. {
  77. x2 = Rand(a, b);
  78. y2 = function2(x2);
  79. f_delta = y2 - y1;
  80. if (f_delta <= 0)
  81. {
  82. x1 = x2;
  83. y1 = y2;
  84. P = 1;
  85. change = "yes";
  86. }
  87. else
  88. {
  89. P = exp(-(f_delta / Ti));
  90. if (probability(P))
  91. {
  92. x1 = x2;
  93. y1 = y2;
  94. change = "yes";
  95. }
  96. else change = "no";
  97. }
  98. cout << setw(3) << ++n << fixed << right << setw(6) << " " << x1 << " " << setw(12) << y1 << " " << P << " " << setw(3) << change << " " << setw(6) << Ti << endl;
  99. Ti = Ti * 0.95;
  100. }
  101.  
  102.  
  103. return 0;
  104.  
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement