vadimk772336

Untitled

Mar 9th, 2020
568
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.36 KB | None | 0 0
  1. /*
  2. plot "D:\spline_graphic.txt" with lines title "Сплайн", "D:\original_graphic.txt" with lines title "График функции"
  3. plot "D:\difference_graphic.txt" with lines title "Разница"
  4. */
  5. #include <iostream>
  6. #include <math.h>
  7. #include <cmath>
  8. #include <vector>
  9. #include <Windows.h>
  10. #include <stdlib.h>
  11. #include <fstream>
  12. #include <string.h>
  13. #include <time.h>
  14. #include <iomanip>
  15. using namespace std;
  16. const double PI = 3.141592653589793238463;
  17.  
  18. //Для хранения сетки
  19. typedef struct Node
  20. {
  21.     double x, y;
  22. }
  23. Node;
  24.  
  25. //Задание функции
  26. typedef double(*functiontype)(double x);
  27. double Myfunc(double x)
  28. {
  29.     return x * x*x;
  30.     //return x;
  31.     //return x;
  32. }
  33. functiontype Func = &Myfunc;
  34.  
  35. //Возведение числа в натуральную степень
  36. double degree(double x, int n)
  37. {
  38.     double b = x;
  39.     for (int i = 1; i < n; i++)
  40.         x *= b;
  41.     return x;
  42. }
  43.  
  44. //Приблизительное значение 2 производной
  45. double approximate_derivative_2(functiontype* f, double x)
  46. {
  47.     double delta = 1e-5;
  48.     return (((*f)(x) - 2 * (*f)(x + delta) + (*f)(x + 2 * delta)) / (delta * delta));
  49. }
  50.  
  51. //Приблизительное значение 1 производной
  52. double approximate_derivative_1(functiontype* f, double x)
  53. {
  54.     double delta = 1e-5;
  55.     return (((*f)(x + delta) - (*f)(x)) / delta);
  56. }
  57.  
  58. //Возвращает значение сплайна в точке на итервале[xi,x_i+1]
  59. double Spline(double x, int i, double* Array_steps, double* x_massiv, Node* Array)
  60. {
  61.     double h2, g1, g2, x1, x2, y1, y2;
  62.  
  63.     h2 = Array_steps[i + 1];
  64.     g1 = x_massiv[i];
  65.     g2 = x_massiv[i + 1];
  66.     x1 = Array[i].x;
  67.     x2 = Array[i + 1].x;
  68.     y1 = Array[i].y;
  69.     y2 = Array[i + 1].y;
  70.  
  71.     return (
  72.         (y1 * (x2 - x) + y2 * (x - x1)) / h2 +
  73.         (g1 * (degree((x2 - x), 3) - h2 * h2 * (x2 - x))) +
  74.         (g2 * (degree((x - x1), 3) - h2 * h2 * (x - x1)) / (6 * h2))
  75.         );
  76. }
  77.  
  78. //Равномерная сетка
  79. void ValueUniformTable(functiontype* f, Node* Array, double Initial, double End, int CountSegments, double* Array_steps_uni)
  80. {
  81.     double h;
  82.     Array[0].x = Initial;
  83.     Array[0].y = (*f)(Initial);
  84.     Array_steps_uni[0] = 0;
  85.     h = abs(End - Initial) / CountSegments;
  86.  
  87.     for (int i = 1; i <= CountSegments; i++)
  88.     {
  89.         Array[i].x = Array[i - 1].x + h;
  90.         Array[i].y = (*f)(Array[i].x);
  91.         Array_steps_uni[i] = h;
  92.     }
  93. }
  94.  
  95. //Чебышёвская сетка
  96. void ValueChebyshevTable(functiontype* f, Node* Array, double Initial, double End, int CountSegments, double* Array_steps_cheb)
  97. {
  98.     Array_steps_cheb[0] = 0;
  99.     Array[0].x = Initial;
  100.     Array[0].y = (*f)(Array[0].x);
  101.  
  102.     for (int i = 1; i < CountSegments; i++)
  103.     {
  104.         Array[i].x = -((End + Initial) / 2) -
  105.             ((End - Initial) / 2) * cos(((2 * i + 1) * PI) / (2 * (CountSegments + 1)));
  106.         Array[i].y = (*f)(Array[i].x);
  107.         Array_steps_cheb[i] = Array[i].x - Array[i - 1].x;
  108.     }
  109.  
  110.     Array[CountSegments].x = End;
  111.     Array[CountSegments].y = (*f)(Array[CountSegments].x);
  112.     Array_steps_cheb[CountSegments] = Array[CountSegments].x - Array[CountSegments - 1].x;
  113. }
  114.  
  115. //Составляем СЛАУ с учётом нач данных и закидываем это всё в один массив matrix_coeffs - массив вида[[a1,b1,c1,d1], [a2,b2,c2,d2],..., [an,bn,cn,dn]]
  116. void get_matrix_coeffs(double Initial, double End, int CountSegments, functiontype* f, double** matrix_coeffs, Node* Array, double* Array_steps)
  117. {
  118.     int i;
  119.     double h1, h2, A, B, y1, y2;
  120.     int matrix_size = CountSegments + 1;
  121.  
  122.     A = approximate_derivative_2(&Func, Array[0].x);
  123.     B = approximate_derivative_1(&Func, Array[CountSegments].x);
  124.  
  125.     //первое уравнение
  126.     matrix_coeffs[0][0] = 0;
  127.     matrix_coeffs[0][1] = 1;
  128.     matrix_coeffs[0][2] = 0;
  129.     matrix_coeffs[0][3] = A;
  130.  
  131.     for (i = 1; i < matrix_size - 1; i++)
  132.     {
  133.         h1 = Array_steps[i];
  134.         h2 = Array_steps[i + 1];
  135.         matrix_coeffs[i][0] = h1;
  136.         matrix_coeffs[i][1] = 2 * (h1 + h2);
  137.         matrix_coeffs[i][2] = h2;
  138.         matrix_coeffs[i][3] = 6 * ((Array[i + 1].y - Array[i].y) / h2 - (Array[i].y - Array[i - 1].y) / h1);
  139.     }
  140.  
  141.     //n+1 уравнение
  142.     h2 = Array_steps[matrix_size - 1];
  143.     y1 = Array[matrix_size - 2].y;
  144.     y2 = Array[matrix_size - 1].y;
  145.     matrix_coeffs[matrix_size - 1][0] = 0;
  146.     matrix_coeffs[matrix_size - 1][1] = 2;
  147.     matrix_coeffs[matrix_size - 1][2] = 1;
  148.     matrix_coeffs[matrix_size - 1][3] = 6 * (y2 - y1 - B * h2) / (h2 * h2);
  149.  
  150.  
  151.     for (i = 0; i < matrix_size; i++) {
  152.         cout << "(" << matrix_coeffs[i][0] << ")" << "  " << "(" << matrix_coeffs[i][1] << ")" << "  " << "(" << matrix_coeffs[i][2] << ")" << "  = " << matrix_coeffs[i][3] << endl;
  153.     }
  154.  
  155. }
  156.  
  157. /*Принимает на вход коэффициенты СЛАУ в виде массива matrix_coeffs - массив вида[[a1,b1,c1,d1], [a2,b2,c2,d2],..., [an,bn,cn,dn]].
  158.    Заполняет Двумерный массив прогоночных коэффициентов coeffs_massiv (0 - Кси, 1 - Эта). Находит решение в виде массива x_massiv*/
  159. void tridiagonal_matrix_algorithm(double** matrix_coeffs, double* Array_steps, Node* Array, int CountSegments, double* x_massiv)
  160. {
  161.     int i;
  162.     double denominator;
  163.     double K, E, K_prev = 0, E_prev = 0;
  164.     int matrix_size = CountSegments + 1;
  165.  
  166.     double** coeffs_massiv;
  167.     coeffs_massiv = new double*[matrix_size + 1];
  168.     for (i = 0; i < matrix_size+1; i++)
  169.         coeffs_massiv[i] = new double[2];
  170.  
  171.     //Прямой ход
  172.     coeffs_massiv[0][0] = 0;
  173.     coeffs_massiv[0][1] = 0;
  174.  
  175.     for (i = 0; i <= matrix_size - 1; i++)
  176.     {
  177.         denominator = matrix_coeffs[i][0] * K_prev + matrix_coeffs[i][1];
  178.         K = -(matrix_coeffs[i][2]) / denominator;
  179.         E = (matrix_coeffs[i][3] - matrix_coeffs[i][0] * E_prev) / denominator;
  180.         K_prev = K;
  181.         E_prev = E;
  182.         coeffs_massiv[i + 1][0] = K;
  183.         coeffs_massiv[i + 1][1] = E;
  184.     }
  185.  
  186.     //Обратный ход
  187.     x_massiv[matrix_size - 1] = coeffs_massiv[matrix_size][1];  //определяю xn = e_n+1
  188.     for (i = matrix_size - 2; i >= 0; i--)
  189.     {
  190.         x_massiv[i] = coeffs_massiv[i + 1][0] * x_massiv[i + 1] + coeffs_massiv[i + 1][1];
  191.     }
  192.  
  193.     cout << endl << "Размер матрицы = " << matrix_size << endl;
  194.     for (i = 0; i < matrix_size; i++)
  195.         cout << "g_ " << i << ": " << x_massiv[i] << endl;
  196.  
  197.     cout << endl;
  198.     for (i = 0; i <= matrix_size; i++)
  199.         cout << "e,k: " << i << ": " << coeffs_massiv[i][0] << " " << coeffs_massiv[i][1] << endl;
  200. }
  201.  
  202. //Запись в 3 файла значений сплайна, интерполируемой функции и их разницы в соответственных точках
  203. void tables_in_file(functiontype* f, Node* Array, int Count_dots, int Count_Segments, double Initial, double End, double* Array_steps, double* x_massiv)
  204. {
  205.     int i = 0;
  206.     double step = (End - Initial) / (Count_dots - 1), y_value1, y_value2, x_value;
  207.  
  208.     ofstream fout1("D:/spline_graphic.txt");
  209.     ofstream fout2("D:/original_graphic.txt");
  210.     ofstream fout3("D:/difference_graphic.txt");
  211.     x_value = Initial;
  212.     for (i = 0; i < Count_Segments; i++)
  213.     {
  214.         while (x_value < Array[i + 1].x)
  215.         {
  216.             fout1 << x_value << " ";
  217.             fout2 << x_value << " ";
  218.             fout3 << x_value << " ";
  219.             y_value1 = Spline(x_value, i, Array_steps, x_massiv, Array);
  220.             y_value2 = (*f)(x_value);
  221.             fout1 << y_value1 << endl;
  222.             fout2 << y_value2 << endl;
  223.             fout3 << abs(y_value2 - y_value1) << endl;
  224.             x_value += step;
  225.         }
  226.     }
  227.     fout1 << End << " ";
  228.     fout2 << End << " ";
  229.     fout1 << Spline(End, Count_Segments - 1, Array_steps, x_massiv, Array) << endl;
  230.     fout2 << (*f)(End) << endl;
  231.     fout1.close();
  232.     fout2.close();
  233.     cout << endl << "Запись в файл осуществлена!" << endl;
  234. }
  235.  
  236.  
  237. int main()
  238. {
  239.     setlocale(LC_ALL, "RUS");
  240.     functiontype Func = &Myfunc;
  241.     int CountSegments, i, Countdots = 15000;
  242.  
  243.     /*---------------------------------Входные данные-------------------------------------*/
  244.     double Initial = -5, End = 5;
  245.     cout << "Введите N: ";
  246.     cin >> CountSegments;
  247.  
  248.     /*---------------------------------Сетки-------------------------------------*/
  249.     double* Array_steps_cheb = new double[CountSegments + 1];
  250.     double* Array_steps_uni = new double[CountSegments + 1];
  251.  
  252.     //Равномерная сетка
  253.     Node* ArrayUniformNodes = new Node[CountSegments + 1];
  254.     ValueUniformTable(&Func, ArrayUniformNodes, Initial, End, CountSegments, Array_steps_uni);
  255.     //Чебышёв
  256.     Node* ArrayChebyshevNodes = new Node[CountSegments + 1];
  257.     ValueChebyshevTable(&Func, ArrayChebyshevNodes, Initial, End, CountSegments, Array_steps_cheb);
  258.  
  259.     /*---------------------------------СЛАУ - matrix_coeffs -------------------------------------*/
  260.     double** matrix_coeffs;
  261.     matrix_coeffs = new double*[CountSegments+1];
  262.     for (i = 0; i < CountSegments+1; i++)
  263.         matrix_coeffs[i] = new double[3];
  264.  
  265.     //cout << endl << "Полученная матрица: " << endl;
  266.     get_matrix_coeffs(Initial, End, CountSegments, &Func, matrix_coeffs, ArrayUniformNodes, Array_steps_uni);
  267.  
  268.     /*---------------------------------Прогонка - запись в x_massiv -------------------------------------*/
  269.     //Получение ответа в x_massiv
  270.     double* x_massiv = new double[CountSegments + 1];
  271.     tridiagonal_matrix_algorithm(matrix_coeffs, Array_steps_uni, ArrayUniformNodes, CountSegments, x_massiv);
  272.  
  273.     /*---------------------------------Графики -------------------------------------*/
  274.     //Запись значений сплайна и интерполируемой функции в файлы соответсвенно
  275.     //tables_in_file(&Func, ArrayUniformNodes, Countdots, CountSegments, Initial, End, Array_steps_uni, x_massiv);
  276.  
  277.     /*---------------------------------Вывод в консоль значений -------------------------------------*/
  278.    
  279.     //Шаги
  280.     cout << endl;
  281.     for (i = 1; i <= CountSegments; i++) {
  282.         cout << "h_" << i << ": " << Array_steps_uni[i] << endl;
  283.     }
  284.  
  285.     cout << endl << "Cетка:  " << endl;
  286.         for (int i = 0; i < CountSegments + 1; i++)
  287.     cout << "(" << ArrayUniformNodes[i].x << ":" << ArrayUniformNodes[i].y << ")" << endl;
  288.  
  289.  
  290.     cout << endl;
  291.     for (i = 0; i < CountSegments; i++)
  292.         cout << "Spline in x_" << i << " " << Spline(ArrayUniformNodes[i].x, i, Array_steps_uni, x_massiv, ArrayUniformNodes) << endl;
  293.     cout << "Spline in x_" << CountSegments << " " << Spline(ArrayUniformNodes[CountSegments].x, CountSegments - 1, Array_steps_uni, x_massiv, ArrayUniformNodes) << endl;
  294.    
  295.  
  296.     cout << endl;
  297.     system("pause");
  298.     return 0;
  299. }
Advertisement
Add Comment
Please, Sign In to add comment