Advertisement
Guest User

Untitled

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