vadimk772336

Untitled

Oct 7th, 2019
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.66 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <iostream>
  3. #include<math.h>
  4. #include <vector>
  5. #include <Windows.h>
  6. #include <stdlib.h>
  7.  
  8. using namespace std;
  9.  
  10. const double PI = 3.1415926535897932384626433832795;
  11.  
  12. typedef double (*functiontype)(double x);
  13.  
  14. typedef struct Node
  15. {
  16.     double x,y;
  17. }Node;
  18.  
  19. typedef struct Interval
  20. {
  21.     double InitialNode,EndNode;
  22. }Interval;
  23.  
  24. int Factorial(int n)
  25. { // Факториал
  26.     int x = 1;
  27.     for (int i = 1; i <= n; i++)
  28.     {
  29.         x *= i;
  30.     }
  31.     return x;
  32. }
  33.  
  34. double sinus(double x)
  35. { // Синус
  36.     return sin(x);
  37. }
  38.  
  39. double xv10(double x)
  40. { // x^10
  41.     return double(powl(x,10));
  42. }
  43.  
  44. double exponenta(double x)
  45. { // Экспонента
  46.     return exp(x);
  47. }
  48.  
  49. void ValueUniformTable(functiontype *f, Node *Array, double Initial, double End, int Count)
  50. { // Создание равномерной таблицы значений
  51.     double step = abs(Initial - End) / (Count - 1);
  52.     Array[0].x = Initial;
  53.     Array[0].y = (*f)(Array[0].x);
  54.     for (int i = 1; i < Count; i++)
  55.     {
  56.         Array[i].x = Array[i-1].x + step;
  57.         Array[i].y = (*f)(Array[i].x);
  58.     }
  59. }
  60.  
  61. void ValueChebyshevTable(functiontype *f, Node *Array, double Initial, double End, int Count)
  62. { // Создание таблицы Чебышевских значений
  63.     for (int i = 0; i < Count; i++)
  64.     {
  65.         Array[i].x = ((End + Initial) / 2) + ((End - Initial) / 2) * cos(((2 * i + 1) * PI) / (2 * (Count + 1)));
  66.         Array[i].y = (*f)(Array[i].x);
  67.     }
  68. }
  69.  
  70. double PolynomLG(double x, Node *Array, int Count)
  71. { // Полином Лагранжа
  72.     double Polynom = 0;
  73.     for (int i = 0; i < Count; i++)
  74.     { // Счетчик по функциям l_i
  75.         double Li = 1;
  76.         for (int j = 0; j < Count; j++)
  77.         { // Счетчик по x_j
  78.             if (i != j) Li *= (x - Array[j].x) / (Array[i].x - Array[j].x);
  79.         }
  80.         Polynom += Array[i].y * Li;
  81.     }
  82.     return Polynom;
  83. }
  84.  
  85. double PolynomN(double x, Node *Array, int Count)
  86. {
  87.     double Result = Array[0].y;
  88.     for (int i = 1; i < Count; i++)
  89.     {
  90.         double Polynom = 0;
  91.         for (int j = 0; j <= i; j++)
  92.         { // Следующее слагаемое полинома
  93.             double Composition = 1;
  94.             for (int k = 0; k <= i; k++)
  95.             { // Считаем знаменатель разделенной разности
  96.                 if (k != j) Composition *= (Array[j].x - Array[k].x);
  97.             }
  98.             Polynom += Array[j].y / Composition; // Считаем разделенную разность
  99.         }
  100.         for (int k = 0; k < i; k++) Polynom *= (x - Array[k].x); // Домножаем разделенную разность на скобки (x-x[0])...(x-x[i-1])
  101.         Result += Polynom; // Полином
  102.     }
  103.     return Result;
  104. }
  105.  
  106. void CoordinateSystemAndFunction(functiontype *f, double Initial, double End)
  107. { // Центр 100,200
  108.     HDC hDC = GetDC( GetConsoleWindow( ) ); // Получаем контекст устройства по полученному дескриптору
  109.     HPEN PenWhite = CreatePen( PS_SOLID, 2, RGB(255, 255, 255)); // Задается перо (стиль,ширина,цвет)
  110.     HPEN PenGreen = CreatePen( PS_SOLID, 2, RGB(0, 128, 0));
  111.     SelectObject( hDC, PenWhite ); // Выбираем элемент рисования
  112.    
  113.     // Рисуем оси координат
  114.     MoveToEx( hDC, 0, 200, NULL ); // Ставим точку горизонталь
  115.     LineTo( hDC, 200, 200 ); // Соеденяем
  116.     MoveToEx( hDC, 100, 100, NULL ); // Ставим точку вертикаль
  117.     LineTo( hDC, 100, 300 ); // Соеденяем
  118.    
  119.     // График интерполируемой функции
  120.     SelectObject( hDC, PenGreen );
  121.     for (double x = Initial; x <= End; x += 0.01)
  122.     {
  123.         MoveToEx( hDC, 30 * x + 100, -30 * (*f)(x) + 200, NULL ); // 20 - Масштаб
  124.         LineTo( hDC, 30 * x + 100, -30 * (*f)(x) + 200 );
  125.     }
  126. }
  127.  
  128. void AbsoluteError(functiontype *Function, double Initial, double End, Node *Array, int Count)
  129. { // Абсолютная погрешность
  130.     HDC hDC = GetDC( GetConsoleWindow( ) ); // Получаем контекст устройства по полученному дескриптору
  131.     HPEN PenBlue = CreatePen( PS_SOLID, 2, RGB(0, 0, 255));
  132.     SelectObject( hDC, PenBlue );
  133.     for (double x = Initial; x <= End; x += 0.01f)
  134.     {
  135.         MoveToEx( hDC, 30 * x + 100, -30 * abs((*Function)(x) - PolynomLG(x,Array,Count)) + 200, NULL ); // 20 - Масштаб
  136.         LineTo( hDC, 30 * x + 100, -30 * abs((*Function)(x) - PolynomLG(x,Array,Count)) + 200 );
  137.     }
  138. }
  139.  
  140. double DFunc(functiontype *func, double x, int n)
  141. { // Производная функции
  142.     double h = 0.0001;
  143.     if (n == 1)
  144.     {
  145.         return ((*func)(x + h) - (*func)(x)) / h;
  146.     }
  147.     else
  148.     {
  149.         return (DFunc(func, x + h, n - 1) - DFunc(func, x, n - 1)) / h;
  150.     }
  151. }
  152.  
  153. double test(functiontype *func, double x, int n, long double h)
  154. { // Производная функции
  155.     h = 0.0001;
  156.     while (n > 1)
  157.     {
  158.         return (test(func, x + h, n - 1, h / pow(10,-2)) - test(func,x - h,n - 1,h / pow(10,-2))) / 2 * h;
  159.     }
  160.     if (n == 1)
  161.     {
  162.         return ((*func)(x + h) - (*func)(x - h)) / 2 * h;
  163.     }
  164. }
  165.  
  166. double DividedDifference(int i, Node *Array)
  167. { // Разделенная разность
  168.     double DD = 0;
  169.     for (int j = 0; j <= i; j++)
  170.     {
  171.         double tmp = 1;
  172.         for (int k = 0; k <= i; k++)
  173.         {
  174.             if (k != j)
  175.                 tmp *= Array[j].x - Array[k].x;
  176.         }
  177.         DD += Array[j].y / tmp;
  178.     }
  179.     return DD;
  180. }
  181.  
  182. double W(double x, int n, Node *Array)
  183. { // Полином вида: (x - x1) * (x - x2) * ... * (x - xn)
  184.     double w = 1;
  185.     for (int i = 1; i <= n; i++)
  186.     {
  187.         w *= x - Array[i].x;
  188.     }
  189.     return w;
  190. }
  191.  
  192. void ApproximateError(functiontype *Function, double Initial, double End, Node *Array, int Count)
  193. { // Приближенная погрешность
  194.     HDC hDC = GetDC( GetConsoleWindow( ) ); // Получаем контекст устройства по полученному дескриптору
  195.     HPEN PenYellow = CreatePen( PS_SOLID, 2, RGB(255, 255, 0));
  196.     SelectObject( hDC, PenYellow );
  197.     for (double x = Initial; x <= End; x += 0.01f)
  198.     {
  199.         MoveToEx( hDC, 30 * x + 100, -30 * abs(exp(End) * W(x,Count,Array) / Factorial(Count + 1)) + 200, NULL ); // 20 - Масштаб
  200.         LineTo( hDC, 30 * x + 100, -30 * abs(exp(End) * W(x,Count,Array) / Factorial(Count + 1)) + 200 );
  201.     }
  202. }
  203.  
  204. int main()
  205. {
  206.     Interval Interval;
  207.     int CountNodes;
  208.     functiontype Func = &exponenta;
  209.  
  210.     cout << "Enter the interval: " << endl;
  211.     cin >> Interval.InitialNode >> Interval.EndNode;
  212.     cout << "Enter the number of nodes: " << endl;
  213.     cin >> CountNodes;
  214.  
  215.     Node *ArrayUniformNodes = new Node[CountNodes]; // Массив с равномерными узлами
  216.     Node *ArrayChebyshevNodes = new Node[CountNodes]; // Массив с Чебышевскими узлами
  217.     ValueUniformTable(&Func,ArrayUniformNodes,Interval.InitialNode,Interval.EndNode,CountNodes); // Заполнение таблицы равномерных значений
  218.     ValueChebyshevTable(&Func,ArrayChebyshevNodes,Interval.InitialNode,Interval.EndNode,CountNodes); // Заполнение таблицы Чебышевских значений
  219.  
  220.     //cout << "PolynomLG_Uni: " << PolynomLG(1,ArrayUniformNodes,CountNodes) << endl;
  221.     //cout << "PolynomN_Uni: " << PolynomN(1,ArrayUniformNodes,CountNodes) << endl;
  222.  
  223.     //cout << "PolynomLG_Chb: " << PolynomLG(1,ArrayChebyshevNodes,CountNodes) << endl;
  224.     //cout << "PolynomN_Chb: " << PolynomN(1,ArrayChebyshevNodes,CountNodes) << endl;
  225.  
  226.     CoordinateSystemAndFunction(&Func,Interval.InitialNode,Interval.EndNode);
  227.    
  228.     HDC hDC = GetDC( GetConsoleWindow( ) ); // Получаем контекст устройства по полученному дескриптору
  229.    
  230.     // График Полинома Лагранжа
  231.     /*HPEN PenRed = CreatePen( PS_SOLID, 2, RGB(255, 0, 0));
  232.     SelectObject( hDC, PenRed );
  233.     for (double x = Interval.InitialNode; x <= Interval.EndNode; x += 0.01 )
  234.     {
  235.         MoveToEx( hDC, 30 * x + 100, -30 * PolynomLG(x,ArrayUniformNodes,CountNodes) + 200, NULL ); // 20 - Масштаб
  236.         LineTo( hDC, 30 * x + 100, -30 * PolynomLG(x,ArrayUniformNodes,CountNodes) + 200 );
  237.     }*/
  238.  
  239.     // График Полинома Ньютона
  240.     HPEN Pentest = CreatePen( PS_SOLID, 2, RGB(255, 0, 255));
  241.     SelectObject( hDC, Pentest );
  242.     for (double x = Interval.InitialNode; x <= Interval.EndNode; x += 0.01 )
  243.     {
  244.         MoveToEx( hDC, 20 * x + 100, -20 * PolynomN(x,ArrayChebyshevNodes,CountNodes) + 200, NULL ); // 20 - Масштаб
  245.         LineTo( hDC, 20 * x + 100, -20 * PolynomN(x,ArrayChebyshevNodes,CountNodes) + 200 );
  246.     }
  247.  
  248.     // Абсолютная погрешность
  249.     AbsoluteError(&Func,Interval.InitialNode,Interval.EndNode,ArrayUniformNodes,CountNodes);
  250.     // Приближенная погрешность
  251.     ApproximateError(&Func,Interval.InitialNode,Interval.EndNode,ArrayUniformNodes,CountNodes);
  252.  
  253.     system("pause");
  254.     return 0;
  255. }
Add Comment
Please, Sign In to add comment