Advertisement
Guest User

Untitled

a guest
Dec 11th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.36 KB | None | 0 0
  1. using namespace std;
  2.  
  3. #include <cstdio>
  4. #include <cmath>
  5.  
  6. double F(double x) {
  7.     return x * x * x * x + 2 * x * x * x + sin(x) + 0.5;
  8. }
  9.  
  10. double f(double x) {
  11.     return 2 * (2 * x + 3) * x * x + cos(x);
  12. }
  13.  
  14. double f2(double x) {
  15.     return 12 * x * (x + 1) - sin(x);
  16. }
  17.  
  18. int *iteration(double root, double step) {
  19.     double a = root;
  20.     double b = root + step;
  21.     double da = f(a);
  22.     double db = f(b), m, M;
  23.     abs(da) > abs(db) ? (M = da, m = db) : (M = db, m = da);
  24.  
  25.     double alpha = 1 / M, q = 1 - abs(m / M);
  26.  
  27.     int *iters = new int[3], *iters_ptr = iters;
  28.     printf("|  eps  | equation root | accuracy estimation for I |\n");
  29.     for (double eps = 1e-2; eps > 1e-14; eps *= 1e-3) {
  30.         double sigma = (1 - q) / q * eps, x = (a + b) / 2, x1 = x;
  31.         int iterations = 0;
  32.         do {
  33.             iterations++;
  34.             x = x1;
  35.             x1 = x - alpha * F(x);
  36.         } while (abs(x1 - x) > sigma);
  37.         *iters_ptr++ = iterations;
  38.         double accuracy = abs(abs(x1) - abs(x)) * q / (1 - q);
  39.         printf("| %.0e | %13.10f | %25.23f |\n", eps, x1, accuracy);
  40.     }
  41.     printf("\n");
  42.     return iters;
  43. }
  44.  
  45. int *tangent(double root, double step) {
  46.     int *iters = new int[3], *iters_ptr = iters;
  47.  
  48.     double a = root;
  49.     double b = root + step;
  50.  
  51.     double m = fabs(f(a));
  52.     for (double x = a; x < b; x += 0.001) {
  53.         double y = fabs(f(x));
  54.         if (m >= y) m = y;
  55.     }
  56.  
  57.     double x1 = F(a) * f2(a) ? a : b;
  58.  
  59.     printf("|  eps  | equation root | accuracy estimation for D |\n");
  60.     for (double eps = 1e-2; eps > 1e-14; eps *= 1e-3) {
  61.         double x = x1;
  62.         int iterations = 0;
  63.         while (fabs(F(x))/m > eps) {
  64.             iterations++;
  65.             x -= F(x) / f(x);    
  66.         }
  67.         *iters_ptr++ = iterations;
  68.         double accuracy = fabs(F(x)) / m;
  69.         printf("| %.0e | %13.10f | %25.23f |\n", eps, x, accuracy);
  70.     }
  71.     printf("\n");
  72.     return iters;
  73. }
  74.  
  75. int main() {
  76.     double step = 0.5;
  77.     double a = -3;
  78.     double b = 0;
  79.  
  80.     double roots[2], *roots_ptr = roots;
  81.     for (double x = a; x < b; x += step) {
  82.         if (F(x) * F(x + step) < 0) {
  83.             *roots_ptr++ = x;
  84.         }
  85.     }
  86.  
  87.     int *iters_i = iteration(roots[0], step);
  88.     for (int i = 1; i < 3; i++) {
  89.         iteration(roots[i], step);
  90.     }
  91.  
  92.     int *iters_d = tangent(roots[0], step);
  93.     for (int i = 1; i < 3; i++) {
  94.         tangent(roots[i], step);
  95.     }
  96.  
  97.     printf("|  eps  | iteration count for I | iteration count for D |\n");
  98.     for (double eps = 1e-2; eps > 1e-14; eps *= 1e-3) {
  99.         printf("| %.0e | %21i | %21i |\n", eps, *iters_i++, *iters_d++);
  100.     }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement