vadimk772336

Untitled

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