Advertisement
vadimk772336

Untitled

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