vadimk772336

Untitled

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