Advertisement
dark-s0ul

lab2

Jan 17th, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.35 KB | None | 0 0
  1. #include <cstdio>
  2. #include <cmath>
  3. #include <vector>
  4.  
  5. double a = -2;
  6. double b = 2;
  7.  
  8. double function(double x) {
  9.     return sin(2.0 * x) + pow(cos(x), 2) - x * 0.5;
  10. }
  11.  
  12. double derivative(double x) {
  13.     return 2.0 * cos(2.0 * x) - 2.0 * sin(x) * cos(x) - 0.5;
  14. }
  15.  
  16. std::vector<int> iteration(double x, double step) {
  17.     std::vector<int> iterations;
  18.  
  19.     double a = x;
  20.     double b = x + step;
  21.     double da = derivative(a);
  22.     double db = derivative(b);
  23.     double alpha = 1.0 / ((abs(da) > abs(db)) ? da : db);
  24.     double q = 1.0 - abs((abs(da) > abs(db) ? (db / da) : (da / db)));
  25.     double k = 1.0 / q - 1.0;
  26.  
  27.     printf("|  eps  |     equation root     | accuracy estimation I |\n");
  28.     for (double eps = 1E-2; eps > 1E-14; eps *= 1E-3) {
  29.         double sigma = k * eps;
  30.         double x0 = (a + b) * 0.5;
  31.         double x1 = x0;
  32.         int count = 0;
  33.         do {
  34.             x0 = x1;
  35.             x1 = x0 - alpha * function(x0);
  36.             count++;
  37.         } while (abs(x1 - x0) > sigma);
  38.         iterations.push_back(count);
  39.         printf("| %.0E | %21.18f | %.15E |\n", eps, x1, abs(x1 - x0) / k);
  40.     }
  41.     printf("\n");
  42.  
  43.     return iterations;
  44. }
  45.  
  46. std::vector<int> bisection(double x, double step) {
  47.     std::vector<int> iterations;
  48.     printf("|  eps  |     equation root     | accuracy estimation B |\n");
  49.     for (double eps = 1E-2; eps > 1E-14; eps *= 1E-3) {
  50.         double a = x;
  51.         double b = x + step;
  52.         int count = 0;
  53.         do {
  54.             double c = (a + b) * 0.5;
  55.  
  56.             double fa = function(a);
  57.             double fb = function(b);
  58.             double fc = function(c);
  59.  
  60.             if (fa * fc < 0) b = c;
  61.             if (fb * fc < 0) a = c;
  62.             count++;
  63.         } while (abs(b - a) >= 2 * eps);
  64.         iterations.push_back(count);
  65.         printf("| %.0E | %21.18f | %.15E |\n", eps, (a + b) * 0.5, abs((b - a) * 0.5));
  66.     }
  67.     printf("\n");
  68.  
  69.     return iterations;
  70. }
  71.  
  72. std::vector<double> findroots() {
  73.     std::vector<double> X;
  74.     for (double x = a; x <= b; x += 0.1) {
  75.         if (function(x) * function(x + 0.1) < 0) {
  76.             X.push_back(x);
  77.         }
  78.     }
  79.     return X;
  80. }
  81.  
  82. int main() {
  83.     auto roots = findroots();
  84.  
  85.     auto ii = iteration(roots[0], 0.1);
  86.     for (int i = 1; i < 3; i++) {
  87.         iteration(roots[i], 0.1);
  88.     }
  89.  
  90.     auto ib = bisection(roots[0], 0.1);
  91.     for (int i = 1; i < 3; i++) {
  92.         bisection(roots[i], 0.1);
  93.     }
  94.  
  95.     printf("|  eps  | iteration | bisection |\n");
  96.     int index = 0;
  97.     for (double eps = 1E-2; eps > 1E-14; eps *= 1E-3, index++) {
  98.         printf("| %.0E | %9i | %9i |\n", eps, ii[index], ib[index]);
  99.     }
  100.     return 0;
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement