Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<iomanip>
- #include<cmath>
- using namespace std;
- void Trapezia(float arr[10], int a, int b, int h, int n);
- void Gauss(int b, int a, float h, int n);
- int main()
- {
- printf("Lab 6\nNumerical integration of functions\nBershadskiy Andrew IS-81\n\n");
- //(cos(x))/x+1;
- float h;
- int a, b, n = 2;
- cout << "Enter a and b (1 3)" << endl;
- cin >> a >> b;
- h = (b - a) / n;
- float arr[10] = { 0 };
- Trapezia(arr, a, b, h, n);
- Gauss(b, a, h, n);
- getchar();
- return 0;
- }
- void Trapezia(float arr[10], int a, int b, int h, int n)
- {
- float S = 0, S1 = 0, S2 = 0;
- for (int i = 0; i <= n; i++)
- {
- arr[0] = a;
- if (i != 0)
- {
- arr[i] = arr[i - 1] + h;
- }
- }
- S1 = ((cos(a) / (a + 1)) + (cos(b) / (b + 1)));
- for (int i = 1; i < n; i++)
- {
- S2 += cos(arr[i]) / (arr[i] + 1);
- }
- S = (S1 + 2 * S2) * 1 / 2;
- cout << "Result wikth method of Trapezoid = " << S << endl;
- }
- void Gauss(int b, int a, float h, int n)
- {
- float arr1[2] = { -0.5735,0.5735 };
- float newarr1[2] = { 0 };
- float arr2[2] = { 1,1 };
- float rez = 0;
- for (int i = 0; i < n; i++)
- {
- newarr1[i] = ((b + a) / 2) + ((b - a) / 2)*arr1[i];
- }
- for (int i = 0; i < n; i++)
- {
- rez += arr2[i] * cos(newarr1[i]) / (newarr1[i] + 1);
- }
- rez = rez*((b - a) / 2);
- cout << "Result with Gauss = " << rez << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement