Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.15 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, 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.         y_value = 0;
  123.         for (j = 0; j < n ; j++) { //убрал n+1 потому что вылезал за границы памяти но поч так хз
  124.             cout << "x: " << x_value << " ";
  125.             cout << polynom_koeff[j] << " ";
  126.             cout << "x: ^  " << get_degree(x_value, j) << " ";  //Чтобы не умножать ахуенно маленькое число на икс в большой степени надо знать какой степени у нас многочлен , а то e^-17 умножаем на x^10
  127.             y_value += polynom_koeff[j] * get_degree(x_value, j); //На 0 индексе стоит свободные член, умножаем на икс в 0 стпени = 1
  128.             cout << y_value << endl << endl << endl;
  129.            
  130.         }
  131.         fout << y_value << endl;
  132.  
  133.     }
  134.  
  135.     fout.close();
  136. }
  137.  
  138.  
  139.  
  140.  
  141.  
  142. void PolynomLG(Node* Array, int n)
  143. {
  144.     int i, j, l, p, z, c;
  145.     double* massiv2 = new double[n]; // массив А0...An конечных коэфф для одного
  146.                                      // шага цикла сколько точек, столько и коэфф
  147.     double* massiv = new double[n - 1]; //массив X0,,без Хi,Хn домноженных на коэфф.  ПОЭТОМУ Н-1
  148.     double* konmas = new double[n]; // будет коннечный массив коэфф      
  149.     double k;
  150.     for (j = 0; j < n; j++)
  151.     {
  152.         konmas[j] = 0.0;
  153.     }
  154.     for (i = 0; i < n; i++)
  155.     {
  156.         c = 0;
  157.         for (j = 0; j < n - 1; j++)
  158.         {
  159.             massiv[j] = 0.0;
  160.             massiv2[j] = 0.0;
  161.         }
  162.         massiv2[n - 1] = 0.0;
  163.         k = Array[i].y;
  164.         for (p = 0; p < n; p++)
  165.         {
  166.             if (p != i)
  167.             {
  168.                 k *= 1.0 / (Array[i].x - Array[p].x);
  169.                 massiv[c] = 0.0 - Array[p].x;
  170.                 c++;
  171.             }
  172.         }
  173.         cout << endl;
  174.         get_koef_polynom(massiv, massiv2, n - 1, n - 1);
  175.         for (l = 0; l < n; l++)
  176.         {
  177.             konmas[l] += massiv2[l] * k;
  178.         }
  179.         cout << "konmas  ";
  180.         for (z = 0; z < n; z++)
  181.         {
  182.             cout << konmas[z] << ' ';
  183.         }
  184.     }
  185.  
  186.     table_in_file(Array, n, konmas); //                      Файл не создает(((
  187.     cout << endl;
  188. }
  189. double DividedDifference(int i, Node* Array)
  190. { // Разделенная разность
  191.     double DD = 0;
  192.     for (int j = 0; j <= i; j++)
  193.     {
  194.         double tmp = 1;
  195.         for (int k = 0; k <= i; k++)
  196.         {
  197.             if (k != j)
  198.                 tmp *= Array[j].x - Array[k].x;
  199.         }
  200.         DD += Array[j].y / tmp;
  201.     }
  202.  
  203.     return DD;
  204. }
  205.  
  206. void PolynomN(Node* Array, int n)
  207. {
  208.     int j;
  209.     double dd;
  210.     double* massiv2 = new double[n]; // массив А0...An конечных коэфф для одного
  211.                                      // шага цикла сколько точек, столько и коэфф
  212.     double* massiv = new double[n - 1]; //массив X0,,,Хn                 +1 для зануления
  213.     double* konmas = new double[n]; // будет коннечный массив коэфф
  214.     int i, c;
  215.  
  216.     for (int j = 0; j < n - 1; j++)
  217.     {
  218.         konmas[j] = 0.0;
  219.         massiv[j] = 0.0;
  220.     }
  221.  
  222.     konmas[n - 1] = 0.0;
  223.  
  224.     for (int j = 0; j < n - 1; j++)
  225.     { //Кладем все иксы
  226.         massiv[j] = 0.0 - Array[j].x;
  227.     }
  228.  
  229.     konmas[0] = Array[0].y; // 0 индекс хранит свободный член итого многочлена
  230.  
  231.     for (i = 1; i < n; i++)
  232.     {
  233.         for (j = 0; j < n; j++)
  234.         {
  235.             massiv2[j] = 0.0;
  236.         }
  237.  
  238.         dd = DividedDifference(i, Array); //Ищем итую РР
  239.         cout << "DD " << dd << endl << endl;
  240.         get_koef_polynom(massiv, massiv2, n - 1,
  241.             i); //Возвращает кф при перемножении (х-Х0)...(х-Хi+1),
  242.         for (j = 0; j < n; j++)
  243.         {
  244.             konmas[j] += massiv2[j] * dd;
  245.         }
  246.         cout << endl;
  247.         for (j = 0; j < n; j++)
  248.         {
  249.             cout << konmas[j] << "   ";
  250.         }
  251.         cout << endl;
  252.     }
  253.  
  254.     for (j = 0; j < n; j++)
  255.     {
  256.         cout << konmas[j] << "   ";
  257.     }
  258.     cout << endl;
  259.     table_in_file(Array, n, konmas);
  260.    
  261. }
  262.  
  263. double DFunc(functiontype* func, double x,
  264.     int n) //возварщает проивзодную нго порядка как константу типа дабл
  265. { // Производная функции
  266.     double h = 0.00001;
  267.     if (n == 1)
  268.     {
  269.         return ((*func)(x + h) - (*func)(x)) / h;
  270.     }
  271.     else
  272.     {
  273.         return (DFunc(func, x + h, n - 1) - DFunc(func, x, n - 1)) / h;
  274.     }
  275. }
  276.  
  277. double test(functiontype* func, double x, int n, long double h)
  278. { // Производная функции
  279.     h = 0.0001;
  280.     while (n > 1)
  281.     {
  282.         return (test(func, x + h, n - 1, h / pow(10, -2))
  283.             - test(func, x - h, n - 1, h / pow(10, -2)))
  284.             / 2 * h;
  285.     }
  286.     if (n == 1)
  287.     {
  288.         return ((*func)(x + h) - (*func)(x - h)) / 2 * h;
  289.     }
  290. }
  291.  
  292. double W(double x, int n, Node* Array)
  293. { // Полином вида: (x - x1) * (x - x2) * ... * (x - xn)
  294.     double w = 1;
  295.     for (int i = 1; i <= n; i++)
  296.     {
  297.         w *= x - Array[i].x;
  298.     }
  299.     return w;
  300. }
  301.  
  302. int main()
  303. {
  304.  
  305.  
  306.  
  307.     Interval Interval;
  308.     int CountNodes;
  309.     functiontype Func = &Myfunk;
  310.  
  311.     cout << "Enter the interval: " << endl;
  312.     cin >> Interval.InitialNode >> Interval.EndNode;
  313.     cout << "Enter the number of nodes: " << endl;
  314.     cin >> CountNodes;
  315.  
  316.     //cout << DFunc(&Func, 1.00, 2) << endl;
  317.     //cout << test(&Func, 1.00, 2, 0.0001);
  318.  
  319.    
  320.    
  321.  
  322.      Node *ArrayUniformNodes = new Node[CountNodes]; // Массив с равномерными // узлами
  323.  
  324.      
  325.  
  326.  
  327.     //Node* ArrayChebyshevNodes = new Node[CountNodes]; // Массив с Чебышевскими узлами
  328.  
  329.     //Node *ArrayformNodes2 = new Node[100]; //Массив где будет много точек для построение ориг графика
  330.  
  331.     ValueUniformTable(&Func, ArrayUniformNodes, Interval.InitialNode, Interval.EndNode, CountNodes); // Заполнение таблицы равномерных значений
  332.  
  333.     //ValueChebyshevTable(&Func, ArrayChebyshevNodes, Interval.InitialNode, Interval.EndNode, CountNodes); // Заполнение таблицы Чебышевских значений
  334.  
  335.     cout << endl;
  336.     //PolynomLG(ArrayUniformNodes, CountNodes);
  337.     cout << endl;
  338.     PolynomN(ArrayUniformNodes, CountNodes);
  339.  
  340.     system("pause");
  341.     return 0;
  342. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement