Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.29 KB | None | 0 0
  1. #include<iostream>
  2. #include<iomanip>
  3. #include<cmath>
  4. using namespace std;
  5. void Trapezia(float arr[10], int a, int b, int h, int n);
  6. void Gauss(int b, int a, float h, int n);
  7. int main()
  8. {
  9.     printf("Lab 6\nNumerical integration of functions\nBershadskiy Andrew IS-81\n\n");
  10.    
  11.     //(cos(x))/x+1;
  12.     float h;
  13.     int a, b, n = 2;
  14.     cout << "(cos(x))/(x+1)" << endl << endl;
  15.     cout << "e = 0,0001" << endl << endl;
  16.    
  17.     a = 1; b = 3;
  18.     cout << "Boundaries of integration:" << endl;
  19.     cout << "a:" << a << endl << "b:" << b << endl << endl;
  20.    
  21.     double uravnenie = 1;
  22.     double iterations = 1;
  23.     while(0.0001 <= uravnenie){
  24.         uravnenie = (pow((b-a), 3))/(12*pow(iterations, 2))*0.3101;
  25.         iterations++;
  26.         //
  27.     }
  28.     cout << endl << "Iterations Trapezia:" <<  iterations << endl << endl;
  29.    
  30.    
  31.     double uravnenie1 = 1;
  32.     double iterations1 = 1;
  33.     while(0.0001 <= uravnenie1){
  34.         uravnenie1 = (8)/(69120)*(-0.13);
  35.         iterations1++;
  36.         //
  37.     }
  38.     cout << endl << "Iterations Gauss:" <<  iterations1-1 << endl << endl;
  39.    
  40.     h = (b - a) / n;
  41.     double h1 = (b - a) / iterations;
  42.     cout << "h = (b - a) / n" << endl << "h = " << h1 << endl << endl;
  43.    
  44.     float arr[10] = { 0 };
  45.     Trapezia(arr, a, b, h, n);
  46.     Gauss(b, a, h, n);
  47.     getchar();
  48.     return 0;
  49. }
  50. void Trapezia(float arr[10], int a, int b, int h, int n)
  51. {
  52.     float S = 0, S1 = 0, S2 = 0;
  53.     for (int i = 0; i <= n; i++)
  54.     {
  55.         arr[0] = a;
  56.         if (i != 0)
  57.         {
  58.             arr[i] = arr[i - 1] + h;
  59.         }
  60.     }
  61.     S1 = ((cos(a) / (a + 1)) + (cos(b) / (b + 1)));
  62.     for (int i = 1; i < n; i++)
  63.     {
  64.         S2 += cos(arr[i]) / (arr[i] + 1);
  65.     }
  66.     S = (S1 + 2 * S2) * 1 / 2;
  67.     cout << "Result with method of Trapezoid = " << S << endl;
  68. }
  69. void Gauss(int b, int a, float h, int n)
  70. {
  71.     float arr1[2] = { -0.5735,0.5735 };
  72.     float newarr1[2] = { 0 };
  73.     float arr2[2] = { 1,1 };
  74.     float rez = 0;
  75.     for (int i = 0; i < n; i++)
  76.     {
  77.         newarr1[i] = ((b + a) / 2) + ((b - a) / 2)*arr1[i];
  78.     }
  79.     for (int i = 0; i < n; i++)
  80.     {
  81.         rez += arr2[i] * cos(newarr1[i]) / (newarr1[i] + 1);
  82.     }
  83.     rez = rez*((b - a) / 2);
  84.     cout << "Result with Gauss = " << rez << endl;
  85.    
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement