Advertisement
bogdan2004333

Untitled

Oct 12th, 2022 (edited)
550
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <locale.h>
  3. #include <math.h>
  4.  
  5. float *func(float xl, float xr) {
  6.  
  7.     float res[2];
  8.     float n = 0;
  9.     float X = (xl + xr) / 2;
  10.  
  11.     while (fabs(X - cos(X)) > 0.000001) {
  12.         n++;
  13.         if (X > cos(X)) {
  14.             xr = X;
  15.             X = (xr + xl) / 2;
  16.         } else if (X < cos(X)) {
  17.             xl = X;
  18.             X = (xr + xl) / 2;
  19.         }
  20.     }
  21.  
  22.     res[0] = X;
  23.     res[1] = n;
  24.  
  25.     return res;
  26. }
  27.  
  28. float *func2(float xl1, float xr1) {
  29.  
  30.     float res1[2];
  31.     float n1 = 0;
  32.     float X1 = (xl1 + xr1) / 2;
  33.  
  34.     while (fabs(X1 - cos(X1)) > 0.00000001) {
  35.         n1++;
  36.         if (X1 > cos(X1)) {
  37.             xr1 = X1;
  38.             X1 = (xr1 + xl1) / 2;
  39.         } else if (X1 < cos(X1)) {
  40.             xl1 = X1;
  41.             X1 = (xr1 + xl1) / 2;
  42.         }
  43.     }
  44.  
  45.     res1[0] = X1;
  46.     res1[1] = n1;
  47.  
  48.     return res1;
  49. }
  50.  
  51. float *func3(float xl2, float xr2, float k) {
  52.  
  53.     float res2[2];
  54.     float n2 = 0;
  55.     float X2 = (xl2 + xr2) / 2;
  56.  
  57.     while (fabs(X2 - k * cos(X2)) > 0.000001) {
  58.         n2++;
  59.         if (X2 > k * cos(X2)) {
  60.             xr2 = X2;
  61.             X2 = (xr2 + xl2) / 2;
  62.         } else if (X2 < k * cos(X2)) {
  63.             xl2 = X2;
  64.             X2 = (xr2 + xl2) / 2;
  65.         }
  66.     }
  67.  
  68.     res2[0] = X2;
  69.     res2[1] = n2;
  70.  
  71.     return res2;
  72. }
  73.  
  74. int main() {
  75.     setlocale(0, "");
  76.     float x = 0;
  77.     int n = 0;
  78.     float delta = 0.000001;
  79.     float delta1 = 0.00000001;
  80.     fflush(stdout);
  81.     printf("\nУравнение: x-cos(x)=0. Погрешность: %f", delta);
  82.     printf("\n № метода%8sx%15sN", "", "");
  83.  
  84.     while (fabs(x - cos(x)) > delta) {
  85.         x = cos(x);
  86.         n++;
  87.     }
  88.     printf("\n1%15s%.8f%7s%d", "", x, "", n);
  89.  
  90.     float *res;
  91.     res = func(-1, 1);
  92.     printf("\n2%15s%.8f%7s%d", "", res[0], "", (int) res[1]);
  93.  
  94.     x = 0;
  95.     n = 0;
  96.  
  97.     while (fabs(x - cos(x)) > delta) {
  98.         x = x - ((x - cos(x)) / (1 + sin(x)));
  99.         n++;
  100.     }
  101.     printf("\n3%15s%.8f%7s%d", "", x, "", n);
  102.  
  103.     printf("\n\nУравнение: x-cos(x)=0. Погрешность: %.8f", delta1);
  104.     printf("\n № метода%8sx%15sN", "", "");
  105.  
  106.     double x1 = 0;
  107.     int n1 = 0;
  108.  
  109.     while (fabs(x1 - cos(x1)) > delta1) {
  110.         x1 = cos(x1);
  111.         n1++;
  112.     }
  113.     printf("\n1%15s%.8lf%7s%d", "", x1, "", n1);
  114.  
  115.     float *res1;
  116.     res1 = func2(-1, 1);
  117.     printf("\n2%15s%.8f%7s%d", "", res1[0], "", (int) res1[1]);
  118.  
  119.     x1 = 0;
  120.     n1 = 0;
  121.  
  122.     while (fabs(x1 - cos(x1)) > delta1) {
  123.         x1 = x1 - ((x1 - cos(x1)) / (1 + sin(x1)));
  124.         n1++;
  125.     }
  126.     printf("\n3%15s%.8f%7s%d", "", x1, "", n1);
  127.     printf("\n\nУравнение: x - k * cos(x) = 0. k = 5. Погрешность: %f", delta);
  128.     printf("\n № метода%8sx%15sN", "", "");
  129.  
  130.     double x2 = 0;
  131.     int n2 = 0;
  132.     double k = 5;
  133.  
  134.     while (fabs(x2 - k * cos(x2)) > 0.000001) {
  135.         x2 = k * cos(x2);
  136.         n2++;
  137.     }
  138.     printf("\n1%15s%.8lf%7s%d", "", x2, "", n2);
  139.  
  140.     float *res2;
  141.     res2 = func3(-5, 5, 5);
  142.     printf("\n2%15s%.8f%7s%d", "", res2[0], "", (int) res2[1]);
  143.  
  144.     x2 = 0;
  145.     n2 = 0;
  146.  
  147.     while (fabs(x2 - k * cos(x2)) > delta) {
  148.         x2 = x2 - ((x2 - k * cos(x2)) / (1 + k * sin(x2)));
  149.         n2++;
  150.     }
  151.     printf("\n3%15s%.8lf%7s%d", "", x2, "", n2);
  152.  
  153.  
  154.     printf("\n\nУравнение: x - k * cos(x) = 0, k = 10. Погрешность: %f", delta);
  155.     printf("\n № метода%8sx%15s№", "", "");
  156.  
  157.     double x3 = 0;
  158.     int n3 = 0;
  159.     double k1 = 10;
  160.  
  161.     while (fabs(x3 - k1 * cos(x3)) > 0.000001) {
  162.         x3 = k * cos(x3);
  163.         n3++;
  164.     }
  165.     printf("\n1%15s%.8lf%7s%d", "", x3, "", n3);
  166.  
  167.  
  168.     float *res3;
  169.     res3 = func3(-10, 10, 10);
  170.     printf("\n2%15s%.8f%7s%d", "", res3[0], "", (int) res3[1]);
  171.  
  172.     x3 = 0;
  173.     n3 = 0;
  174.  
  175.     while (fabs(x3 - k1 * cos(x3)) > delta) {
  176.         x3 = x3 - ((x3 - k1 * cos(x3)) / (1 + k1 * sin(x3)));
  177.         n3++;
  178.     }
  179.     printf("\n3%15s%.8lf%7s%d", "", x3, "", n3);
  180.  
  181.     return 0;
  182. }
  183.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement