Advertisement
Guest User

Untitled

a guest
May 24th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.07 KB | None | 0 0
  1. // laba 6+++++.cpp: определяет точку входа для консольного приложения.
  2. //
  3.  
  4. #pragma
  5.  
  6. #include "stdafx.h"
  7. #define _USE_MATH_DEFINES
  8. #include <iostream>
  9. #include <conio.h>
  10. #include <cmath>
  11. #include <tuple>
  12.  
  13. using namespace std;
  14.  
  15. typedef double(*type_f)(double);
  16. double fun(double);
  17. double diff_fun(double);
  18. tuple<double, int> Method_Del_2(type_f, double, double, double);
  19. tuple<double, int> Method_Newton(double, double);
  20. tuple<double, int> Method_Secant(double, double);
  21. tuple<double, int> Metod_Vegsteina(type_f, double, double, double);
  22.  
  23. int main()
  24. {
  25. setlocale(LC_ALL, "rus");
  26. double a, b, x, eps, h, y;
  27. int nom = 0, exit = 0, iter = 1;
  28. do {
  29. cout << "Функция: 4*x - 7*sin(x)" << endl;
  30. cout << "Введите предел [a;b] (для [-2;5] 3 корня)\n=>";
  31. cout << "\ta = ";
  32. cin >> a;
  33. cout << "\tb = ";
  34. cin >> b;
  35. if (a > b) // если пользователь перепутал границы отрезка, меняем их местами
  36. {
  37. x = a;
  38. a = b;
  39. b = x;
  40. }
  41. cout << "Введите значения eps, h: " << endl;
  42. cout << "\teps = ";
  43. cin >> eps;
  44. cout << "\th = ";
  45. cin >> h;
  46. cout << "\n\nТаблица значений функции при разных значениях х, с шагом 1 " << endl;
  47. cout << "_________________________________" << endl;
  48.  
  49. cout << "____x____|_______fun(x)_________|" << endl;
  50. for (double x = a; x <= b; x += 1)
  51. {
  52. cout << " " << x << "\t | " << ' ' << fun(x) << "\t\t|" << endl;
  53. cout << "_________|______________________|" << endl;
  54. }
  55. if (eps == 0 && h == 0)
  56. {
  57. cout << "Задайте eps > 0 и h > 0 \n" << endl;
  58. }
  59. else
  60. {
  61. cout << " \t\tМетод: деление по-полам\n";
  62. for (x = a; x <= b; x += h)
  63. if (fun(x) * fun(x + h) < 0)
  64. {
  65. nom++;
  66. tie(y, iter) = Method_Del_2(fun, x, x + h, eps);
  67. // Вывод номера nom и приближенного корня y
  68. cout << " Root " << nom << " = " << y << "\tколичество итераций = " << iter << "\tзначение функции в точке = " << fun(y) << endl;
  69. }
  70.  
  71. cout << "\t\tМетод Ньютона\n";
  72. for (x = a; x <= b; x += h)
  73. {
  74. if (diff_fun == 0)
  75. {
  76. cout << "Error! Деление на ноль!";
  77. }
  78. else
  79. {
  80. if (fun(x)*fun(x + h) < 0)
  81. {
  82. nom++;
  83. tie(y, iter) = Method_Newton(x, eps);
  84. cout << " Root " << nom << " = " << y << "\tколичество итераций = " << iter << "\tзначение функции в точке = " << fun(y) << endl;
  85. }
  86. }
  87. }
  88.  
  89. cout << "\t\tМетод секущих\n";
  90. for (x = a; x <= b; x += h)
  91. {
  92. if (fun(x)*fun(x + h) < 0)
  93. {
  94. nom++;
  95. tie(y, iter) = Method_Secant(x, eps);
  96. cout << " Root " << nom << " = " << y << "\tколичество итераций = " << iter << "\tзначение функции в точке = " << fun(y) << endl;
  97. }
  98. }
  99.  
  100. cout << "\t\tМетод Вегстейна\n";
  101. for (x = a; x <= b; x += h)
  102. {
  103. if (fun(x)*fun(x + h) < 0)
  104. {
  105. nom++;
  106. tie(y, iter) = Metod_Vegsteina(fun, x, x + h, eps);
  107. cout << " Root " << nom << " = " << y << "\tколичество итераций = " << iter << "\tзначение функции в точке = " << fun(y) << endl;
  108. }
  109. }
  110. if (nom == 0) cout << "На отрезке корней НЕТ!" << endl;
  111. cout << "\nExit? Yes - 1 =>";
  112. cin >> exit;
  113. }
  114. } while (exit != 1); // пока пользователь не ввел exit = 1
  115. }
  116.  
  117. double fun(double x)
  118. {
  119. return 4 * x - 7 * sin(x);
  120. }
  121.  
  122. double diff_fun(double x)
  123. {
  124. return 4 - 7 * cos(x);
  125. }
  126.  
  127. tuple<double, int> Method_Del_2(type_f fun, double x0, double x1, double eps)
  128. {
  129. double x2, y0, y2;
  130. int iter = 0;
  131. y0 = fun(x0);
  132. do
  133. {
  134. x2 = (x0 + x1) / 2;
  135. y2 = fun(x2);
  136. if (y0*y2 > 0)
  137. {
  138. x0 = x2;
  139. y0 = y2;
  140. }
  141. else x1 = x2;
  142. } while (fabs(x1 - x0) > eps && ++iter);
  143. //cout << iter << " iterations" << endl;
  144. return make_tuple((x0 + x1) / 2, iter);
  145. }
  146.  
  147. tuple<double, int> Method_Newton(double xt, double eps)
  148. {
  149. double xn;
  150. int iter = 0;
  151. do
  152. {
  153. xn = xt;
  154. xt = xn - fun(xn) / diff_fun(xn);
  155. } while (fabs(xt - xn) > eps && ++iter);
  156. return make_tuple(xt, iter); // Возвращаем значение, для которого достигнута точность
  157. }
  158.  
  159. tuple<double, int> Method_Secant(double x0, double eps)
  160. {
  161. double x1, res = 0, y;
  162. int iter = 0;
  163. do
  164. {
  165. y = res;
  166. x1 = x0 + eps;
  167. res = x1 - ((x1 - x0) / (fun(x1) - fun(x0))) * fun(x1);
  168. } while (fabs(y - res) >= eps && ++iter);
  169. return make_tuple(res, iter);
  170. }
  171.  
  172. tuple<double, int> Metod_Vegsteina(type_f fun, double x0, double x1, double eps)
  173. {
  174. double y0, y1, x2, de, iter = 5;
  175. //int iter = 0;
  176. y0 = fun(x0);
  177. y1 = fun(x1);
  178. do
  179. {
  180. x2 = x1 - y1 * (x1 - x0) / (y1 - y0);
  181. de = fabs(x1 - x2);
  182. x0 = x1;
  183. x1 = x2;
  184. y0 = y1;
  185. y1 = fun(x2);
  186. } while (de > eps && ++iter);
  187. return make_tuple(x2, iter);
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement