vadimk772336

Untitled

Apr 3rd, 2020
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.74 KB | None | 0 0
  1. /*
  2. plot "D:\graphic.txt" with lines title "График U(x)"
  3. plot "D:\graphic.txt" with lines title "График U(x)", "D:\graphic.txt" with points title "" pointtype 3 pointsize 1 lc 200 200 200
  4. */
  5. #include <iostream>
  6. #include <math.h>
  7. #include <cmath>
  8. #include <stdlib.h>
  9. #include <fstream>
  10. using namespace std;
  11. const double PI = 3.141592653589793238463;
  12.  
  13. //Задание функции
  14. typedef double(*functiontype1)(double x);
  15. typedef double(*functiontype2)(double x, double s);
  16. double f(double x)
  17. {
  18.     return 28*x * x * x + x - 56.33333333;
  19. }
  20. double K(double x, double s)
  21. {
  22.     return x * x*x -s;
  23. }
  24. functiontype1 Func1 = &f;
  25. functiontype2 Func2 = &K;
  26.  
  27. //Задает набор узлов, равномерно удаленных друг от друга
  28. void ValueUniformTable(double* Array, double Initial, double End, int CountDots)
  29. {
  30.     double h = abs(End - Initial) / (CountDots - 1);
  31.     Array[0] = Initial;
  32.     for (int i = 1; i < CountDots; i++)
  33.         Array[i] = Array[i - 1] + h;
  34. }
  35.  
  36. void Gaussian_elimination(double** coeffs_massiv, double* y_Massiv, double* U_massiv, int n) {
  37.  
  38.     double* tmp_mas;
  39.     tmp_mas = new double[n];
  40.  
  41.     int i, j, k, index;
  42.     double tmp, max, r;
  43.  
  44.     for (j = 0; j <= n - 2; j++) {
  45.  
  46.         max = abs(coeffs_massiv[j][j]);
  47.         index = j;
  48.  
  49.         for (i = index + 1; i <= n - 1; i++) {
  50.             if (abs(coeffs_massiv[i][index]) > max) {
  51.                 max = abs(coeffs_massiv[i][index]);
  52.                 index = i;
  53.             }
  54.         }
  55.  
  56.         if (index != j) {
  57.             for (i = 0; i <= n - 1; i++) {
  58.                 tmp_mas[i] = coeffs_massiv[j][i];
  59.                 coeffs_massiv[j][i] = coeffs_massiv[index][i];
  60.                 coeffs_massiv[index][i] = tmp_mas[i];
  61.             }
  62.             tmp = y_Massiv[j];
  63.             y_Massiv[j] = y_Massiv[index];
  64.             y_Massiv[index] = tmp;
  65.         }
  66.  
  67.         for (i = j + 1; i <= n - 1; i++) {
  68.             r = -coeffs_massiv[i][j] / coeffs_massiv[j][j];
  69.  
  70.             for (k = j; k <= n - 1; k++) {
  71.                 coeffs_massiv[i][k] = coeffs_massiv[i][k] + r * coeffs_massiv[j][k];
  72.             }
  73.             y_Massiv[i] = y_Massiv[i] + r * y_Massiv[j];
  74.         }
  75.     }
  76.  
  77.  
  78.     U_massiv[n - 1] = y_Massiv[n - 1] / coeffs_massiv[n - 1][n - 1];  
  79.     for (i = n - 2; i >= 0; i--) {
  80.         tmp = 0.0;
  81.         for (int j = i + 1; j <= n - 1; j++)
  82.             tmp += coeffs_massiv[i][j] * U_massiv[j];
  83.         U_massiv[i] = (y_Massiv[i] - tmp) / coeffs_massiv[i][i];
  84.     }
  85. }
  86.  
  87.  
  88. //Запись в файл (x_i, U_i)
  89. void table_in_file(double* x_Array, int CountDots, double* U_massiv)
  90. {
  91.     ofstream fout("D:/graphic.txt");
  92.     for (int i = 0; i < CountDots; i++)
  93.         fout << x_Array[i] << " " << U_massiv[i] << endl;
  94.  
  95.     fout.close();
  96. }
  97.  
  98. //Массив, хранящий коэффиценты системы
  99. void get_coeffs_massiv(functiontype2* K, functiontype1* f, int CountDots, double** coeffs_massiv, double* x_Array, double* y_Array) {
  100.     int i, j;
  101.     for (i = 0; i < CountDots; i++) {
  102.         for (j = 0; j < CountDots; j++) {
  103.                 coeffs_massiv[i][j] = (x_Array[j] - x_Array[j - 1]) * (*K)(x_Array[i], x_Array[j]);
  104.                 if  (j == 0) coeffs_massiv[i][j] = 0;
  105.                 if (i == j) coeffs_massiv[i][j] += 1;
  106.                
  107.         }
  108.         y_Array[i] = (*f)(x_Array[i]);
  109.     }
  110. }
  111.  
  112. int main()
  113. {
  114.     setlocale(LC_ALL, "RUS");
  115.     int i, j;
  116.  
  117.     /*---------------------------------Входные данные-------------------------------------*/
  118.     double Initial = 0, End = 4 ;
  119.     int CountDots = 50;
  120.  
  121.  
  122.     /*---------------------------------Задаём необходимые массивы-------------------------*/
  123.     double** coeffs_massiv; //массив хранящий коэффиценты СЛАУ
  124.     coeffs_massiv = new double* [CountDots];
  125.     for (i = 0; i < CountDots; i++)
  126.         coeffs_massiv[i] = new double[CountDots];
  127.  
  128.     double* U_massiv = new double[CountDots]; //Хранит U_i
  129.     double* y_Array = new double[CountDots]; //Правая часть СЛАУ
  130.     double* x_Array = new double[CountDots]; //Узлы
  131.  
  132.  
  133.     /*---------------------------------Заполняем и решаем---------------------------------*/
  134.     ValueUniformTable(x_Array, Initial, End, CountDots); //Строим сетку
  135.     get_coeffs_massiv(&Func2, &Func1, CountDots, coeffs_massiv, x_Array, y_Array); //Строим СЛАУ
  136.     Gaussian_elimination(coeffs_massiv, y_Array, U_massiv, CountDots);
  137.     table_in_file(x_Array, CountDots, U_massiv);
  138.  
  139.  
  140.     /*---------------------------------Вывод в консоль------------------------------------*/
  141.  
  142.     /*cout << endl << "Полученная СЛАУ: " << endl;
  143.     for (i = 0; i < CountDots; i++) {
  144.         for (int j = 0; j < CountDots; j++) {
  145.             cout << coeffs_massiv[i][j] << " ";
  146.         }
  147.         cout << y_Array[i] << endl;
  148.         cout << endl;
  149.     }*/
  150.  
  151.     cout << endl << "Узлы: " << endl;
  152.     for (j = 0; j < CountDots; j++) {
  153.         cout << x_Array[j] << " ";
  154.     }
  155.  
  156.     cout << endl << endl << "U_i: " << endl;
  157.     for (j = 0; j < CountDots; j++) {
  158.         cout << U_massiv[j] << " ";
  159.     }
  160.  
  161.     cout << endl;
  162.     system("pause");
  163.     return 0;
  164. }
Advertisement
Add Comment
Please, Sign In to add comment