vadimk772336

Untitled

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