Guest User

Untitled

a guest
Dec 14th, 2019
100
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <cmath>
  4. using namespace std;
  5.  
  6. double f(double x, int k)
  7. {
  8. return (x - k * cos(x));
  9. }
  10.  
  11.  
  12.  
  13. double pr(double x, int k)
  14. {
  15. return (1 + k * sin(x));
  16. }
  17.  
  18.  
  19. double pd(double a, double b, double eps, int k)
  20. {
  21. double c, x;
  22. int i;
  23. i = 0;
  24. while (abs(a - b) > eps)
  25. {
  26. c = (a + b) / 2;
  27. if ((f(a, k) * f(c, k)) < 0)
  28. {
  29. b = c;
  30. }
  31. else
  32. {
  33. a = c;
  34. }
  35. i++;
  36. }
  37. x = (a + b) / 2;
  38. cout << i << " iterrations" << endl;
  39. return x;
  40. }
  41.  
  42.  
  43. double newton(double a, double eps, int k)
  44. {
  45. double x1, xn;
  46. int i;
  47. i = 0;
  48. x1 = a;
  49. xn = x1 - f(x1, k) / pr(x1, k);
  50. while (abs(xn - x1) > eps)
  51. {
  52. x1 = xn;
  53. xn = x1 - f(x1, k) / pr(x1, k);
  54. i++;
  55. }
  56. cout << i << " iterrations" << endl;
  57. return xn;
  58. }
  59.  
  60. double iterration(double a, double eps, int k)
  61. {
  62. double x1, xn;
  63. int i;
  64. i = 0;
  65. x1 = a;
  66. xn = k * cos(x1);
  67. while (abs(xn - x1) > eps)
  68. {
  69. x1 = xn;
  70. xn = k * cos(x1);
  71. i++;
  72. if (i > 100000)
  73. break;
  74. }
  75.  
  76. cout << i << " iterrations" << endl;
  77. return xn;
  78. }
  79. #include <iostream>
  80. #include <cmath>
  81. #include <iomanip>
  82. #include "прототипы.h"
  83. using namespace std;
  84.  
  85. int main(){
  86. setlocale(LC_ALL, "rus");
  87. double a, b, kor;
  88.  
  89. cin >> a;
  90. cin >> b;
  91. cout << endl;
  92. cout << "x-cos(x)"<< endl;
  93.  
  94. kor = pd(a, b, 0.000001, 1);
  95. cout <<" метод половинное деление " << kor << endl;
  96. cout << endl;
  97.  
  98. kor = newton(a, 0.000001, 1);
  99. cout <<"метод Ньютона "<< kor << endl;
  100. cout << endl;
  101.  
  102. kor = iterration(a, 0.000001, 1);
  103. cout <<"метод итерации " <<kor << endl;
  104. cout << endl;
  105.  
  106. cout << "x-cos(x)" << endl;
  107.  
  108. kor = pd(a, b, 0.00000001, 1);
  109. cout << fixed << setprecision(8)<< "метод половинное деление " << kor << endl;
  110. cout << endl;
  111.  
  112. kor = newton(a, 0.00000001, 1);
  113. cout << fixed << setprecision(8) << "метод Ньютона " << kor << endl;
  114. cout << endl;
  115.  
  116. kor = iterration(a, 0.00000001, 1);
  117. cout <<fixed << setprecision(8)<< "метод итерации " << kor << endl;
  118. cout << endl;
  119.  
  120. cout << "x-10cos(x)" << endl;
  121.  
  122. kor = pd(a, b, 0.000001, 10);
  123. cout << "метод половинное деление " << kor << endl;
  124. cout << endl;
  125.  
  126. kor = newton(a, 0.000001, 10);
  127. cout << "метод Ньютона " << kor << endl;
  128. cout << endl;
  129.  
  130. kor = iterration(a, 0.000001, 10);
  131. cout << "метод итерации " << kor << endl;
  132.  
  133. system("pause");
  134. return 0;
  135. }
RAW Paste Data