Advertisement
Guest User

Untitled

a guest
Apr 1st, 2020
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.07 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, 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[i][index] << 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.    
  116.     //обратная подстановка
  117.     x[n - 1] = y_Massiv[n - 1]/coeffs_massiv[n - 1][n - 1];  //Первый шаг длаем сами
  118.     for (j = n - 2; j >= 0; j--) { //фиксируем Jую строку - будем искать xj
  119.         for (int i = j+1; i <= n-1; i++)
  120.             y_Massiv[j] = y_Massiv[j] - coeffs_massiv[j][i] * x[j]; //Переносим вправо всё что известно и суммируем
  121.         x[j] = y_Massiv[j]/coeffs_massiv[j][j]
  122.     }
  123. }
  124.  
  125.  
  126. //Запись в 3 файла: значений сплайна, интерполируемой функции и их разницы в countdots точках
  127. void tables_in_file(functiontype1* f, double* Array, int Count_dots, int CountSegments, double Initial, double End)
  128. {
  129.     int i = 0;
  130.     double step = (End - Initial) / (Count_dots - 1), y_value1, y_value2, x_value;
  131.  
  132.     ofstream fout1("D:/spline_graphic.txt");
  133.     ofstream fout2("D:/original_graphic.txt");
  134.     ofstream fout3("D:/difference_graphic.txt");
  135.     x_value = Initial;
  136.     for (i = 0; i < CountSegments; i++)
  137.     {
  138.         while (x_value < Array[i + 1])
  139.         {
  140.             fout1 << x_value << " ";
  141.             fout2 << x_value << " ";
  142.             fout3 << x_value << " ";
  143.             x_value += step;
  144.         }
  145.     }
  146.     fout1 << End << " ";
  147.     fout2 << End << " ";
  148.     fout2 << (*f)(End) << endl;
  149.     fout1.close();
  150.     fout2.close();
  151.     //cout << endl << "Запись в файл осуществлена!" << endl;
  152. }
  153.  
  154. void get_equations_massiv(functiontype2* K, functiontype1* f, int CountDots, double** coeffs_massiv, double* x_Array, double* y_Array) {
  155.     int i, j;
  156.  
  157.     for (i = 0; i < CountDots; i++) {
  158.         for (j = 0; j < CountDots; j++) {
  159.             if (j == 0)
  160.                 coeffs_massiv[i][j] = 1;
  161.             else
  162.                 coeffs_massiv[i][j] = -(x_Array[j] - x_Array[j - 1])* (*K)(x_Array[i], x_Array[j]);
  163.         }
  164.         y_Array[i] = (*f)(x_Array[i]);
  165.     }
  166.  
  167.     for (i = 0; i < CountDots; i++) {
  168.         for (j = 0; j < CountDots; j++) {
  169.             cout << coeffs_massiv[i][j] << " ";
  170.         }
  171.         cout << y_Array[i] << endl;
  172.         cout << endl;
  173.     }
  174.  
  175. }
  176.  
  177. int main()
  178. {
  179.     setlocale(LC_ALL, "RUS");
  180.     functiontype1 Func = &f;
  181.     int CountDots = 3, i, Graphic_Dots = 250;
  182.  
  183.     /*---------------------------------Входные данные-------------------------------------*/
  184.     double Initial = -10, End = 10;
  185.  
  186.  
  187.  
  188.     double** coeffs_massiv;
  189.     coeffs_massiv = new double*[CountDots];
  190.     for (i = 0; i < CountDots; i++)
  191.         coeffs_massiv[i] = new double[2];
  192.     double* y_Array = new double[CountDots];
  193.     /*double* x_Array = new double[CountDots];
  194.     ValueUniformTable(x_Array, Initial, End, CountDots);
  195.  
  196.     double* y_Array = new double[CountDots];
  197.     get_equations_massiv(&Func2, &Func1, CountDots, coeffs_massiv, x_Array, y_Array);*/
  198.     coeffs_massiv[0][0] = 1;
  199.     coeffs_massiv[0][1] = 0;
  200.     coeffs_massiv[0][2] = 1;
  201.     coeffs_massiv[1][0] = 0;
  202.     coeffs_massiv[1][1] = 1;
  203.     coeffs_massiv[1][2] = 1;
  204.     coeffs_massiv[2][0] = 1;
  205.     coeffs_massiv[2][1] = 0;
  206.     coeffs_massiv[2][2] = 0;
  207.     y_Array[0] = 1;
  208.     y_Array[1] = 2;
  209.     y_Array[2] = 3;
  210.     Gauss_with_vibor(coeffs_massiv, y_Array, CountDots);
  211.     cout << endl;
  212.     /*for (i = 0; i < CountDots; i++) {
  213.         for (int j = 0; j < CountDots; j++) {
  214.             cout << coeffs_massiv[i][j] << " ";
  215.         }
  216.         cout << y_Array[i] << endl;
  217.         cout << endl;
  218.     }*/
  219.  
  220.     cout << endl;
  221.     system("pause");
  222.     return 0;
  223. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement