Advertisement
vadimk772336

Untitled

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