Advertisement
gashink_t

interpol_aitcen_bewton_lagranz(lab_6,7,8, vm)

Mar 30th, 2021
621
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.70 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. using namespace std;
  4. #define N 4
  5.  
  6. float f(float x)
  7. {
  8.     return x * x;
  9. }
  10.  
  11. void print(float X[N], float Y[N]);
  12. float lagranz(float x[N], float y[N], float X);
  13. float aitken(float x[N], float y[N], float X);
  14. float newton(float x[N], float y[N], float X);
  15.  
  16. int main()
  17. {
  18.     int c = 1;
  19.     float x;
  20.     float X[N] = { 1, 2, 3, 4 };
  21.     float Y[N];
  22.     for (int i = 0; i < N; i++)
  23.     {
  24.         Y[i] = f(X[i]);
  25.     }
  26.     print(X, Y);
  27.  
  28.     cout << "\n\nEnter the value of the X point:\nX = ";
  29.     cin >> x;
  30.  
  31.     while (c)
  32.     {
  33.         cout << "\nSelect a method: Lagranz(1), Aitken(2), Newton(3), the end(0)\n->";
  34.         cin >> c;
  35.         switch (c)
  36.         {
  37.         case 0:
  38.             break;
  39.         case 1:
  40.             cout << "\nMethos Lagranz\n\t\tAnswer:\tP(" << x << ") = " << lagranz(X, Y, x) << "\n\n";
  41.             break;
  42.         case 2:
  43.             cout << "\nMethos Aitken\n\t\tAnswer:\tP(" << x << ") = " << aitken(X, Y, x) << "\n\n";
  44.             break;
  45.         case 3:
  46.             cout << "\nMethos Newton\n\t\tAnswer:\tP(" << x << ") = " << newton(X, Y, x) << "\n\n";
  47.             break;
  48.         }
  49.     }
  50.    
  51.     return 0;
  52. }
  53.  
  54. void print(float X[N], float Y[N])
  55. {
  56.     cout << "Table:\nX|";
  57.     for (int i = 0; i < N; i++)
  58.     {
  59.         cout << setw(10) << X[i];
  60.     }
  61.     cout << "\nY|";
  62.     for (int i = 0; i < N; i++)
  63.     {
  64.         cout << setw(10) << Y[i];
  65.     }
  66. }
  67.  
  68. float lagranz(float x[N], float y[N], float X)
  69. {
  70.     float answer = 0, ch, zn;
  71.     for (int j = 0; j < N; j++) {
  72.         ch = 1; zn = 1;
  73.         for (int i = 0; i < N; i++) {
  74.             if (i != j)
  75.             {
  76.                 ch *= (X - x[i]);
  77.                 zn *= (x[j] - x[i]);
  78.             }
  79.         }
  80.         answer += y[j] * ch / zn;
  81.     }
  82.  
  83.     return answer;
  84. }
  85.  
  86. float aitken(float x[N], float y[N], float X)
  87. {
  88.     float P[N];
  89.     for (int i = 0; i < N; i++)
  90.     {
  91.         P[i] = y[i];
  92.     }
  93.  
  94.     for (int k = 1; k < N; k++)
  95.     {
  96.         for (int i = 0; i < N - k; i++)
  97.         {
  98.             P[i] = (P[i] * (x[i + k] - X) - P[i + 1] * (x[i] - X)) / (x[i + k] - x[i]);
  99.         }
  100.         P[N - k] = 0;
  101.     }
  102.  
  103.     return P[0];
  104. }
  105.  
  106. float newton(float x[N], float y[N], float X)
  107. {
  108.     float answer = y[0], a, h;
  109.     for (int i = 1; i < N; i++)
  110.     {
  111.         a = 0;
  112.         for (int j = 0; j <= i; j++)
  113.         {
  114.             h = 1;
  115.             for (int k = 0; k <= i; k++)
  116.             {
  117.                 if (k != j)
  118.                     h *= (x[j] - x[k]);
  119.             }
  120.             a += y[j] / h;
  121.         }
  122.         for (int j = 0; j < i; j++)
  123.             a *= (X - x[j]);
  124.         answer += a;
  125.     }
  126.     return answer;
  127. }
  128.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement