vadimk772336

Untitled

Dec 16th, 2019
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 12.98 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 * (sqrt(4 - x * 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) * ((*f)(Array[i + 1].x) + (*f)(Array[i].x)) / 2;
  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, area = 0;
  68.     for (i = 0; i < CountSegments; i++)
  69.     {
  70.         x1 = (Array[i + 1].x + Array[i].x) / 2 + (Array[i + 1].x - Array[i].x) * sqrt(3) / 6;
  71.         x2 = (Array[i + 1].x + Array[i].x) / 2 - (Array[i + 1].x - Array[i].x) * sqrt(3) / 6;
  72.         area += (Array[i + 1].x - Array[i].x) * ((*f)(x1) + (*f)(x2)) / 2;
  73.     }
  74.  
  75.     return area;
  76. }
  77. double orig_integral(double Initial, double End)
  78. {
  79.     return (pow((4 - Initial * Initial), 1.5) - pow((4 - End * End), 1.5)) / 3;
  80. }
  81.  
  82. //Заполняет переданный массив эксп порядоком точности для трапец и Гаусса соот-но
  83. void exp_order_accuracy(int CountSegments, functiontype* f, double Initial, double End, double* massiv_exp_accuracy) {
  84.  
  85.     double orig_square_exp, R_G, R_G2, R_T, R_T2, exp_p_G, exp_p_T;
  86.     double Gauss_square_exp, Gauss_square_exp2, trapezoid_square_exp, trapezoid_square_exp2;
  87.  
  88.  
  89.     Node* Array2Nodes = new Node[2 * CountSegments + 1];
  90.     Node* ArrayNodes = new Node[CountSegments + 1];
  91.     ValueUniformTable(f, Array2Nodes, Initial, End, 2 * CountSegments + 1);
  92.     ValueUniformTable(f, ArrayNodes, Initial, End, CountSegments + 1);
  93.     Gauss_square_exp = Gauss_formula(ArrayNodes, f, CountSegments);
  94.     Gauss_square_exp2 = Gauss_formula(Array2Nodes, f, 2 * CountSegments);
  95.     trapezoid_square_exp = trapezoid_formula(ArrayNodes, f, CountSegments);
  96.     trapezoid_square_exp2 = trapezoid_formula(Array2Nodes, f, 2 * CountSegments);
  97.     orig_square_exp = orig_integral(Initial, End);
  98.  
  99.     R_G = abs(orig_square_exp - Gauss_square_exp);
  100.     R_G2 = abs(orig_square_exp - Gauss_square_exp2);
  101.     R_T = abs(orig_square_exp - trapezoid_square_exp);
  102.     R_T2 = abs(orig_square_exp - trapezoid_square_exp2);
  103.  
  104.     exp_p_G = log(abs(R_G / R_G2)) / log(2);
  105.     exp_p_T = log(abs(R_T / R_T2)) / log(2);
  106.  
  107.     massiv_exp_accuracy[0] = exp_p_T;
  108.     massiv_exp_accuracy[1] = exp_p_G;
  109.  
  110. }
  111.  
  112. //Заполняет переданный массив погрешностью полученной по правилу Рунге для трапец и Гаусса соот-но
  113. void Runge_Err(functiontype* f, int CountSegments, double* massiv_data, double Initial, double End)
  114. {
  115.     functiontype Func = &Myfunc;
  116.     int p_trap = 2, p_Gauss = 4;
  117.     double numerator_trap, numerator_Gauss, denominator_Gauss, denominator_Trap, R_trap, R_Gauss;
  118.  
  119.     Node* Array4 = new Node[4 * CountSegments + 1];
  120.     Node* Array2 = new Node[2 * CountSegments + 1];
  121.     Node* Array = new Node[CountSegments + 1];
  122.     ValueUniformTable(f, Array4, Initial, End, 4 * CountSegments + 1);
  123.     ValueUniformTable(f, Array2, Initial, End, 2 * CountSegments + 1);
  124.     ValueUniformTable(f, Array, Initial, End, CountSegments + 1);
  125.  
  126.     numerator_trap = abs((trapezoid_formula(Array2, f, 2 * CountSegments) - trapezoid_formula(Array, f, CountSegments)))* pow(2, p_trap);
  127.     numerator_Gauss = abs((Gauss_formula(Array2, f, 2 * CountSegments) - Gauss_formula(Array, f, CountSegments)))* pow(2, p_Gauss);
  128.  
  129.     denominator_Gauss = (pow(2, p_Gauss) - 1);
  130.     denominator_Trap = (pow(2, p_trap) - 1);
  131.  
  132.     R_trap = numerator_trap / denominator_Trap;
  133.     R_Gauss = numerator_Gauss / denominator_Gauss;
  134.  
  135.     massiv_data[0] = R_trap;
  136.     massiv_data[1] = R_Gauss;
  137. }
  138.  
  139. //Заполняет переданный массив порядком точности на основе правила Рунге для трапец и Гаусса соот-но
  140. void Runge_order_accuracy(int CountSegments, functiontype* f, double Initial, double End, double* order_massiv) {
  141.  
  142.     double our_numerator_trap, our_numerator_Gauss, our_denominator_Gauss, our_denominator_trap, our_accuracy_trap, our_accuracy_Gauss;
  143.  
  144.     Node* Array4 = new Node[4 * CountSegments + 1];
  145.     Node* Array2 = new Node[2 * CountSegments + 1];
  146.     Node* Array = new Node[CountSegments + 1];
  147.     ValueUniformTable(f, Array4, Initial, End, 4 * CountSegments + 1);
  148.     ValueUniformTable(f, Array2, Initial, End, 2 * CountSegments + 1);
  149.     ValueUniformTable(f, Array, Initial, End, CountSegments + 1);
  150.  
  151.     our_numerator_trap = abs(trapezoid_formula(Array2, f, 2 * CountSegments) - trapezoid_formula(Array, f, CountSegments));
  152.     our_numerator_Gauss = abs(Gauss_formula(Array2, f, 2 * CountSegments) - Gauss_formula(Array, f, CountSegments));
  153.     our_denominator_Gauss = abs(Gauss_formula(Array2, f, 2 * CountSegments) - Gauss_formula(Array4, f, 4 * CountSegments));
  154.     our_denominator_trap = abs(trapezoid_formula(Array2, f, 2 * CountSegments) - trapezoid_formula(Array4, f, 4 * CountSegments));
  155.  
  156.     our_accuracy_trap = log(abs(our_numerator_trap / our_denominator_trap)) / log(2);
  157.     our_accuracy_Gauss = log(abs(our_numerator_Gauss / our_denominator_Gauss)) / log(2);
  158.  
  159.     order_massiv[0] = our_accuracy_trap;
  160.     order_massiv[1] = our_accuracy_Gauss;
  161.  
  162. }
  163.  
  164. //Строит график эксп точности
  165. void graphic_exp_acc(functiontype* f, double Initial, double End, int СountDots) {
  166.     ofstream Trap_File("H:/exp_accuracy_T.txt");
  167.     ofstream Gauss_File("H:/exp_accuracy_G.txt");
  168.     double* massiv_exp_accuracy = new double[2];
  169.     for (int n = 1; n < СountDots; n++) {
  170.         massiv_exp_accuracy[0] = 0;
  171.         massiv_exp_accuracy[1] = 0;
  172.         exp_order_accuracy(n, f, Initial, End, massiv_exp_accuracy);
  173.         Trap_File << n << " " << massiv_exp_accuracy[0] << endl;
  174.         Gauss_File << n << " " << massiv_exp_accuracy[1] << endl;
  175.     }
  176. }
  177.  
  178. //Строит график точности по Рунге
  179. void graphic_Runge_acc(functiontype* f, double Initial, double End, int СountDots) {
  180.     ofstream Trap_File("H:/Runge_accuracy_T.txt");
  181.     ofstream Gauss_File("H:/Runge_accuracy_G.txt");
  182.     double* massiv_Runge_accuracy = new double[2];
  183.     for (int n = 1; n < СountDots; n++) {
  184.         massiv_Runge_accuracy[0] = 0;
  185.         massiv_Runge_accuracy[1] = 0;
  186.         Runge_order_accuracy(n, f, Initial, End, massiv_Runge_accuracy);
  187.         Trap_File << n << " " << massiv_Runge_accuracy[0] << endl;
  188.         Gauss_File << n << " " << massiv_Runge_accuracy[1] << endl;
  189.     }
  190. }
  191.  
  192. //Строит график погрешности по Рунге
  193. void graph_Runge_error(functiontype* f, double Initial, double End, int СountDots) {
  194.     ofstream Trap_error_file("H:/Runge_error_T.txt");
  195.     ofstream Gauss_error_file("H:/Runge_error_G.txt");
  196.  
  197.     double* Runge_eror_massiv = new double[2];
  198.     for (int n = 1; n < СountDots; n++) {
  199.         Runge_eror_massiv[0] = 0;
  200.         Runge_eror_massiv[1] = 0;
  201.         Runge_Err(f, n, Runge_eror_massiv, Initial, End);
  202.         Trap_error_file << n << " " << Runge_eror_massiv[0] << endl;
  203.         Gauss_error_file << n << " " << Runge_eror_massiv[1] << endl;
  204.     }
  205. }
  206.  
  207. //Строит график абсолютной погрешности
  208. void graph_abs_error(Node* Array, functiontype* f, double Initial, double End, int СountDots) {
  209.     ofstream Trap_error_file("H:/abs_error_T.txt");
  210.     ofstream Gauss_error_file("H:/abs_error_G.txt");
  211.  
  212.     for (int n = 1; n < СountDots; n++) {
  213.         Trap_error_file << n << " " << abs(orig_integral(Initial, End) - trapezoid_formula(Array, f, n)) << endl;
  214.         Gauss_error_file << n << " " << abs(orig_integral(Initial, End) - Gauss_formula(Array, f, n)) << endl;
  215.     }
  216.     Trap_error_file.close();
  217.     Gauss_error_file.close();
  218. }
  219.  
  220.  
  221. void PrintNodes(Node* Array, int CountSegments)
  222. {
  223.     int i;
  224.     for (i = 0; i < CountSegments - 1; i++)
  225.         cout << "(" << (Array[i].x) << ":" << (Array[i + 1].x) << ")" << endl;
  226. }
  227.  
  228.  
  229. int main()
  230. {
  231.     setlocale(LC_ALL, "RUS");
  232.     functiontype Func = &Myfunc;
  233.     Interval Interval; Interval.InitialNode = 0;
  234.     Interval.EndNode = 1.5;
  235.     //Interval.EndNode = 1.5;
  236.     int CountSegments, СountDots = 1000;
  237.     cout << "Введите число интервалов разбиения: " << endl; cin >> CountSegments; cout << endl;
  238.     int CountNodes = CountSegments + 1;
  239.  
  240.     Node* ArrayUniformNodes = new Node[CountNodes];
  241.     ValueUniformTable(&Func, ArrayUniformNodes, Interval.InitialNode, Interval.EndNode, CountNodes);
  242.  
  243.     /*cout << "Разбиение интервала на равные отрезки интегрирования:" << endl;
  244.     PrintNodes(ArrayUniformNodes, CountNodes);
  245.     cout << endl;*/
  246.  
  247.  
  248.     cout << "Трапеция:" << endl;
  249.     double trapezoid_square = trapezoid_formula(ArrayUniformNodes, &Func, CountSegments);
  250.     cout << trapezoid_square << endl << endl;
  251.  
  252.  
  253.     cout << "Гаусс:" << endl;
  254.     double Gauss_square = Gauss_formula(ArrayUniformNodes, &Func, CountSegments);
  255.     cout << Gauss_square << endl << endl;
  256.  
  257.     cout << "Интеграл:" << endl;
  258.     double orig_square = orig_integral(Interval.InitialNode, Interval.EndNode);
  259.     cout << orig_square << endl << endl;
  260.  
  261.     cout << "Абс. погрешность вычисления по формуле Гаусса:" << endl;
  262.     cout << abs(orig_square - Gauss_square) << endl << endl;
  263.  
  264.     cout << "Абс. погрешность вычисления по формуле Трапеций:" << endl;
  265.     cout << abs(orig_square - trapezoid_square) << endl << endl;
  266.  
  267.  
  268.     /* -----------------Реализация правила Рунге для оценки погрешности -----------------------------*/
  269.     double* massiv_data = new double[4];
  270.     double* order_massiv = new double[4];
  271.     Runge_Err(&Func, CountSegments, massiv_data, Interval.InitialNode, Interval.EndNode);
  272.     Runge_order_accuracy(CountSegments, &Func, Interval.InitialNode, Interval.EndNode, order_massiv);
  273.  
  274.     cout << "Оценка погрешности по Рунге для формулы трапеций:" << endl;
  275.     cout << massiv_data[0] << endl << endl;
  276.     cout << "Оценка погрешности по Рунге для формулы Гаусса:" << endl;
  277.     cout << massiv_data[1] << endl << endl;
  278.  
  279.     //График
  280.     graph_Runge_error(&Func, Interval.InitialNode, Interval.EndNode, СountDots);
  281.     /* -----------------------Конец -----------------------------------*/
  282.  
  283.     /* -----------------Порядок точности на основе правила Рунге -----------------------------*/
  284.     cout << "Оценка точности на основе правила Рунге для формулы трапеций:" << endl;
  285.     cout << order_massiv[0] << endl << endl;
  286.     cout << "Оценка точности на основе правила Рунге для для формулы Гаусса:" << endl;
  287.     cout << order_massiv[1] << endl << endl;
  288.  
  289.     //График
  290.     graphic_Runge_acc(&Func, Interval.InitialNode, Interval.EndNode, СountDots);
  291.     /* -----------------------Конец -----------------------------------*/
  292.  
  293.     /* -----------------Экспериментальный порядок точности   -----------------------------*/
  294.     double* massiv_exp_accuracy = new double[2];
  295.     exp_order_accuracy(CountSegments, &Func, Interval.InitialNode, Interval.EndNode, massiv_exp_accuracy);
  296.     cout << "Эксп-ый порядок точности для Гаусса и Трапеций соответственно:" << endl;
  297.     cout << massiv_exp_accuracy[1] << " " << massiv_exp_accuracy[0] << endl << endl;
  298.  
  299.     //График
  300.     graphic_exp_acc(&Func, Interval.InitialNode, Interval.EndNode, СountDots);
  301.     /* -------------------------   Конец --------------------------------------*/
  302.  
  303.     //График абсолютной погрешности
  304.     graph_abs_error(ArrayUniformNodes, &Func, Interval.InitialNode, Interval.EndNode, СountDots);
  305.  
  306.  
  307.     cout << endl;
  308.     system("pause");
  309.     return 0;
  310. }
Advertisement
Add Comment
Please, Sign In to add comment