vadimk772336

Untitled

Oct 14th, 2019
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.96 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <iostream>
  3. #include <math.h>
  4. #include <vector>
  5. #include <Windows.h>
  6. #include <stdlib.h>
  7.  
  8. using namespace std;
  9.  
  10. const double PI = 3.1415926535897932384626433832795;
  11.  
  12. typedef double(*functiontype)(double x);
  13.  
  14. typedef struct Node
  15. {
  16.     double x, y;
  17. } Node;
  18.  
  19. typedef struct Interval
  20. {
  21.     double InitialNode, EndNode;
  22. } Interval;
  23.  
  24. int Factorial(int n)
  25. { // Факториал
  26.     int x = 1;
  27.     for (int i = 1; i <= n; i++)
  28.     {
  29.         x *= i;
  30.     }
  31.     return x;
  32. }
  33.  
  34. double Myfunk(double x)
  35. {
  36.     return (1 / (1 + x * x));
  37. }
  38.  
  39. double exponenta(double x)
  40. { // Экспонента
  41.     return exp(x);
  42. }
  43.  
  44. void ValueUniformTable(functiontype* f, Node* Array, double Initial, double End, int Count)
  45. { // Создание равномерной таблицы значений
  46.     double step = abs(Initial - End) / (Count - 1);
  47.     Array[0].x = Initial;
  48.     Array[0].y = (*f)(Array[0].x);
  49.     cout << Array[0].x << "      " << Array[0].y << endl;
  50.     for (int i = 1; i < Count; i++)
  51.     {
  52.         Array[i].x = Array[i - 1].x + step;
  53.         Array[i].y = (*f)(Array[i].x);
  54.         cout << Array[i].x << "      " << Array[i].y << endl;
  55.     }
  56. }
  57.  
  58. void ValueChebyshevTable(functiontype* f, Node* Array, double Initial, double End, int Count)
  59. { // Создание таблицы Чебышевских значений
  60.     for (int i = 0; i < Count; i++)
  61.     {
  62.         Array[i].x = ((End + Initial) / 2)
  63.             + ((End - Initial) / 2) * cos(((2 * i + 1) * PI) / (2 * (Count + 1)));
  64.         Array[i].y = (*f)(Array[i].x);
  65.     }
  66. }
  67.  
  68. void get_koef_polynom(double* mas, double* res, int n, int k)
  69. { // Функция на вход берет массив из n-1 точек и
  70.     // возвращает кф полином получившегося при
  71.     // раскрытии скобок !!!(И еще надо домножить на
  72.     // игрик)!!!
  73.     int i;
  74.     double* anx = new double[n + 5];
  75.     double* ann = new double[n + 5];
  76.     res[0] = mas[0];
  77.     res[1] = 1;
  78.     int j;
  79.     for (j = 0; j <= n; j++)
  80.     {
  81.         anx[j] = 0.0;
  82.     }
  83.  
  84.     for (j = 0; j < n; j++)
  85.     {
  86.         ann[j] = 0.0;
  87.     }
  88.     for (i = 1; i < k; i++) //считаем коэффициенты
  89.         for (j = 0; j <= i + 1; j++)
  90.         {
  91.             anx[j + 1] = res[j];
  92.             ann[j] = res[j] * mas[i];
  93.             res[j] = anx[j] + ann[j];
  94.         }
  95. }
  96.  
  97. void PolynomLG(Node* Array, int n)
  98. {
  99.     int i, j, l, p, z, c;
  100.     double* massiv2 = new double[n]; // массив А0...An конечных коэфф для одного
  101.     // шага цикла сколько точек, столько и коэфф
  102.     double* massiv = new double[n - 1]; //массив X0,,без Хi,Хn домноженных на коэфф.  ПОЭТОМУ Н-1
  103.     double* konmas = new double[n]; // будет коннечный массив коэфф
  104.     double k;
  105.     for (j = 0; j < n; j++)
  106.     {
  107.         konmas[j] = 0.0;
  108.     }
  109.     for (i = 0; i < n; i++)
  110.     {
  111.         c = 0;
  112.         for (j = 0; j < n - 1; j++)
  113.         {
  114.             massiv[j] = 0.0;
  115.             massiv2[j] = 0.0;
  116.         }
  117.         massiv2[n - 1] = 0.0;
  118.         k = Array[i].y;
  119.         for (p = 0; p < n; p++)
  120.         {
  121.             if (p != i)
  122.             {
  123.                 k *= 1.0 / (Array[i].x - Array[p].x);
  124.                 massiv[c] = 0.0 - Array[p].x;
  125.                 c++;
  126.             }
  127.         }
  128.         cout << endl;
  129.         get_koef_polynom(massiv, massiv2, n - 1, n - 1);
  130.         for (l = 0; l < n; l++)
  131.         {
  132.             konmas[l] += massiv2[l] * k;
  133.         }
  134.         cout << "konmas  ";
  135.         for (z = 0; z < n; z++)
  136.         {
  137.             cout << konmas[z] << ' ';
  138.         }
  139.     }
  140.     cout << endl;
  141. }
  142. double DividedDifference(int i, Node* Array)
  143. { // Разделенная разность
  144.     double DD = 0;
  145.     for (int j = 0; j <= i; j++)
  146.     {
  147.         double tmp = 1;
  148.         for (int k = 0; k <= i; k++)
  149.         {
  150.             if (k != j)
  151.                 tmp *= Array[j].x - Array[k].x;
  152.         }
  153.         DD += Array[j].y / tmp;
  154.     }
  155.  
  156.     return DD;
  157. }
  158.  
  159. void PolynomN(Node* Array, int n)
  160. {
  161.     int j;
  162.     double dd;
  163.     double* massiv2 = new double[n]; // массив А0...An конечных коэфф для одного
  164.     // шага цикла сколько точек, столько и коэфф
  165.     double* massiv = new double[n - 1]; //массив X0,,,Хn                 +1 для зануления
  166.     double* konmas = new double[n]; // будет коннечный массив коэфф
  167.     int i, c;
  168.  
  169.     for (int j = 0; j < n - 1; j++)
  170.     {
  171.         konmas[j] = 0.0;
  172.         massiv[j] = 0.0;
  173.     }
  174.  
  175.     konmas[n - 1] = 0.0;
  176.  
  177.     for (int j = 0; j < n - 1; j++)
  178.     { //Кладем все иксы
  179.         massiv[j] = 0.0 - Array[j].x;
  180.     }
  181.  
  182.     konmas[0] = Array[0].y; // 0 индекс хранит свободный член итого многочлена
  183.  
  184.     for (i = 1; i < n; i++)
  185.     {
  186.         for (j = 0; j < n; j++)
  187.         {
  188.             massiv2[j] = 0.0;
  189.         }
  190.  
  191.         dd = DividedDifference(i, Array); //Ищем итую РР
  192.         cout << "DD " << dd << endl << endl;
  193.         get_koef_polynom(massiv, massiv2, n - 1,
  194.             i); //Возвращает кф при перемножении (х-Х0)...(х-Хi+1),
  195.         for (j = 0; j < n; j++)
  196.         {
  197.             konmas[j] += massiv2[j] * dd;
  198.         }
  199.         cout << endl;
  200.         for (j = 0; j < n; j++)
  201.         {
  202.             cout << konmas[j] << "   ";
  203.         }
  204.         cout << endl;
  205.     }
  206.  
  207.     for (j = 0; j < n; j++)
  208.     {
  209.         cout << konmas[j] << "   ";
  210.     }
  211.     cout << endl;
  212. }
  213.  
  214. double DFunc(functiontype* func, double x,
  215.     int n) //возварщает проивзодную нго порядка как константу типа дабл
  216. { // Производная функции
  217.     double h = 0.00001;
  218.     if (n == 1)
  219.     {
  220.         return ((*func)(x + h) - (*func)(x)) / h;
  221.     }
  222.     else
  223.     {
  224.         return (DFunc(func, x + h, n - 1) - DFunc(func, x, n - 1)) / h;
  225.     }
  226. }
  227.  
  228. double test(functiontype* func, double x, int n, long double h)
  229. { // Производная функции
  230.     h = 0.0001;
  231.     while (n > 1)
  232.     {
  233.         return (test(func, x + h, n - 1, h / pow(10, -2))
  234.             - test(func, x - h, n - 1, h / pow(10, -2)))
  235.             / 2 * h;
  236.     }
  237.     if (n == 1)
  238.     {
  239.         return ((*func)(x + h) - (*func)(x - h)) / 2 * h;
  240.     }
  241. }
  242.  
  243. double W(double x, int n, Node* Array)
  244. { // Полином вида: (x - x1) * (x - x2) * ... * (x - xn)
  245.     double w = 1;
  246.     for (int i = 1; i <= n; i++)
  247.     {
  248.         w *= x - Array[i].x;
  249.     }
  250.     return w;
  251. }
  252.  
  253. int main()
  254. {
  255.     Interval Interval;
  256.     int CountNodes;
  257.     functiontype Func = &Myfunk;
  258.  
  259.     cout << "Enter the interval: " << endl;
  260.     cin >> Interval.InitialNode >> Interval.EndNode;
  261.     cout << "Enter the number of nodes: " << endl;
  262.     cin >> CountNodes;
  263.  
  264.     cout << DFunc(&Func, 1.00, 2) << endl;
  265.     cout << test(&Func, 1.00, 2, 0.0001);
  266.  
  267.     // Node *ArrayUniformNodes = new Node[CountNodes]; // Массив с равномерными
  268.     // узлами
  269.     Node* ArrayChebyshevNodes = new Node[CountNodes]; // Массив с Чебышевскими узлами
  270.  
  271.     // ValueUniformTable(&Func, ArrayUniformNodes, Interval.InitialNode,
  272.     // Interval.EndNode, CountNodes); // Заполнение таблицы равномерных значений
  273.     ValueChebyshevTable(&Func, ArrayChebyshevNodes, Interval.InitialNode, Interval.EndNode,
  274.         CountNodes); // Заполнение таблицы Чебышевских значений
  275.  
  276.     cout << endl;
  277.     // PolynomLG(ArrayChebyshevNodes, CountNodes);
  278.     cout << endl;
  279.     // PolynomN(ArrayChebyshevNodes, CountNodes);
  280.  
  281.     system("pause");
  282.     return 0;
  283. }
Advertisement
Add Comment
Please, Sign In to add comment