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