vadimk772336

Untitled

Mar 29th, 2020
566
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.68 KB | None | 0 0
  1. /*
  2. plot "D:\spline_graphic.txt" with lines title "Сплайн", "D:\original_graphic.txt" with lines title "График функции"
  3. plot "D:\difference_graphic.txt" with lines title "Разница"
  4. */
  5. #include <iostream>
  6. #include <math.h>
  7. #include <cmath>
  8. #include <Windows.h>
  9. #include <stdlib.h>
  10. #include <fstream>
  11. using namespace std;
  12. const double PI = 3.141592653589793238463;
  13.  
  14. //Задание функции
  15. typedef double(*functiontype1)(double x);
  16. typedef double(*functiontype2)(double x, double s);
  17. double f(double x)
  18. {
  19.     return exp(x) + (1 + 593.653)*x*x;
  20. }
  21. double K(double x, double s)
  22. {
  23.     return x * s;
  24. }
  25. functiontype1 Func1 = &f;
  26. functiontype2 Func2 = &K;
  27.  
  28.  
  29. void ValueUniformTable(double* Array, double Initial, double End, int CountSegments)
  30. {
  31.     double h;
  32.     ofstream fout4("D:\setka_uni.txt");
  33.     Array[0] = Initial;
  34.     fout4 << Array[0] << " " << endl;
  35.     h = abs(End - Initial) / CountSegments;
  36.  
  37.     for (int i = 1; i <= CountSegments; i++)
  38.     {
  39.         Array[i] = Array[i - 1] + h;
  40.         fout4 << Array[i] << endl;
  41.     }
  42.     fout4.close();
  43. }
  44.  
  45. void ValueChebyshevTable(double* Array, double Initial, double End, int CountSegments)
  46. {
  47.     ofstream fout5("D:\setka_cheb.txt");
  48.     Array[0] = Initial;
  49.     fout5 << Array[0] << " " << Array[0] << endl;
  50.     double k1, k2, denom;
  51.     k1 = (End + Initial) / 2;
  52.     k2 = (End - Initial) / 2;
  53.     denom = 2 * (CountSegments + 1);
  54.     for (int i = 1; i < CountSegments; i++)
  55.     {
  56.         cout << k1 + k2 * cos(PI / (denom)) << endl;
  57.         Array[CountSegments - i] = k1 + k2 * cos(((2 * i + 1)*PI) / (denom));
  58.         fout5 << Array[CountSegments - i] << " " << endl;
  59.     }
  60.  
  61.     Array[CountSegments] = End;
  62.     fout5 << Array[CountSegments] << " " << endl;
  63.     fout5.close();
  64. }
  65.  
  66.  
  67. void Gauss_with_vibor(double** coeffs_massiv, double* y_Massiv, int n) {
  68.  
  69.     double* tmp_mas;
  70.     tmp_mas = new double[n];
  71.  
  72.     int p, i, j, k, q;
  73.     double tmp, max,r;
  74.  
  75.     for (j = 0; j <= n - 2; j++) {
  76.  
  77.         max = abs(coeffs_massiv[j][j]);
  78.         q = j;
  79.  
  80.         //Реализация перестановки перед итерацией
  81.         for (p = j; p <= n - 1; p++) { // Идем по по столбцу, то есть меняем строки при фикс столбце j
  82.             if (abs(coeffs_massiv[p][j]) > max) {
  83.                 max = abs(coeffs_massiv[p][j]);
  84.                 q = p;
  85.             }
  86.         }
  87.         if (q != j) {
  88.             for (p = 0; p <= n - 1; p++) {
  89.                 tmp_mas[p] = coeffs_massiv[j][p];
  90.                 coeffs_massiv[j][p] = coeffs_massiv[q][p];
  91.                 coeffs_massiv[q][p] = tmp_mas[p];
  92.  
  93.                 tmp = y_Massiv[p];
  94.                 y_Massiv[p] = y_Massiv[q];
  95.                 y_Massiv[q] = tmp;
  96.             }
  97.         }
  98.  
  99.  
  100.         for (i = j + 1; i <= n - 1; i++) {
  101.             r = -coeffs_massiv[i][j] / coeffs_massiv[j][j];
  102.             for (k = j; k <= n; k++) {
  103.                 coeffs_massiv[i][k] = coeffs_massiv[i][k] + r * coeffs_massiv[j][k];
  104.             }
  105.             y_Massiv[i] = y_Massiv[i] + r * y_Massiv[j];
  106.         }
  107.  
  108.     }
  109. }
  110. /*DO FOR j = 1 TO n − 1
  111.   DO FOR i = j + 1 TO n
  112.     rij ←(−aij / ajj)
  113.     DO FOR k = j TO n
  114.       aik ← aik + rijajk
  115.     END DO
  116.     bi ← bi + rijbj
  117.   END DO
  118. END DO*/
  119.  
  120. //Запись в 3 файла: значений сплайна, интерполируемой функции и их разницы в countdots точках
  121. void tables_in_file(functiontype1* f, double* Array, int Count_dots, int CountSegments, double Initial, double End)
  122. {
  123.     int i = 0;
  124.     double step = (End - Initial) / (Count_dots - 1), y_value1, y_value2, x_value;
  125.  
  126.     ofstream fout1("D:/spline_graphic.txt");
  127.     ofstream fout2("D:/original_graphic.txt");
  128.     ofstream fout3("D:/difference_graphic.txt");
  129.     x_value = Initial;
  130.     for (i = 0; i < CountSegments; i++)
  131.     {
  132.         while (x_value < Array[i + 1])
  133.         {
  134.             fout1 << x_value << " ";
  135.             fout2 << x_value << " ";
  136.             fout3 << x_value << " ";
  137.             x_value += step;
  138.         }
  139.     }
  140.     fout1 << End << " ";
  141.     fout2 << End << " ";
  142.     fout2 << (*f)(End) << endl;
  143.     fout1.close();
  144.     fout2.close();
  145.     //cout << endl << "Запись в файл осуществлена!" << endl;
  146. }
  147.  
  148. void get_equations_massiv(functiontype2* K, functiontype1* f, int CountDots, double** coeffs_massiv, double* x_Array, double* y_Array) {
  149.     int i, j;
  150.  
  151.     for (i = 0; i < CountDots; i++) {
  152.         for (j = 0; j < CountDots; j++) {
  153.             if (j == 0)
  154.                 coeffs_massiv[i][j] = 1;
  155.             else
  156.                 coeffs_massiv[i][j] = -(x_Array[j] - x_Array[j - 1])* (*K)(x_Array[i], x_Array[j]);
  157.         }
  158.         y_Array[i] = (*f)(x_Array[i]);
  159.     }
  160.  
  161.     for (i = 0; i < CountDots; i++) {
  162.         for (j = 0; j < CountDots; j++) {
  163.             cout << coeffs_massiv[i][j] << " ";
  164.         }
  165.         cout << y_Array[i] << endl;
  166.         cout << endl;
  167.     }
  168.  
  169. }
  170.  
  171. int main()
  172. {
  173.     setlocale(LC_ALL, "RUS");
  174.     functiontype1 Func = &f;
  175.     int CountDots = 3, i, Graphic_Dots = 250;
  176.  
  177.     /*---------------------------------Входные данные-------------------------------------*/
  178.     double Initial = -10, End = 10;
  179.  
  180.  
  181.  
  182.     double** coeffs_massiv;
  183.     coeffs_massiv = new double*[CountDots];
  184.     for (i = 0; i < CountDots; i++)
  185.         coeffs_massiv[i] = new double[2];
  186.     double* y_Array = new double[CountDots];
  187.     /*double* x_Array = new double[CountDots];
  188.     ValueUniformTable(x_Array, Initial, End, CountDots);
  189.  
  190.     double* y_Array = new double[CountDots];
  191.     get_equations_massiv(&Func2, &Func1, CountDots, coeffs_massiv, x_Array, y_Array);*/
  192.     coeffs_massiv[0][0] = 1;
  193.     coeffs_massiv[0][1] = 0;
  194.     coeffs_massiv[0][2] = 1;
  195.     coeffs_massiv[1][0] = 0;
  196.     coeffs_massiv[1][1] = 1;
  197.     coeffs_massiv[1][2] = 1;
  198.     coeffs_massiv[2][0] = 1;
  199.     coeffs_massiv[2][1] = 0;
  200.     coeffs_massiv[2][2] = 0;
  201.     y_Array[0] = 1;
  202.     y_Array[1] = 2;
  203.     y_Array[2] = 3;
  204.     Gauss_with_vibor(coeffs_massiv, y_Array, CountDots);
  205.     cout << endl;
  206.     /*for (i = 0; i < CountDots; i++) {
  207.         for (int j = 0; j < CountDots; j++) {
  208.             cout << coeffs_massiv[i][j] << " ";
  209.         }
  210.         cout << y_Array[i] << endl;
  211.         cout << endl;
  212.     }*/
  213.  
  214.     cout << endl;
  215.     system("pause");
  216.     return 0;
  217. }
Advertisement
Add Comment
Please, Sign In to add comment