vadimk772336

Untitled

Dec 24th, 2019
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 13.39 KB | None | 0 0
  1. /*
  2. //Эксп ПТ
  3. plot "H:/exp_accuracy_T.txt" with lines title "Эксп ПТ Трапец", "H:/exp_accuracy_G.txt" with lines title "Эксп ПТ Гаусс"
  4.  
  5. //Точность Рунге
  6. plot "H:/Runge_accuracy_T.txt" with lines title "Точность Рунге Трапец","H:/Runge_accuracy_G.txt" with lines title "Точность Рунге Гаус"
  7.  
  8. //Погреш Рунге
  9. plot "H:/Runge_error_T.txt" with lines title "Погр Рунге Трап", "H:/Runge_error_G.txt" with lines title "Погр Рунге Гаусс"
  10.  
  11. plot "H:/abs_error_T.txt" with lines title "Абс погр Трапец", "H:/abs_error_G.txt" with lines title "Абс погр Гаусс"
  12. */
  13.  
  14. #include <iostream>
  15. #include <math.h>
  16. #include <cmath>
  17. #include <vector>
  18. #include <Windows.h>
  19. #include <stdlib.h>
  20. #include <fstream>
  21. #include <string.h>
  22. #include <time.h>
  23.  
  24. using namespace std;
  25.  
  26. typedef double(*functiontype)(double x);
  27. typedef struct Node
  28. {
  29.     double x, y;
  30. } Node;
  31. typedef double(*method)(double x, Node* Array, int Count, double* DD_massiv);
  32. typedef struct Interval
  33. {
  34.     double InitialNode, EndNode;
  35. } Interval;
  36. void ValueUniformTable(functiontype* f, Node* Array, double Initial, double End, int CountNodes)
  37.  
  38. {
  39.     double step = abs(Initial - End) / (CountNodes - 1);
  40.     Array[0].x = Initial;
  41.     Array[0].y = (*f)(Array[0].x);
  42.     for (int i = 1; i < CountNodes; i++)
  43.     {
  44.         Array[i].x = Array[i - 1].x + step;
  45.     }
  46. }
  47. double Myfunc(double x)
  48. {
  49.     return x * sin(x);
  50. }
  51. functiontype Func = &Myfunc;
  52. double trapezoid_formula(Node* Array, functiontype* f, int CountSegments)
  53. { //Теор. порядок точности = 2
  54.     int i;
  55.     double area = 0;
  56.  
  57.     for (i = 0; i < CountSegments; i++) {
  58.         area += ((Array[i + 1].x - Array[i].x) / 6) * ((*f)(Array[i + 1].x) + (4 * (*f)(((Array[i].x) + (Array[i + 1].x)) / 2)) + (*f)(Array[i].x));
  59.     }
  60.     return area;
  61. }
  62. double Gauss_formula(Node* Array, functiontype* f, int CountSegments)
  63. { //Теор. порядок точности = 4
  64.  
  65.  
  66.     int i;
  67.     double x1, x2, x3, area = 0;
  68.  
  69.     double t1, t2, t3 = 0;
  70.     t3 = -sqrt(0.6); t1 = sqrt(0.6); t2 = 0;
  71.  
  72.  
  73.     double a1 = 5/9;
  74.     double a2 = 8/9;
  75.     double a3 = 5/9;
  76.  
  77.     for (i = 0; i < CountSegments; i++)
  78.     {
  79.         x1 = ((Array[i + 1].x + Array[i].x) / 2) + ((Array[i + 1].x - Array[i].x) * t1) / 2;
  80.         x2 = ((Array[i + 1].x + Array[i].x) / 2);
  81.         x3 = ((Array[i + 1].x + Array[i].x) / 2) + ((Array[i + 1].x - Array[i].x) * t3) / 2;
  82.         //area += ((Array[i + 1].x - Array[i].x) / 2) * ((a1 * (*f)(x1) + a2 * (*f)(x2) + a3 * (*f)(x3)));
  83.         area += ((Array[i + 1].x - Array[i].x) / 2) * ((a1 * (*f)(t1) + a2 * (*f)(0) + a3 * (*f)(t3)));
  84.     }
  85.  
  86.     return area;
  87. }
  88. double orig_integral(double Initial, double End)
  89. {
  90.     return (sin(End) - End * (cos(End))) - (sin(Initial)) - Initial * (cos(Initial));
  91. }
  92.  
  93. //Заполняет переданный массив эксп порядоком точности для трапец и Гаусса соот-но
  94. void exp_order_accuracy(int CountSegments, functiontype* f, double Initial, double End, double* massiv_exp_accuracy) {
  95.  
  96.     double orig_square_exp, R_G, R_G2, R_T, R_T2, exp_p_G, exp_p_T;
  97.     double Gauss_square_exp, Gauss_square_exp2, trapezoid_square_exp, trapezoid_square_exp2;
  98.  
  99.  
  100.     Node* Array2Nodes = new Node[2 * CountSegments + 1];
  101.     Node* ArrayNodes = new Node[CountSegments + 1];
  102.     ValueUniformTable(f, Array2Nodes, Initial, End, 2 * CountSegments + 1);
  103.     ValueUniformTable(f, ArrayNodes, Initial, End, CountSegments + 1);
  104.     Gauss_square_exp = Gauss_formula(ArrayNodes, f, CountSegments);
  105.     Gauss_square_exp2 = Gauss_formula(Array2Nodes, f, 2 * CountSegments);
  106.     trapezoid_square_exp = trapezoid_formula(ArrayNodes, f, CountSegments);
  107.     trapezoid_square_exp2 = trapezoid_formula(Array2Nodes, f, 2 * CountSegments);
  108.     orig_square_exp = orig_integral(Initial, End);
  109.  
  110.     R_G = abs(orig_square_exp - Gauss_square_exp);
  111.     R_G2 = abs(orig_square_exp - Gauss_square_exp2);
  112.     R_T = abs(orig_square_exp - trapezoid_square_exp);
  113.     R_T2 = abs(orig_square_exp - trapezoid_square_exp2);
  114.  
  115.     exp_p_G = log(abs(R_G / R_G2)) / log(2);
  116.     exp_p_T = log(abs(R_T / R_T2)) / log(2);
  117.  
  118.     massiv_exp_accuracy[0] = exp_p_T;
  119.     massiv_exp_accuracy[1] = exp_p_G;
  120.  
  121. }
  122.  
  123. //Заполняет переданный массив погрешностью полученной по правилу Рунге для трапец и Гаусса соот-но
  124. void Runge_Err(functiontype* f, int CountSegments, double* massiv_data, double Initial, double End)
  125. {
  126.     functiontype Func = &Myfunc;
  127.     int p_trap = 2, p_Gauss = 4;
  128.     double numerator_trap, numerator_Gauss, denominator_Gauss, denominator_Trap, R_trap, R_Gauss;
  129.  
  130.     Node* Array4 = new Node[4 * CountSegments + 1];
  131.     Node* Array2 = new Node[2 * CountSegments + 1];
  132.     Node* Array = new Node[CountSegments + 1];
  133.     ValueUniformTable(f, Array4, Initial, End, 4 * CountSegments + 1);
  134.     ValueUniformTable(f, Array2, Initial, End, 2 * CountSegments + 1);
  135.     ValueUniformTable(f, Array, Initial, End, CountSegments + 1);
  136.  
  137.     numerator_trap = abs((trapezoid_formula(Array2, f, 2 * CountSegments) - trapezoid_formula(Array, f, CountSegments)))* pow(2, p_trap);
  138.     numerator_Gauss = abs((Gauss_formula(Array2, f, 2 * CountSegments) - Gauss_formula(Array, f, CountSegments)))* pow(2, p_Gauss);
  139.  
  140.     denominator_Gauss = (pow(2, p_Gauss) - 1);
  141.     denominator_Trap = (pow(2, p_trap) - 1);
  142.  
  143.     R_trap = numerator_trap / denominator_Trap;
  144.     R_Gauss = numerator_Gauss / denominator_Gauss;
  145.  
  146.     massiv_data[0] = R_trap;
  147.     massiv_data[1] = R_Gauss;
  148. }
  149.  
  150. //Заполняет переданный массив порядком точности на основе правила Рунге для трапец и Гаусса соот-но
  151. void Runge_order_accuracy(int CountSegments, functiontype* f, double Initial, double End, double* order_massiv) {
  152.  
  153.     double our_numerator_trap, our_numerator_Gauss, our_denominator_Gauss, our_denominator_trap, our_accuracy_trap, our_accuracy_Gauss;
  154.  
  155.     Node* Array4 = new Node[4 * CountSegments + 1];
  156.     Node* Array2 = new Node[2 * CountSegments + 1];
  157.     Node* Array = new Node[CountSegments + 1];
  158.     ValueUniformTable(f, Array4, Initial, End, 4 * CountSegments + 1);
  159.     ValueUniformTable(f, Array2, Initial, End, 2 * CountSegments + 1);
  160.     ValueUniformTable(f, Array, Initial, End, CountSegments + 1);
  161.  
  162.     our_numerator_trap = abs(trapezoid_formula(Array2, f, 2 * CountSegments) - trapezoid_formula(Array, f, CountSegments));
  163.     our_numerator_Gauss = abs(Gauss_formula(Array2, f, 2 * CountSegments) - Gauss_formula(Array, f, CountSegments));
  164.     our_denominator_Gauss = abs(Gauss_formula(Array2, f, 2 * CountSegments) - Gauss_formula(Array4, f, 4 * CountSegments));
  165.     our_denominator_trap = abs(trapezoid_formula(Array2, f, 2 * CountSegments) - trapezoid_formula(Array4, f, 4 * CountSegments));
  166.  
  167.     our_accuracy_trap = log(abs(our_numerator_trap / our_denominator_trap)) / log(2);
  168.     our_accuracy_Gauss = log(abs(our_numerator_Gauss / our_denominator_Gauss)) / log(2);
  169.  
  170.     order_massiv[0] = our_accuracy_trap;
  171.     order_massiv[1] = our_accuracy_Gauss;
  172.  
  173. }
  174.  
  175. //Строит график эксп точности
  176. void graphic_exp_acc(functiontype* f, double Initial, double End, int СountDots) {
  177.     ofstream Trap_File("H:/exp_accuracy_T.txt");
  178.     ofstream Gauss_File("H:/exp_accuracy_G.txt");
  179.     double* massiv_exp_accuracy = new double[2];
  180.     for (int n = 1; n < СountDots; n++) {
  181.         massiv_exp_accuracy[0] = 0;
  182.         massiv_exp_accuracy[1] = 0;
  183.         exp_order_accuracy(n, f, Initial, End, massiv_exp_accuracy);
  184.         Trap_File << n << " " << massiv_exp_accuracy[0] << endl;
  185.         Gauss_File << n << " " << massiv_exp_accuracy[1] << endl;
  186.     }
  187. }
  188.  
  189. //Строит график точности по Рунге
  190. void graphic_Runge_acc(functiontype* f, double Initial, double End, int СountDots) {
  191.     ofstream Trap_File("H:/Runge_accuracy_T.txt");
  192.     ofstream Gauss_File("H:/Runge_accuracy_G.txt");
  193.     double* massiv_Runge_accuracy = new double[2];
  194.     for (int n = 1; n < СountDots; n++) {
  195.         massiv_Runge_accuracy[0] = 0;
  196.         massiv_Runge_accuracy[1] = 0;
  197.         Runge_order_accuracy(n, f, Initial, End, massiv_Runge_accuracy);
  198.         Trap_File << n << " " << massiv_Runge_accuracy[0] << endl;
  199.         Gauss_File << n << " " << massiv_Runge_accuracy[1] << endl;
  200.     }
  201. }
  202.  
  203. //Строит график погрешности по Рунге
  204. void graph_Runge_error(functiontype* f, double Initial, double End, int СountDots) {
  205.     ofstream Trap_error_file("H:/Runge_error_T.txt");
  206.     ofstream Gauss_error_file("H:/Runge_error_G.txt");
  207.  
  208.     double* Runge_eror_massiv = new double[2];
  209.     for (int n = 1; n < СountDots; n++) {
  210.         Runge_eror_massiv[0] = 0;
  211.         Runge_eror_massiv[1] = 0;
  212.         Runge_Err(f, n, Runge_eror_massiv, Initial, End);
  213.         Trap_error_file << n << " " << Runge_eror_massiv[0] << endl;
  214.         Gauss_error_file << n << " " << Runge_eror_massiv[1] << endl;
  215.     }
  216. }
  217.  
  218. //Строит график абсолютной погрешности
  219. void graph_abs_error(Node* Array, functiontype* f, double Initial, double End, int СountDots) {
  220.     ofstream Trap_error_file("H:/abs_error_T.txt");
  221.     ofstream Gauss_error_file("H:/abs_error_G.txt");
  222.  
  223.     for (int n = 1; n < СountDots; n++) {
  224.         Trap_error_file << n << " " << abs(orig_integral(Initial, End) - trapezoid_formula(Array, f, n)) << endl;
  225.         Gauss_error_file << n << " " << abs(orig_integral(Initial, End) - Gauss_formula(Array, f, n)) << endl;
  226.     }
  227.     Trap_error_file.close();
  228.     Gauss_error_file.close();
  229. }
  230.  
  231.  
  232. void PrintNodes(Node* Array, int CountSegments)
  233. {
  234.     int i;
  235.     for (i = 0; i < CountSegments - 1; i++)
  236.         cout << "(" << (Array[i].x) << ":" << (Array[i + 1].x) << ")" << endl;
  237. }
  238.  
  239.  
  240. int main()
  241. {
  242.     setlocale(LC_ALL, "RUS");
  243.     functiontype Func = &Myfunc;
  244.     Interval Interval; Interval.InitialNode = 0;
  245.     Interval.EndNode = 4;
  246.     //Interval.EndNode = 1.5;
  247.     int CountSegments, СountDots = 1000;
  248.     cout << "Введите число интервалов разбиения: " << endl; cin >> CountSegments; cout << endl;
  249.     int CountNodes = CountSegments + 1;
  250.  
  251.     Node* ArrayUniformNodes = new Node[CountNodes];
  252.     ValueUniformTable(&Func, ArrayUniformNodes, Interval.InitialNode, Interval.EndNode, CountNodes);
  253.  
  254.     /*cout << "Разбиение интервала на равные отрезки интегрирования:" << endl;
  255.     PrintNodes(ArrayUniformNodes, CountNodes);
  256.     cout << endl;*/
  257.  
  258.  
  259.     cout << "Трапеция:" << endl;
  260.     double trapezoid_square = trapezoid_formula(ArrayUniformNodes, &Func, CountSegments);
  261.     cout << trapezoid_square << endl << endl;
  262.  
  263.  
  264.     cout << "Гаусс:" << endl;
  265.     double Gauss_square = Gauss_formula(ArrayUniformNodes, &Func, CountSegments);
  266.     cout << Gauss_square << endl << endl;
  267.  
  268.     cout << "Интеграл:" << endl;
  269.     double orig_square = orig_integral(Interval.InitialNode, Interval.EndNode);
  270.     cout << orig_square << endl << endl;
  271.  
  272.     cout << "Абс. погрешность вычисления по формуле Гаусса по 3 узлам:" << endl;
  273.     cout << abs(orig_square - Gauss_square) << endl << endl;
  274.  
  275.     cout << "Абс. погрешность вычисления по формуле Трапеций:" << endl;
  276.     cout << abs(orig_square - trapezoid_square) << endl << endl;
  277.  
  278.  
  279.     /* -----------------Реализация правила Рунге для оценки погрешности -----------------------------*/
  280.     double* massiv_data = new double[4];
  281.     double* order_massiv = new double[4];
  282.     Runge_Err(&Func, CountSegments, massiv_data, Interval.InitialNode, Interval.EndNode);
  283.     Runge_order_accuracy(CountSegments, &Func, Interval.InitialNode, Interval.EndNode, order_massiv);
  284.  
  285.     cout << "Оценка погрешности по Рунге для формулы Симпсона:" << endl;
  286.     cout << massiv_data[0] << endl << endl;
  287.     cout << "Оценка погрешности по Рунге для формулы Гаусса по 3 узлам:" << endl;
  288.     cout << massiv_data[1] << endl << endl;
  289.  
  290.     //График
  291.     graph_Runge_error(&Func, Interval.InitialNode, Interval.EndNode, СountDots);
  292.     /* -----------------------Конец -----------------------------------*/
  293.  
  294.     /* -----------------Порядок точности на основе правила Рунге -----------------------------*/
  295.     cout << "Оценка точности на основе правила Рунге для формулы Симпсона:" << endl;
  296.     cout << order_massiv[0] << endl << endl;
  297.     cout << "Оценка точности на основе правила Рунге для для формулы Гаусса по 3 узлам:" << endl;
  298.     cout << order_massiv[1] << endl << endl;
  299.  
  300.     //График
  301.     graphic_Runge_acc(&Func, Interval.InitialNode, Interval.EndNode, СountDots);
  302.     /* -----------------------Конец -----------------------------------*/
  303.  
  304.     /* -----------------Экспериментальный порядок точности   -----------------------------*/
  305.     double* massiv_exp_accuracy = new double[2];
  306.     exp_order_accuracy(CountSegments, &Func, Interval.InitialNode, Interval.EndNode, massiv_exp_accuracy);
  307.     cout << "Эксп-ый порядок точности для Гаусса по 3 узлам и Cимпсона соответственно:" << endl;
  308.     cout << massiv_exp_accuracy[1] << " " << massiv_exp_accuracy[0] << endl << endl;
  309.  
  310.     //График
  311.     graphic_exp_acc(&Func, Interval.InitialNode, Interval.EndNode, СountDots);
  312.     /* -------------------------   Конец --------------------------------------*/
  313.  
  314.     //График абсолютной погрешности
  315.     graph_abs_error(ArrayUniformNodes, &Func, Interval.InitialNode, Interval.EndNode, СountDots);
  316.  
  317.  
  318.     cout << endl;
  319.     system("pause");
  320.     return 0;
  321. }
Advertisement
Add Comment
Please, Sign In to add comment