vadimk772336

Untitled

Mar 28th, 2020
472
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.82 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. //Запись в 3 файла: значений сплайна, интерполируемой функции и их разницы в countdots точках
  67. void tables_in_file(functiontype1* f, double* Array, int Count_dots, int CountSegments, double Initial, double End)
  68. {
  69.     int i = 0;
  70.     double step = (End - Initial) / (Count_dots - 1), y_value1, y_value2, x_value;
  71.  
  72.     ofstream fout1("D:/spline_graphic.txt");
  73.     ofstream fout2("D:/original_graphic.txt");
  74.     ofstream fout3("D:/difference_graphic.txt");
  75.     x_value = Initial;
  76.     for (i = 0; i < CountSegments; i++)
  77.     {
  78.         while (x_value < Array[i + 1])
  79.         {
  80.             fout1 << x_value << " ";
  81.             fout2 << x_value << " ";
  82.             fout3 << x_value << " ";
  83.             x_value += step;
  84.         }
  85.     }
  86.     fout1 << End << " ";
  87.     fout2 << End << " ";
  88.     fout2 << (*f)(End) << endl;
  89.     fout1.close();
  90.     fout2.close();
  91.     //cout << endl << "Запись в файл осуществлена!" << endl;
  92. }
  93.  
  94. void get_equations_massiv(functiontype2* K, functiontype1* f, int CountDots, double** coeffs_massiv, double* x_Array, double* y_Array) {
  95.     int i, j;
  96.  
  97.     for (i = 0; i < CountDots; i++) {
  98.         for (j = 0; j < CountDots; j++) {
  99.             if (j == 0)
  100.                 coeffs_massiv[i][j] = 1;
  101.             else
  102.                 coeffs_massiv[i][j] = -(x_Array[j] - x_Array[j - 1])* (*K)(x_Array[i], x_Array[j]);
  103.         }
  104.         y_Array[i] = (*f)(x_Array[i]);
  105.     }
  106.  
  107.     for (i = 0; i < CountDots; i++) {
  108.         for (j = 0; j < CountDots; j++) {
  109.             cout << coeffs_massiv[i][j] << " ";
  110.         }
  111.         cout << y_Array[i] << endl;
  112.         cout << endl;
  113.     }
  114.    
  115. }
  116.  
  117. int main()
  118. {
  119.     setlocale(LC_ALL, "RUS");
  120.     functiontype1 Func = &f;
  121.     int CountDots = 3, i, Graphic_Dots = 250;
  122.  
  123.     /*---------------------------------Входные данные-------------------------------------*/
  124.     double Initial = -10, End = 10;
  125.    
  126.  
  127.  
  128.     double** coeffs_massiv;
  129.     coeffs_massiv = new double*[CountDots];
  130.     for (i = 0; i < CountDots; i++)
  131.         coeffs_massiv[i] = new double[2];
  132.  
  133.     double* x_Array = new double[CountDots];
  134.     ValueUniformTable(x_Array, Initial, End, CountDots);
  135.  
  136.     double* y_Array = new double[CountDots];
  137.     get_equations_massiv(&Func2, &Func1, CountDots, coeffs_massiv, x_Array, y_Array);
  138.    
  139.  
  140.     cout << endl;
  141.     system("pause");
  142.     return 0;
  143. }
Advertisement
Add Comment
Please, Sign In to add comment