Advertisement
Guest User

Untitled

a guest
Apr 2nd, 2020
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.02 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, double* x, int n) {
  68.  
  69.     double* tmp_mas;
  70.     tmp_mas = new double[n];
  71.  
  72.     int p, i, j, k, index;
  73.     double tmp, max, r;
  74.  
  75.     for (j = 0; j <= n - 2; j++) {
  76.  
  77.          max = abs(coeffs_massiv[j][j]);
  78.          index = j;
  79.  
  80.          //Поиск строки с макс по модулю элементом в jом столбце
  81.          for (i = index+1; i <= n - 1; i++) { // Идем по по столбцу index=j  до самого низа
  82.              if (abs(coeffs_massiv[i][index]) > max) {
  83.                  max = abs(coeffs_massiv[i][index]);
  84.                  index = i;
  85.              }
  86.          } //index хранит строку с макс эл в jом столбце
  87.          cout << "max: " << coeffs_massiv[index][j] << endl;
  88.          cout << "Переставляем " << j << " и " << index << " строку " << endl;
  89.  
  90.          //Реализация перестановки 2х строк: j-ой и найденной = index перед итерацией Гаусса
  91.          if (index != j) {
  92.              for (i = 0; i <= n - 1; i++) {
  93.                  tmp_mas[i] = coeffs_massiv[j][i];
  94.                  coeffs_massiv[j][i] = coeffs_massiv[index][i];
  95.                  coeffs_massiv[index][i] = tmp_mas[i];
  96.              }
  97.              tmp = y_Massiv[j]; //правая часть Jой строки
  98.              y_Massiv[j] = y_Massiv[index];
  99.              y_Massiv[index] = tmp;
  100.          }
  101.          
  102.  
  103.          // Будем Jой строкой убивать jые элементы во всех строках находящихся ниже
  104.         for (i = j + 1; i <= n - 1; i++) { //фиксируем iую строку (это внутри цикла для jj элемента)
  105.             r = -coeffs_massiv[i][j] / coeffs_massiv[j][j];
  106.  
  107.             for (k = j; k <= n - 1; k++) { //Домножили jую и сложили с ioй - изменилась iая строка
  108.                 coeffs_massiv[i][k] = coeffs_massiv[i][k] + r * coeffs_massiv[j][k];
  109.             }
  110.  
  111.             y_Massiv[i] = y_Massiv[i] + r * y_Massiv[j];
  112.         }
  113.     }
  114.  
  115.     double tmp1 = 0;
  116.     //обратная подстановка
  117.     x[n - 1] = y_Massiv[n - 1] / coeffs_massiv[n - 1][n - 1];  //Первый шаг длаем сами
  118.     for (i = n - 2; i >= 0; i--) { //фиксируем iую строку - будем искать xi
  119.         tmp1 = 0;
  120.         for (int j = i + 1; j <= n - 1; j++)
  121.             tmp1 += coeffs_massiv[i][j]*x[j]; //Переносим вправо всё что известно и суммируем, но в отдельную переменную чтобы не испортить правую часть.
  122.         x[i] = y_Massiv[i]-tmp1 / coeffs_massiv[i][i];
  123.     }
  124.    
  125. }
  126.  
  127.  
  128. //Запись в 3 файла: значений сплайна, интерполируемой функции и их разницы в countdots точках
  129. void tables_in_file(functiontype1* f, double* Array, int Count_dots, int CountSegments, double Initial, double End)
  130. {
  131.     int i = 0;
  132.     double step = (End - Initial) / (Count_dots - 1), y_value1, y_value2, x_value;
  133.  
  134.     ofstream fout1("D:/spline_graphic.txt");
  135.     ofstream fout2("D:/original_graphic.txt");
  136.     ofstream fout3("D:/difference_graphic.txt");
  137.     x_value = Initial;
  138.     for (i = 0; i < CountSegments; i++)
  139.     {
  140.         while (x_value < Array[i + 1])
  141.         {
  142.             fout1 << x_value << " ";
  143.             fout2 << x_value << " ";
  144.             fout3 << x_value << " ";
  145.             x_value += step;
  146.         }
  147.     }
  148.     fout1 << End << " ";
  149.     fout2 << End << " ";
  150.     fout2 << (*f)(End) << endl;
  151.     fout1.close();
  152.     fout2.close();
  153.     //cout << endl << "Запись в файл осуществлена!" << endl;
  154. }
  155.  
  156. void get_equations_massiv(functiontype2* K, functiontype1* f, int CountDots, double** coeffs_massiv, double* x_Array, double* y_Array) {
  157.     int i, j;
  158.  
  159.     for (i = 0; i < CountDots; i++) {
  160.         for (j = 0; j < CountDots; j++) {
  161.             if (j == 0)
  162.                 coeffs_massiv[i][j] = 1;
  163.             else
  164.                 coeffs_massiv[i][j] = -(x_Array[j] - x_Array[j - 1]) * (*K)(x_Array[i], x_Array[j]);
  165.         }
  166.         y_Array[i] = (*f)(x_Array[i]);
  167.     }
  168.  
  169.     for (i = 0; i < CountDots; i++) {
  170.         for (j = 0; j < CountDots; j++) {
  171.             cout << coeffs_massiv[i][j] << " ";
  172.         }
  173.         cout << y_Array[i] << endl;
  174.         cout << endl;
  175.     }
  176.  
  177. }
  178.  
  179. int main()
  180. {
  181.     setlocale(LC_ALL, "RUS");
  182.     functiontype1 Func = &f;
  183.     int CountDots = 3, i, Graphic_Dots = 250;
  184.  
  185.     /*---------------------------------Входные данные-------------------------------------*/
  186.     double Initial = -10, End = 10;
  187.  
  188.  
  189.  
  190.     double** coeffs_massiv;
  191.     coeffs_massiv = new double* [CountDots];
  192.     for (i = 0; i < CountDots; i++)
  193.         coeffs_massiv[i] = new double[2];
  194.     double* y_Array = new double[CountDots];
  195.     double* X_Array = new double[CountDots];
  196.     /*double* x_Array = new double[CountDots];
  197.     ValueUniformTable(x_Array, Initial, End, CountDots);
  198.  
  199.     double* y_Array = new double[CountDots];
  200.     get_equations_massiv(&Func2, &Func1, CountDots, coeffs_massiv, x_Array, y_Array);*/
  201.     coeffs_massiv[0][0] = 1;
  202.     coeffs_massiv[0][1] = 1;
  203.     coeffs_massiv[0][2] = 1;
  204.     coeffs_massiv[1][0] = 0;
  205.     coeffs_massiv[1][1] = 0;
  206.     coeffs_massiv[1][2] = 1;
  207.     coeffs_massiv[2][0] = 0;
  208.     coeffs_massiv[2][1] = 1;
  209.     coeffs_massiv[2][2] = 1;
  210.     y_Array[0] = 1;
  211.     y_Array[1] = 2;
  212.     y_Array[2] = 3;
  213.     Gauss_with_vibor(coeffs_massiv, y_Array,X_Array, CountDots);
  214.     cout << endl;
  215.     for (i = 0; i < CountDots; i++) {
  216.         for (int j = 0; j < CountDots; j++) {
  217.             cout << coeffs_massiv[i][j] << " ";
  218.         }
  219.         cout << y_Array[i] << endl;
  220.         cout << endl;
  221.     }
  222.     for (int j = 0; j < CountDots; j++) {
  223.         cout << X_Array[j] << " ";
  224.     }
  225.     cout << endl;
  226.     system("pause");
  227.     return 0;
  228. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement