vadimk772336

Untitled

Nov 28th, 2019
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.00 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. const double PI = 3.1415926535897932384626433832795;
  11. typedef double(*functiontype)(double x);
  12.  
  13. typedef struct Node {
  14.     double x, y;
  15. } Node;
  16.  
  17. typedef double(*method)(double x, Node* Array, int Count, double *DD_massiv);
  18.  
  19. typedef struct Interval {
  20.     double InitialNode, EndNode;
  21. } Interval;
  22.  
  23. int Factorial(int n)
  24. { // Факториал
  25.     int x = 1;
  26.     for (int i = 1; i <= n; i++) {
  27.         x *= i;
  28.     }
  29.     return x;
  30. }
  31.  
  32. void ValueUniformTable(functiontype* f, Node* Array, double Initial, double End, int CountNodes) // Точек на 1 больше чем отрезков, массив хранит все точки (их пары образуют отрезки)
  33. { // Создание равномерной таблицы значений
  34.     double step = abs(Initial - End) / (CountNodes-1);
  35.     Array[0].x = Initial;
  36.     Array[0].y = (*f)(Array[0].x);
  37.     for (int i = 1; i < CountNodes; i++) {
  38.         Array[i].x = Array[i - 1].x + step;
  39.         Array[i].y = (*f)(Array[i].x);
  40.        
  41.     }
  42. }
  43.  
  44. //Возвращает число, возведенное в cтепень i
  45. double get_degree(double x, int degree)
  46. {
  47.     int i;
  48.     double y = x;
  49.     if (degree == 0)
  50.         return 1;
  51.  
  52.     for (i = 0; i < degree - 1; i++)
  53.         y *= x;
  54.     return y;
  55. }
  56.  
  57. double Myfunc(double x)
  58. {
  59.     return (x * (sqrt(4 - x*x)));
  60. }
  61.  
  62. void orig_table_in_file(Node* Array, int Count)
  63. { // Функция берет таблицу иксов и игриков, количество точек в которых считаем знач полинома,
  64.     // По итогу имеем файл с 2 столбацами: х и у
  65.     int k;
  66.     double y_value, x_value;
  67.  
  68.     ofstream fout("D:/Original_graphic.txt");
  69.  
  70.     for (k = 0; k < Count; k++) { //n иксов подставляев в полином степени n-1
  71.         x_value = Array[k].x;
  72.         fout << x_value << " ";
  73.         y_value = Array[k].y;
  74.         fout << y_value << endl;
  75.     }
  76.  
  77.     fout.close();
  78. }
  79.  
  80. double trapezoid_formula(Node* Array, functiontype* f, int CountSegments) { //Теор. порядок точности = 2
  81.     int i;
  82.     double area = 0;
  83.  
  84.     for (i = 0; i < CountSegments; i++)
  85.         area += (Array[i+1].x - Array[i].x) * ((*f)(Array[i + 1].x) + (*f)(Array[i].x)) / 2;
  86.     return area;
  87. }
  88.  
  89. double Gause_formula(Node* Array, functiontype* f, int CountSegments) { //Теор. порядок точности =
  90.  
  91. /*порядок точности составной квадратурной формулы Гаусса по n узлам равен 2n. стр 216
  92. квадр формула Гаусс точно на полиномах степени 2n-1 стр 209
  93. 211 стр формула погр
  94. Алгебраическая степень точности квадратурной формулы, построенной по n узлам, не может превосходить 2n−1.
  95. Как следствие, если производные подинтегральной функции не рас-
  96. тут «слишком быстро» с ростом их порядка, то при увеличении числа
  97.  
  98. узлов и гладкости интегрируемой функции порядок точности квадра-
  99. турных формул Гаусса может быть сделан сколь угодно высоким.
  100. 202 стр теорема*/
  101.     int i;
  102.     double x1,x2,area = 0;
  103.     for (i = 0; i < CountSegments; i++) {
  104.         x1 = (Array[i+1].x + Array[i].x) / 2 + (Array[i+1].x - Array[i].x)*sqrt(3) / 6;
  105.         x2 = (Array[i+1].x + Array[i].x) / 2 - (Array[i+1].x - Array[i].x)*sqrt(3) / 6;
  106.         area += (Array[i + 1].x - Array[i].x)  * ( (*f)(x1) + (*f)(x2) ) / 2;
  107.     }
  108.    
  109.     return area;
  110. }
  111.  
  112. void PrintNodes(Node *Array, int CountSegments) {
  113.     int i;
  114.     for (i=0; i< CountSegments-1; i++)
  115.     cout << "(" << (Array[i].x) << ":" << (Array[i+1].x) << ")" << endl;
  116. }
  117.  
  118. double orig_integral(double Initial, double End) {
  119.     return (pow((4 - Initial * Initial), 1.5) - pow( (4 - End * End), 1.5 ) ) / 3 ;
  120. }
  121.  
  122. double get_C(functiontype* f, int CountSegments2, int CountSegments, Node* Array2, Node* Array, string method_name) {
  123.     int p;
  124.     double numerator, denominator;
  125.     functiontype Func = &Myfunc;
  126.     double step2 = 2 / (CountSegments2);
  127.  
  128.     if (method_name == "trap") {
  129.         p = 2;
  130.         numerator = (trapezoid_formula(Array2, &Func, CountSegments2) - trapezoid_formula(Array, &Func, CountSegments)) * pow(2, p);
  131.     }
  132.     else {
  133.         p = 4;
  134.         numerator = (Gause_formula(Array2, &Func, CountSegments2) - Gause_formula(Array, &Func, CountSegments)) * pow(2, p);
  135.     }
  136.    
  137.     denominator = pow(step2, p) * (pow(2, p) - 1);
  138.  
  139.     cout  << endl;
  140.     cout <<  "numerator: " << numerator <<   "denominator: " <<  denominator << endl;
  141.     cout << endl;
  142.  
  143.     return numerator / denominator;
  144.  
  145. }
  146.  
  147.  
  148. int main()
  149. {
  150.     setlocale(LC_ALL, "RUS");
  151.     Interval Interval;
  152.     int CountSegments, MyNodes = 5000;
  153.     double C;
  154.     functiontype Func = &Myfunc;
  155.     cout << "Enter the n: " << endl;
  156.     cin >> CountSegments;
  157.     double CountNodes = CountSegments + 1;
  158.     Interval.InitialNode = 0;
  159.     Interval.EndNode = 2;
  160.  
  161.     Node* ArrayUniformNodes = new Node[CountNodes];    
  162.     ValueUniformTable(&Func, ArrayUniformNodes, Interval.InitialNode, Interval.EndNode, CountNodes);
  163.  
  164.  
  165.     //Реализация правила Рунге
  166.     Node* Array2Nodes = new Node[int(CountNodes)];
  167.     Node* ArrayNodes = new Node[int(CountNodes/2)];
  168.     ValueUniformTable(&Func, Array2Nodes, Interval.InitialNode, Interval.EndNode, CountNodes);
  169.     ValueUniformTable(&Func, ArrayNodes, Interval.InitialNode, Interval.EndNode, int(CountNodes/2));
  170.     C = get_C(&Func, CountSegments, int(CountNodes / 2), Array2Nodes, ArrayNodes, "trap");
  171.     cout << "C = " << C << endl;
  172.  
  173.     /*cout << endl;
  174.     cout << "Разбиение интервала на равные отрезки интегрирования:" << endl;
  175.     PrintNodes(ArrayUniformNodes, CountNodes);
  176.     cout << endl;
  177.  
  178.     cout << "Значение интеграла через формулу трапеций:" << endl;
  179.     double trapezoid_square = trapezoid_formula(ArrayUniformNodes, &Func, CountSegments);
  180.     cout << trapezoid_square << endl << endl;
  181.  
  182.     cout << "Значение интеграла через формулу Гаусса по 2 узлам:" << endl;
  183.     double Gause_square = Gause_formula(ArrayUniformNodes, &Func, CountSegments);
  184.     cout << Gause_square << endl << endl;
  185.  
  186.     cout << "Значение интеграла:" << endl;
  187.     double orig_square = orig_integral(Interval.InitialNode, Interval.EndNode);
  188.     cout << orig_square << endl << endl;
  189.  
  190.     cout << "Разность между значениям интеграла и значением по формуле Гаусса:" << endl;
  191.     cout << abs(orig_square - Gause_square) << endl << endl;
  192.  
  193.     cout << "Разность между значениям интеграла и значением по формуле Трапеций:" << endl;
  194.     cout << abs(orig_square - trapezoid_square) << endl << endl;*/
  195.  
  196.  
  197.  
  198.  
  199.  
  200.     system("pause");
  201.     return 0;
  202. }
Advertisement
Add Comment
Please, Sign In to add comment