Advertisement
Guest User

Untitled

a guest
Dec 8th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.43 KB | None | 0 0
  1. #include <iostream>
  2. #include <math.h>
  3. #include <cmath>
  4. #include <vector>
  5. #include <Windows.h>
  6. #include <stdlib.h>
  7. #include <fstream>
  8. #include <string.h>
  9. using namespace std;
  10.  
  11. typedef double(*functiontype)(double x);
  12. typedef struct Node
  13. {
  14.     double x, y;
  15. } Node;
  16. typedef struct Interval
  17. {
  18.     double InitialNode, EndNode;
  19. } Interval;
  20. void ValueUniformTable(functiontype* f, Node* Array, double Initial, double End, int CountNodes)
  21.  
  22. {
  23.     double step = abs(Initial - End) / (CountNodes - 1);
  24.     Array[0].x = Initial;
  25.     Array[0].y = (*f)(Array[0].x);
  26.     for (int i = 1; i < CountNodes; i++)
  27.     {
  28.         Array[i].x = Array[i - 1].x + step;
  29.     }
  30. }
  31. double Myfunc(double x)
  32. {
  33.     return (abs(sin(x)));
  34. }
  35. functiontype Func = &Myfunc;
  36. double trapezoid_formula(Node* Array, functiontype* f, int CountSegments)
  37. { //Теор. порядок точности = 2
  38.     int i;
  39.     double area = 0;
  40.  
  41.     for (i = 0; i < CountSegments; i++) {
  42.         area += (Array[i + 1].x - Array[i].x) * ((*f)(Array[i + 1].x) + (*f)(Array[i].x)) / 2;
  43.     }
  44.     return area;
  45. }
  46. double Gauss_formula(Node* Array, functiontype* f, int CountSegments)
  47. { //Теор. порядок точности = 4
  48.  
  49.  
  50.     int i;
  51.     double x1, x2, area = 0;
  52.     for (i = 0; i < CountSegments; i++)
  53.     {
  54.         x1 = (Array[i + 1].x + Array[i].x) / 2 + (Array[i + 1].x - Array[i].x) * sqrt(3) / 6;
  55.         x2 = (Array[i + 1].x + Array[i].x) / 2 - (Array[i + 1].x - Array[i].x) * sqrt(3) / 6;
  56.         area += (Array[i + 1].x - Array[i].x) * ((*f)(x1) + (*f)(x2)) / 2;
  57.     }
  58.  
  59.     return area;
  60. }
  61. double orig_integral(double Initial, double End)
  62. {
  63.     return (2-cos(Initial)-cos(End));
  64. }
  65. void get_order_accuracy(int CountSegments, functiontype* f, double Initial, double End, double* massiv_exp_accuracy) {
  66.  
  67.     double orig_square_exp, R_G, R_G2, R_T, R_T2, exp_p_G, exp_p_T;
  68.     double Gauss_square_exp, Gauss_square_exp2, trapezoid_square_exp, trapezoid_square_exp2;
  69.  
  70.  
  71.     Node* Array2Nodes = new Node[2 * CountSegments + 1];
  72.     Node* ArrayNodes = new Node[CountSegments + 1];
  73.     ValueUniformTable(f, Array2Nodes, Initial, End, 2 * CountSegments + 1);
  74.     ValueUniformTable(f, ArrayNodes, Initial, End, CountSegments + 1);
  75.     Gauss_square_exp = Gauss_formula(ArrayNodes, f, CountSegments);
  76.     Gauss_square_exp2 = Gauss_formula(Array2Nodes, f, 2 * CountSegments);
  77.     trapezoid_square_exp = trapezoid_formula(ArrayNodes, f, CountSegments);
  78.     trapezoid_square_exp2 = trapezoid_formula(Array2Nodes, f, 2 * CountSegments);
  79.     orig_square_exp = orig_integral(Initial, End);
  80.  
  81.     R_G = abs(orig_square_exp - Gauss_square_exp);
  82.     R_G2 = abs(orig_square_exp - Gauss_square_exp2);
  83.     R_T = abs(orig_square_exp - trapezoid_square_exp);
  84.     R_T2 = abs(orig_square_exp - trapezoid_square_exp2);
  85.  
  86.     exp_p_G = log(abs(R_G / R_G2)) / log(2);
  87.     exp_p_T = log(abs(R_T / R_T2)) / log(2);
  88.  
  89.     massiv_exp_accuracy[0] = exp_p_T;
  90.     massiv_exp_accuracy[1] = exp_p_G;
  91.  
  92. }
  93. void graphic_exp(functiontype* f, double Initial, double End, int СountDots) {
  94.     ofstream Trap_File("H:/exp_accuracy_T.txt");
  95.     ofstream Gauss_File("H:/exp_accuracy_G.txt");
  96.     double* massiv_exp_accuracy = new double[2];
  97.     for (int n = 1; n < СountDots; n++) {
  98.         massiv_exp_accuracy[0] = 0;
  99.         massiv_exp_accuracy[1] = 0;
  100.         get_order_accuracy(n, f, Initial, End, massiv_exp_accuracy);
  101.         Trap_File << n << " " << massiv_exp_accuracy[0] << endl;
  102.         Gauss_File << n << " " << massiv_exp_accuracy[1] << endl;
  103.     }
  104. }
  105.  
  106. void graph_abs_error(Node* Array, functiontype* f, double Initial, double End, int СountDots) {
  107.     ofstream Trap_error_file("H:/abs_error_T.txt");
  108.     ofstream Gauss_error_file("H:/abs_error_G.txt");
  109.  
  110.     for (int n = 1; n < СountDots; n++) {
  111.         Trap_error_file << n << " " << abs(orig_integral(Initial, End) - trapezoid_formula(Array, f, n)) << endl;
  112.         Gauss_error_file << n << " " << abs(orig_integral(Initial, End) - Gauss_formula(Array, f, n)) << endl;
  113.     }
  114.     Trap_error_file.close();
  115.     Gauss_error_file.close();
  116. }
  117.  
  118. void PrintNodes(Node* Array, int CountSegments)
  119. {
  120.     int i;
  121.     for (i = 0; i < CountSegments - 1; i++)
  122.         cout << "(" << (Array[i].x) << ":" << (Array[i + 1].x) << ")" << endl;
  123. }
  124. void get_mas_pract_error(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.     double our_numerator_trap, our_numerator_Gauss, our_denominator_Gauss, our_denominator_trap, our_accuracy_trap, our_accuracy_Gauss;
  130.     double y = CountSegments, step = 3 / y;
  131.  
  132.     Node* Array4 = new Node[4 * CountSegments + 1];
  133.     Node* Array2 = new Node[2 * CountSegments + 1];
  134.     Node* Array = new Node[CountSegments + 1];
  135.     ValueUniformTable(f, Array4, Initial, End, 4 * CountSegments + 1);
  136.     ValueUniformTable(f, Array2, Initial, End, 2 * CountSegments + 1);
  137.     ValueUniformTable(f, Array, Initial, End, CountSegments + 1);  
  138.  
  139.     numerator_trap = abs((trapezoid_formula(Array2, f, 2 * CountSegments) - trapezoid_formula(Array, f, CountSegments))) * pow(2, p_trap);
  140.     numerator_Gauss = abs((Gauss_formula(Array2, f, 2 * CountSegments) - Gauss_formula(Array, f, CountSegments))) * pow(2, p_Gauss);
  141.  
  142.     our_numerator_trap = abs(trapezoid_formula(Array2, f, 2 * CountSegments) - trapezoid_formula(Array, f, CountSegments));
  143.     our_numerator_Gauss = abs(Gauss_formula(Array2, f, 2 * CountSegments) - Gauss_formula(Array, f, CountSegments));
  144.     our_denominator_Gauss = abs(Gauss_formula(Array2, f, 2 * CountSegments) - Gauss_formula(Array4, f, 4 * CountSegments));
  145.     our_denominator_trap = abs(trapezoid_formula(Array2, f, 2 * CountSegments) - trapezoid_formula(Array4, f, 4 * CountSegments));
  146.  
  147.     denominator_Gauss = (pow(2, p_Gauss) - 1);
  148.     denominator_Trap = (pow(2, p_trap) - 1);
  149.     R_trap = numerator_trap / denominator_Trap;
  150.     R_Gauss = numerator_Gauss / denominator_Gauss;
  151.  
  152.     our_accuracy_trap = log(abs(our_numerator_trap / our_denominator_trap)) / log(2);
  153.     our_accuracy_Gauss = log(abs(our_numerator_Gauss / our_denominator_Gauss)) / log(2);
  154.  
  155.     massiv_data[0] = R_trap;
  156.     massiv_data[1] = R_Gauss;
  157.     massiv_data[2] = our_accuracy_trap;
  158.     massiv_data[3] = our_accuracy_Gauss;
  159.  
  160. }
  161. int main()
  162. {
  163.     setlocale(LC_ALL, "RUS");
  164.     functiontype Func = &Myfunc;
  165.     Interval Interval; Interval.InitialNode = -1; Interval.EndNode = 2;
  166.     int CountSegments, СountDots = 500;
  167.     cout << "Введите число интервалов разбиения: " << endl; cin >> CountSegments; cout << endl;
  168.     int CountNodes = CountSegments + 1;
  169.  
  170.     Node* ArrayUniformNodes = new Node[CountNodes];
  171.     ValueUniformTable(&Func, ArrayUniformNodes, Interval.InitialNode, Interval.EndNode, CountNodes);
  172.  
  173.     cout << "Разбиение интервала на равные отрезки интегрирования:" << endl;
  174.     PrintNodes(ArrayUniformNodes, CountNodes);
  175.     cout << endl;
  176.  
  177.     cout << "Трапеция:" << endl;
  178.     double trapezoid_square = trapezoid_formula(ArrayUniformNodes, &Func, CountSegments);
  179.     cout << trapezoid_square << endl << endl;
  180.  
  181.     cout << "Гаусс:" << endl;
  182.     double Gauss_square = Gauss_formula(ArrayUniformNodes, &Func, CountSegments);
  183.     cout << Gauss_square << endl << endl;
  184.  
  185.     cout << "Интеграл:" << endl;
  186.     double orig_square = orig_integral(Interval.InitialNode, Interval.EndNode);
  187.     cout << orig_square << endl << endl;
  188.  
  189.     cout << "Разность между значениям интеграла и значением по формуле Гаусса:" << endl;
  190.     cout << abs(orig_square - Gauss_square) << endl << endl;
  191.  
  192.     cout << "Разность между значениям интеграла и значением по формуле Трапеций:" << endl;
  193.     cout << abs(orig_square - trapezoid_square) << endl << endl;
  194.  
  195.     /* -----------------Реализация правила Рунге для оценки погрешности-----------------------------*/
  196.     double* massiv_data = new double[4];
  197.     get_mas_pract_error(&Func, CountSegments, massiv_data, Interval.InitialNode, Interval.EndNode);
  198.  
  199.     cout << "Погрешность по Рунге для " << CountSegments << " частей по формуле трапеций:" << endl;
  200.     cout << massiv_data[0] << endl << endl;
  201.     cout << "Погрешность по Рунге для " << CountSegments << " частей по формуле Гаусса:" << endl;
  202.     cout << massiv_data[1] << endl << endl;
  203.     cout << "Точность по своему методу для " << CountSegments << " частей по формуле трапеций" << endl;
  204.     cout << massiv_data[2] << endl << endl;
  205.     cout << "Точность по своему методу для " << CountSegments << " частей по формуле Гаусса:" << endl;
  206.     cout << massiv_data[3] << endl << endl;
  207.     /* -----------------------Конец Рунге-----------------------------------*/
  208.  
  209.  
  210.     /* -----------------Экспериментальный порядок точности   -----------------------------*/
  211.     double* massiv_exp_accuracy = new double[2];
  212.     get_order_accuracy(CountSegments, &Func, Interval.InitialNode, Interval.EndNode, massiv_exp_accuracy);
  213.     cout << "Эксп. Порядок точности для Гаусса и Трапеций соответственно:" << endl;
  214.     cout << massiv_exp_accuracy[1] << " " << massiv_exp_accuracy[0] << endl << endl;
  215.  
  216.     //График
  217.     graphic_exp(&Func, Interval.InitialNode, Interval.EndNode, СountDots);
  218.     /* -------------------------   Конец --------------------------------------*/
  219.     graph_abs_error(ArrayUniformNodes, &Func, Interval.InitialNode, Interval.EndNode, СountDots);
  220.  
  221.     cout << endl;
  222.     system("pause");
  223.     return 0;
  224. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement