vadimk772336

Untitled

Mar 3rd, 2020
522
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.72 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.    Заполняет Двумерный массив прогоночных коэффициентов coeffs_massiv (0 - Кси, 1 - Эта). Находит решение в виде массива x_massiv*/
  117. void tridiagonal_matrix_algorithm(double** matrix_coeffs, double* Array_steps, Node* Array, int CountSegments, double* x_massiv, double B)
  118. {
  119.     int i;
  120.     double denominator;
  121.     double K, E, K_prev = 0, E_prev = 0;    //Прогоночные коэффициенты
  122.     double y1, y2, h2;
  123.     int matrix_size = CountSegments - 1;
  124.  
  125.     double** coeffs_massiv;
  126.     coeffs_massiv = new double*[matrix_size + 1];
  127.     for (i = 0; i <= matrix_size; i++)
  128.         coeffs_massiv[i] = new double[2];
  129.  
  130.     coeffs_massiv[1][0] = -(matrix_coeffs[0][2]) / (matrix_coeffs[0][1]);   //определил К2,Е2
  131.     coeffs_massiv[1][1] = (matrix_coeffs[0][3]) / (matrix_coeffs[0][1]);
  132.  
  133.     //Прямой ход
  134.     for (i = 2; i <= matrix_size; i++)
  135.     {
  136.         denominator = matrix_coeffs[i - 1][0] * K_prev + matrix_coeffs[i - 1][1];
  137.         K = -(matrix_coeffs[i - 1][2]) / denominator;
  138.         E = (matrix_coeffs[i - 1][3] - matrix_coeffs[i - 1][0] * E_prev) / denominator;
  139.         K_prev = K;
  140.         E_prev = E;
  141.         coeffs_massiv[i][0] = K;
  142.         coeffs_massiv[i][1] = E;
  143.     }
  144.  
  145.     //Обратный ход
  146.     x_massiv[matrix_size] = coeffs_massiv[matrix_size][1];  //определяю xn = e_n+1
  147.     for (i = matrix_size - 1; i >= 1; i--)
  148.     {
  149.         x_massiv[i] = coeffs_massiv[i][0] * x_massiv[i + 1] + coeffs_massiv[i][1];
  150.     }
  151.  
  152.     //Определяю gn
  153.     h2 = Array_steps[CountSegments];
  154.     y1 = Array[CountSegments - 1].y;
  155.     y2 = Array[CountSegments].y;
  156.     x_massiv[CountSegments] = 6 * (y2 - y1 - B * h2) / (h2 * h2) - 2 * x_massiv[CountSegments - 1]; //gn
  157.  
  158.    
  159.     /*cout << endl << "Размер матрицы = " << matrix_size << ", Гамм " << matrix_size + 2 << endl;
  160.     for (i = 0; i < matrix_size + 2; i++)
  161.         cout << "g_ " << i << ": " << x_massiv[i] << endl;
  162.    
  163.         cout << endl;
  164.         for (i = 1; i<matrix_size+1; i++)
  165.             cout << "e,k_i " << i << ": " << coeffs_massiv[i][0] << " " << coeffs_massiv[i][1] << endl; */
  166. }
  167.  
  168. //Составляем СЛАУ с учётом нач данных и закидываем это всё в один массив matrix_coeffs - массив вида[[a1,b1,c1,d1], [a2,b2,c2,d2],..., [an,bn,cn,dn]]
  169. void get_matrix_coeffs(double Initial, double End, int CountSegments, functiontype* f, double** matrix_coeffs, Node* Array, double* Array_steps, double B)
  170. {
  171.     int i;
  172.     double h1, h2, h_prev;
  173.     int matrix_size = CountSegments - 1;
  174.  
  175.     for (i = 1; i <= matrix_size; i++)
  176.     {
  177.         h1 = Array_steps[i];
  178.         h2 = Array_steps[i + 1];;
  179.         h_prev = h2;
  180.         matrix_coeffs[i - 1][0] = h1;
  181.         matrix_coeffs[i - 1][1] = 2 * (h1 + h2);
  182.         matrix_coeffs[i - 1][2] = h2;
  183.         matrix_coeffs[i - 1][3] = 6 * ((Array[i + 1].y - Array[i].y) / h2 - (Array[i].y - Array[i - 1].y) / h1);    //Домножили всю СЛАУ на 6 (поэтому тут 6*)
  184.     }
  185.  
  186.     matrix_coeffs[0][0] = 0;    //а1=0
  187.     matrix_coeffs[matrix_size - 1][2] = 0;  //cn=0
  188.  
  189.    
  190.     /*for (i = 0; i < matrix_size; i++) {
  191.         cout << "(" << matrix_coeffs[i][0] << ")" << "  " << "(" << matrix_coeffs[i][1] << ")" << "  " << "(" << matrix_coeffs[i][2] << ")" << "  " << endl;
  192.     }*/
  193.  
  194. }
  195.  
  196. //Запись в 3 файла значений сплайна, интерполируемой функции и их разницы в соответственных точках
  197. void tables_in_file(functiontype* f, Node* Array, int Count_dots, int Count_Segments, double Initial, double End, double* Array_steps, double* x_massiv)
  198. {
  199.     int i = 0;
  200.     double step = (End - Initial) / (Count_dots - 1), y_value1, y_value2, x_value;
  201.  
  202.     ofstream fout1("D:/spline_graphic.txt");
  203.     ofstream fout2("D:/original_graphic.txt");
  204.     ofstream fout3("D:/difference_graphic.txt");
  205.     x_value = Initial;
  206.     for (i = 0; i < Count_Segments; i++)
  207.     {
  208.         while (x_value < Array[i + 1].x)
  209.         {
  210.             fout1 << x_value << " ";
  211.             fout2 << x_value << " ";
  212.             fout3 << x_value << " ";
  213.             y_value1 = Spline(x_value, i, Array_steps, x_massiv, Array);
  214.             y_value2 = (*f)(x_value);
  215.             fout1 << y_value1 << endl;
  216.             fout2 << y_value2 << endl;
  217.             fout3 << abs(y_value2 - y_value1) << endl;
  218.             x_value += step;
  219.         }
  220.     }
  221.     fout1 << End << " ";
  222.     fout2 << End << " ";
  223.     fout1 << Spline(End, Count_Segments - 1, Array_steps, x_massiv, Array) << endl;
  224.     fout2 << (*f)(End) << endl;
  225.     fout1.close();
  226.     fout2.close();
  227.     cout << endl << "Запись в файл осуществлена!" << endl;
  228. }
  229.  
  230.  
  231. int main()
  232. {
  233.     setlocale(LC_ALL, "RUS");
  234.     functiontype Func = &Myfunc;
  235.     int CountSegments, i, Countdots = 15000;
  236.  
  237.     /*---------------------------------Входные данные-------------------------------------*/
  238.     double Initial = -5, End = 5, B, A;
  239.     cout << "Введите N: ";
  240.     cin >> CountSegments;
  241.     A = approximate_derivative_2(&Func, Initial);
  242.     B = approximate_derivative_1(&Func, End);
  243.  
  244.     /*---------------------------------Сетки-------------------------------------*/
  245.     double* Array_steps_cheb = new double[CountSegments + 1];
  246.     double* Array_steps_uni = new double[CountSegments + 1];
  247.  
  248.     //Равномерная сетка
  249.     Node* ArrayUniformNodes = new Node[CountSegments + 1];
  250.     ValueUniformTable(&Func, ArrayUniformNodes, Initial, End, CountSegments, Array_steps_uni);
  251.     //Чебышёв
  252.     Node* ArrayChebyshevNodes = new Node[CountSegments + 1];
  253.     ValueChebyshevTable(&Func, ArrayChebyshevNodes, Initial, End, CountSegments, Array_steps_cheb);
  254.  
  255.     /*---------------------------------СЛАУ - matrix_coeffs -------------------------------------*/
  256.     double** matrix_coeffs;
  257.     matrix_coeffs = new double*[CountSegments - 1];
  258.     for (i = 0; i < CountSegments - 1; i++)
  259.         matrix_coeffs[i] = new double[3];
  260.  
  261.     //cout << endl << "Полученная матрица: " << endl;
  262.     get_matrix_coeffs(Initial, End, CountSegments, &Func, matrix_coeffs, ArrayChebyshevNodes, Array_steps_cheb, B);
  263.  
  264.     /*---------------------------------Прогонка - запись в x_massiv -------------------------------------*/
  265.     //Получение ответа в x_massiv
  266.     double* x_massiv = new double[CountSegments + 1];
  267.     x_massiv[0] = A;    //g0
  268.     tridiagonal_matrix_algorithm(matrix_coeffs, Array_steps_cheb, ArrayChebyshevNodes, CountSegments, x_massiv, B);
  269.  
  270.     /*---------------------------------Графики -------------------------------------*/
  271.     //Запись значений сплайна и интерполируемой функции в файлы соответсвенно
  272.     tables_in_file(&Func, ArrayChebyshevNodes, Countdots, CountSegments, Initial, End, Array_steps_cheb, x_massiv);
  273.  
  274.     /*---------------------------------Вывод в консоль значений -------------------------------------*/
  275.     /*
  276.     //Шаги
  277.     cout << endl;
  278.     for (i = 1; i <= CountSegments; i++) {
  279.         cout << "h_" << i << ": " << Array_steps_cheb[i] << endl;
  280.     }
  281.  
  282.     cout << endl << "Cетка:  " << endl;
  283.         for (int i = 0; i < CountSegments + 1; i++)
  284.     cout << "(" << ArrayUniformNodes[i].x << ":" << ArrayUniformNodes[i].y << ")" << endl;
  285.    
  286.  
  287.     /*cout <<endl << "Правая часть: " << endl;
  288.     for (i = 0; i < CountSegments - 1; i++) {
  289.         cout << "d_" << i << ": " << matrix_coeffs[i][3] << endl;
  290.     }
  291.    
  292.     cout << endl;
  293.     for (i = 0; i < CountSegments; i++)
  294.         cout << "Spline in x_" << i << " " << Spline(ArrayUniformNodes[i].x, i, Array_steps_uni, x_massiv, ArrayUniformNodes) << endl;
  295.     cout << "Spline in x_" << CountSegments << " " << Spline(ArrayUniformNodes[CountSegments].x, CountSegments - 1, Array_steps_uni, x_massiv, ArrayUniformNodes) << endl;
  296.     */
  297.  
  298.     cout << endl;
  299.     system("pause");
  300.     return 0;
  301. }
Advertisement
Add Comment
Please, Sign In to add comment