Advertisement
chuuupa

checkfile

Nov 30th, 2018
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.64 KB | None | 0 0
  1. #define _USE_MATH_DEFINES
  2.  
  3. #include "pch.h"
  4. #include <vector>
  5. #include <cmath>
  6. #include <iostream>
  7.  
  8. using namespace std;
  9.  
  10. const int n = 8; // степень многочлена
  11.  
  12. const int a = -2, b = 1; // границы интервала
  13. double h = static_cast<double>(b - a) / n; // приращение узлов интерполяции
  14.  
  15. #define expr (1+x+x*x)/(1+x*x+x*x*x*x) // исходная функция
  16.  
  17. double* xCoords = new double[n + 1], // создание массивов узлов интерполяции и значений выражения в них
  18. *yCoords = new double[n + 1];
  19.  
  20. void createCoords() { // заполнение массивов
  21.  
  22.     int i = 0;
  23.     for (double x = a; x <= b; x += h) {
  24.         *(xCoords + i) = x;
  25.         *(yCoords + i) = expr;
  26.         i++;
  27.     }
  28. }
  29.  
  30. double* massD = new double[n + 1]; // массив коэфицентов d
  31.  
  32. void createMassD() { // и тут заполнение
  33.  
  34.     *(massD + 0) = *(yCoords + 0); // d0 =  f(x0)
  35.  
  36.     for (int j = 1; j <= n; j++)
  37.         for (int i = 0; i <= n - j; i++) {
  38.  
  39.             *(yCoords + i) = (*(yCoords + i + 1) - *(yCoords + i)) / (*(xCoords + i + j) - *(xCoords + i)); // магия копипасты
  40.             *(massD + j) = *(yCoords + 0);
  41.         }
  42. }
  43.  
  44. void createMassD_() { // и тут заполнение
  45.  
  46.     massD[0] = yCoords[0]; // d0 = f(x0)
  47.  
  48.     vector<vector<double> > vec(n + 1, vector<double>(n + 1, 0));
  49.  
  50.     int t = 1, k = n - 1;
  51.     for (int i = 0; i <= n; i++)
  52.         vec[0][i] = yCoords[i];
  53.     for (int i = 1; i <= n; i++) {
  54.         for (int j = 0; j <= k; j++)
  55.             vec[i][j] = (vec[i - 1][j] - vec[i - 1][j + 1]) / (xCoords[j] - xCoords[j + t]);
  56.         t++;
  57.         k--;
  58.     }
  59.     for (int i = 1; i <= n; i++)
  60.         massD[i] = vec[i][0];
  61. }
  62.  
  63. void createMassD__() { // и тут заполнение
  64.  
  65.     double y = 1.0, u = 0.0;
  66.     int e = 1;
  67.  
  68.     massD[0] = yCoords[0]; // d0 = f(x0)
  69.  
  70.     for (int m = 1; m <= n; m++) {
  71.         for (int i = 0; i <= m; i++) {
  72.             for (int j = 0; j <= m, j != i; j++)
  73.                 y *= 1 / (xCoords[i] - xCoords[j]);
  74.             y *= yCoords[i];
  75.             u += y;
  76.             y = 1.0;
  77.         }
  78.         massD[e] = u;
  79.         u = 0.0;
  80.         e++;
  81.     }
  82. }
  83. double newton(double x_) { // тут считаем значения Pn(x)
  84.  
  85.     double value = *(massD + 0), p = 1;
  86.  
  87.     for (int i = 1; i < n; i++) {
  88.  
  89.         p = p * (x_ - *(xCoords + i - 1));
  90.         value += *(massD + i) * p;
  91.     }
  92.     return value;
  93. }
  94. int main() {
  95.    
  96.     createCoords();
  97.  
  98.     createMassD();
  99.  
  100.     for (int i = 0; i <= n; i++)
  101.         cout << massD[i] << " ";
  102.     cout << "\n\n";
  103.  
  104.     createMassD_();
  105.  
  106.     for (int i = 0; i <= n; i++)
  107.         cout << massD[i] << " ";
  108.     cout << "\n\n";
  109.  
  110.     createMassD__();
  111.  
  112.     for (int i = 0; i <= n; i++)
  113.         cout << massD[i] << " ";
  114.     cout << "\n\n";
  115.  
  116.     return 0;
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement