Advertisement
Guest User

Untitled

a guest
May 21st, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.48 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 << "Enter a and b  (1 3)" << endl;
  15.     cin >> a >> b;
  16.     h = (b - a) / n;
  17.     float arr[10] = { 0 };
  18.     Trapezia(arr, a, b, h, n);
  19.     Gauss(b, a, h, n);
  20.     getchar();
  21.     return 0;
  22. }
  23. void Trapezia(float arr[10], int a, int b, int h, int n)
  24. {
  25.     float S = 0, S1 = 0, S2 = 0;
  26.     for (int i = 0; i <= n; i++)
  27.     {
  28.         arr[0] = a;
  29.         if (i != 0)
  30.         {
  31.             arr[i] = arr[i - 1] + h;
  32.         }
  33.     }
  34.     S1 = ((cos(a) / (a + 1)) + (cos(b) / (b + 1)));
  35.     for (int i = 1; i < n; i++)
  36.     {
  37.         S2 += cos(arr[i]) / (arr[i] + 1);
  38.     }
  39.     S = (S1 + 2 * S2) * 1 / 2;
  40.     cout << "Result wikth method of Trapezoid = " << S << endl;
  41. }
  42. void Gauss(int b, int a, float h, int n)
  43. {
  44.     float arr1[2] = { -0.5735,0.5735 };
  45.     float newarr1[2] = { 0 };
  46.     float arr2[2] = { 1,1 };
  47.     float rez = 0;
  48.     for (int i = 0; i < n; i++)
  49.     {
  50.         newarr1[i] = ((b + a) / 2) + ((b - a) / 2)*arr1[i];
  51.     }
  52.     for (int i = 0; i < n; i++)
  53.     {
  54.         rez += arr2[i] * cos(newarr1[i]) / (newarr1[i] + 1);
  55.     }
  56.     rez = rez*((b - a) / 2);
  57.     cout << "Result with Gauss = " << rez << endl;
  58.    
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement