Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.05 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cmath>
  4. #include <clocale>
  5. double Delta = 0.0000001;
  6.  
  7. double Round(double X, double delta)
  8. {
  9. if(delta <= 1E-9)
  10. {
  11. //std::wcout << L"Неверное задание точности округления\n";
  12. std::cout << "round";
  13. return 0;
  14. }
  15. if(X > 0.0)
  16. return delta * long((X / delta) + 1.0 / 2);
  17. else
  18. return delta * long((X / delta) - 1.0 / 2);
  19. }
  20.  
  21. double F(double x)
  22. {
  23. return sinh(x) - x + 1;
  24. }
  25.  
  26.  
  27. double HORDA(double Left, double Right, double Eps, int &N) {
  28. double FLeft = F(Left);
  29. double FRight = F(Right);
  30. double X, Y;
  31.  
  32. if (FLeft * FRight > 0.0) {
  33. //puts("Неверное задание интервала\n");
  34. std::cout << "F*L";
  35. exit(1);
  36. }
  37.  
  38. if (Eps <= 0.0) {
  39. //puts("Неверное задание точности\n");
  40. std::cout << "EPS";
  41. exit(1);
  42. }
  43.  
  44. N = 0;
  45.  
  46. if (FLeft == 0.0) {
  47. return Left;
  48. }
  49.  
  50. if (FRight == 0.0) {
  51. return Right;
  52. }
  53.  
  54. do {
  55. X = Left - (Right - Left) * FLeft / (FRight - FLeft);
  56. Y = F(X); // результаты и скорость сходимости
  57. //Y = Round(F(X), Delta); // чувствительность метода к ошибкам
  58. if (Y == 0.0) {
  59. return X;
  60. }
  61.  
  62. if (Y * FLeft < 0.0) {
  63. Right = X;
  64. FRight = Y;
  65. } else {
  66. Left = X;
  67. FLeft = Y;
  68. }
  69.  
  70. N++;
  71. } while (fabs(Y) >= Eps);
  72.  
  73. return Round(X, Eps);
  74. }
  75.  
  76. int main() {
  77. setlocale(LC_ALL, "Russian");
  78. int N;
  79. double eps = 0.1;
  80. double res;
  81. const double x = -1.7291169;
  82.  
  83. for(int i = 0; i < 7; i++)
  84. {
  85. res = HORDA(-2, -1.8, eps, N);
  86.  
  87. printf("F(x) = 0 => x = %.7f\n"
  88. "x* - x = %.7f\n"
  89. "N(EPS) = %u(%.7f)\n\n",
  90. res, fabs(res - x), N, eps);
  91.  
  92. eps /= 10;
  93. }
  94. return 0;
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement