Advertisement
vadimk772336

Untitled

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