Advertisement
fqrmix

Untitled

Mar 2nd, 2017
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.79 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <math.h>
  4.  
  5. using namespace std;
  6.  
  7. double xx0, xx1; // промежуток в ответе шагового метода
  8. double a = 0, b = 1, h = 0.1, e = 0.001; // исходные данные
  9.  
  10. double function(double x)
  11. {
  12.     return acos(x) - sqrt(1 - 0.3 * pow(x, 3)); // Исходная функция
  13. }
  14. double derivative(double x)
  15. {
  16.     return (-1 / sqrt(1 - pow(x, 2))) + ((0.45 * pow(x, 2)) / sqrt(1 - 0.3 * pow(x, 3))); // Производная исходной функции
  17. }
  18. double S(double x)
  19. {
  20.     return x - 0.005 * function(x);
  21. }
  22.  
  23. void step_method() // Шаговый метод.
  24. {
  25.     double x0 = a, x1 = x0 + h;
  26.     while (x1 <= b)
  27.     {
  28.         x1 = x0 + h;
  29.         if (function(x0) * function(x1) < 0)
  30.         {
  31.             cout << "Step method:\n";
  32.             cout << "x on int. [" << x0 << "," << x1 << "]\n" << endl;
  33.             xx0 = x0;
  34.             xx1 = x1;
  35.         }
  36.         x0 = x1;
  37.     }
  38. }
  39. void bisection_method() // Метод половинного деления.
  40. {
  41.     double x = xx0;
  42.     a = xx0;
  43.     b = xx1;
  44.     while (fabs(function(x)) > e)
  45.     {
  46.         x = (a + b) / 2;
  47.         if (function(a) * function(x) < 0)
  48.             b = x;
  49.         else a = x;
  50.     }
  51.     cout << "Bisection method:\nx= " << x << "\n" << endl;
  52. }
  53. void newton_method() // Метод Ньютона.
  54. {
  55.     double x2 = xx0;
  56.     while (fabs(function(x2)) > e)
  57.     {
  58.         x2 = x2 - function(x2) / derivative(x2);
  59.     }
  60.     cout << "Newton's method:\nx= " << x2 << "\n" << endl;
  61. }
  62. void fixed_point_iteration() // Метод простой итерации.
  63. {
  64.     double x3 = xx0;
  65.     while (fabs(function(x3)) > e)
  66.     {
  67.         x3 = S(x3);
  68.     }
  69.     cout << "Fixed-point iteration method:\nx= " << x3 << "\n" << endl;
  70. }
  71. int main()
  72. {
  73.  
  74.     setlocale(LC_ALL, "");
  75.  
  76.     step_method();
  77.     bisection_method();
  78.     newton_method();
  79.     fixed_point_iteration();
  80.  
  81.     system("pause");
  82.     return 0;
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement