Advertisement
SeriousVenom

Integration

Dec 17th, 2020
708
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.13 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <math.h>
  4.  
  5. using namespace std;
  6.  
  7. double gauss_def(double a, double b, float y, float res, double integral);
  8. double simpson_def(double a, double b, float x, float y, float res, double integral, int n);
  9. double trapezoid(double a, double b, float x, float y, float res, double integral, int n);
  10. double three8(double a, double b, float x, float y, float res, double integral, int n);
  11. double def_N2(double a, double b, float x, float y, float res, double integral, int n);
  12.  
  13. int main()
  14. {
  15.     double a = 0.5, b = 2.5;
  16.     int n = 0, menu = 0;
  17.  
  18.     double integral = 1.493270847223976;
  19.  
  20.     float y = 0, res = 0, x = a;
  21.     cout << "Menu\n1.Gauss\n2.Simpson\n3.Trapezoid\n4.3/8\n5.2N\n6.Exit\nEnter: "; cin >> menu; cout << endl;
  22.  
  23.     if (menu == 1) gauss_def(a, b, y, res, integral);
  24.     if (menu == 2) simpson_def(a, b, x, y, res, integral, n);
  25.     if (menu == 3) trapezoid(a, b, x, y, res, integral, n);
  26.     if (menu == 4) three8(a, b, x, y, res, integral, n);
  27.     if (menu == 5) def_N2(a, b, x, y, res, integral, n);
  28.  
  29.     //system("pause");
  30.     return 0;
  31. }
  32.  
  33. double gauss_def(double a, double b, float y, float res, double integral)
  34. {
  35.     double t[] = { -0.90617985 , -0.53846931, 0, 0.53846931, 0.90617985 };
  36.     double A[] = { 0.23692688 , 0.47862868, 0.56888889, 0.47862868, 0.23692688 };
  37.  
  38.     cout << "| N = 5 |" << endl;
  39.     cout << "Integration function: " << integral << endl;
  40.     cout << "Interval from " << a << " to " << b << endl;
  41.  
  42.     double xi;
  43.     for (int i = 0; i < 5; i++)
  44.     {
  45.         xi = ((b + a) / 2) + ((b - a) / 2) * t[i];
  46.         //y = 1;
  47.         y = sin((2 * xi) / (pow(xi, 2)));
  48.  
  49.         res += y * A[i];
  50.     }
  51.     res *= (b - a) / 2;
  52.     double r = integral - res;
  53.     cout << "Integrand: sin((2 * x) / (pow(x, 2)))" << endl;
  54.     cout << "Gauss: " << res << " | " << "Error: " << r << endl;
  55.  
  56.     return res;
  57. }
  58.  
  59. double simpson_def(double a, double b, float x, float y, float res, double integral, int n)
  60. {
  61.     cout << "Enter the number of splits per line: "; cin >> n;
  62.  
  63.     while (n % 2 != 0)
  64.     {
  65.         cout << "#Error# ~Invalid number~ | Re-enter: "; cin >> n;
  66.     }
  67.  
  68.     cout << "Interval from " << a << " to " << b << endl;
  69.     float h = (b - a) / n;
  70.  
  71.     for (int i = 0; i <= n; i++)
  72.     {
  73.         x = a + h * i;
  74.         //y = 1;
  75.         y = sin((2 * x) / (pow(x, 2)));
  76.  
  77.         if (i % 2 == 0 && i != 0 && i != n) res += y * 2;
  78.         else if (i % 2 != 0 && i != 0 && i != n) res += y * 4;
  79.         else res += y;
  80.     }
  81.     res *= h / 3;
  82.     double r = integral - res;
  83.     cout << "Integrand: sin((2 * x) / (pow(x, 2)))" << endl;
  84.     cout << "Simpson: " << res << " | " << "Error: " << r << endl;
  85.     return res;
  86. }
  87.  
  88. double trapezoid(double a, double b, float x, float y, float res, double integral, int n)
  89. {
  90.     cout << "Enter the number of splits per line: "; cin >> n;
  91.  
  92.  
  93.     cout << "Interval from " << a << " to " << b << endl;
  94.     float h = (b - a) / n;
  95.  
  96.     for (int i = 0; i <= n; i++)
  97.     {
  98.         x = a + h * i;
  99.         //y = 1;
  100.         y = sin((2 * x) / (pow(x, 2)));
  101.         if (i == 0 || i == n) res += y / 2;
  102.         else res += y;
  103.     }
  104.     res *= h;
  105.     double r = integral - res;
  106.     cout << "Integrand: sin((2 * x) / (pow(x, 2)))" << endl;
  107.     cout << "Trapezoid: " << res << " | " << "Error: " << r << endl;
  108.     return res;
  109. }
  110.  
  111. double three8(double a, double b, float x, float y, float res, double integral, int n)
  112. {
  113.     cout << "Enter the number of splits per line: "; cin >> n;
  114.  
  115.     while (n % 3 != 0)
  116.     {
  117.         cout << "#Error# ~Invalid number~ | Re-enter: "; cin >> n;
  118.     }
  119.  
  120.     cout << "Interval from " << a << " to " << b << endl;
  121.     float h = (b - a) / n;
  122.  
  123.     for (int i = 0; i <= n; i++)
  124.     {
  125.         x = a + h * i;
  126.         //y = 1;
  127.         y = sin((2 * x) / (pow(x, 2)));
  128.  
  129.         if (i % 3 == 0 && i != 0 && i != n) res += y * 2;
  130.         else if (i % 3 != 0 && i != 0 && i != n) res += y * 3;
  131.         else res += y;
  132.     }
  133.     res *= (3 * h) / 8;
  134.     double r = integral - res;
  135.     cout << "Integrand: sin((2 * x) / (pow(x, 2)))" << endl;
  136.     cout << "3/8: " << res << " | " << "Error: " << r << endl;
  137.     return res;
  138. }
  139.  
  140. double def_N2(double a, double b, float x, float y, float res, double integral, int n) //Доработать
  141. {
  142.     int schot = 0;
  143.     double res2 = 0, e = 0;
  144.  
  145.     cout << "Enter precision: "; cin >> e; cout << endl;
  146.     cout << "Enter the number of splits per line: "; cin >> n;
  147.  
  148.     while (n % 2 != 0)
  149.     {
  150.         cout << "#Error# ~Invalid number~ | Re-enter: "; cin >> n;
  151.     }
  152.  
  153.     cout << "Interval from " << a << " to " << b << endl;
  154.     float h = (b - a) / n;
  155.  
  156.     do
  157.     {
  158.         for (int i = 0; i <= n; i++)
  159.         {
  160.             x = a + h * i;
  161.             //y = 1;
  162.             y = sin((2 * x) / (pow(x, 2)));
  163.             if (i % 2 == 0 && i != 0 && i != n) res += y * 2;
  164.             else if (i % 2 != 0 && i != 0 && i != n) res += y * 4;
  165.             else res += y;
  166.         }
  167.         res *= h / 3;
  168.         n *= 2;
  169.         cout << "2N: " << n << endl;
  170.  
  171.         for (int i = 0; i <= n; i++)
  172.         {
  173.             x = a + h * i;
  174.             //y = 1;
  175.             y = sin((2 * x) / (pow(x, 2)));
  176.             if (i % 2 == 0 && i != 0 && i != n) res2 += y * 2;
  177.             else if (i % 2 != 0 && i != 0 && i != n) res2 += y * 4;
  178.             else res2 += y;
  179.         }
  180.         res2 *= h / 3;
  181.         schot += 1;
  182.  
  183.     } while (abs(res - res2) > e);
  184.     cout << "~~~~~~~~" << schot << endl;
  185.  
  186.     double r = integral - res;
  187.     cout << "Integrand: sin((2 * x) / (pow(x, 2)))" << endl;
  188.     cout << "N: " << res << " | " << "2N: " << res2 << " | " << "Error: " << r << endl;
  189.     return res;
  190. }
  191.  
  192.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement