Advertisement
dark-s0ul

lab2.cpp

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