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 << "(cos(x))/(x+1)" << endl << endl;
- cout << "e = 0,0001" << endl << endl;
- a = 1; b = 3;
- cout << "Boundaries of integration:" << endl;
- cout << "a:" << a << endl << "b:" << b << endl << endl;
- double uravnenie = 1;
- double iterations = 1;
- while(0.0001 <= uravnenie){
- uravnenie = (pow((b-a), 3))/(12*pow(iterations, 2))*0.3101;
- iterations++;
- //
- }
- cout << endl << "Iterations Trapezia:" << iterations << endl << endl;
- double uravnenie1 = 1;
- double iterations1 = 1;
- while(0.0001 <= uravnenie1){
- uravnenie1 = (8)/(69120)*(-0.13);
- iterations1++;
- //
- }
- cout << endl << "Iterations Gauss:" << iterations1-1 << endl << endl;
- h = (b - a) / n;
- double h1 = (b - a) / iterations;
- cout << "h = (b - a) / n" << endl << "h = " << h1 << endl << endl;
- 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 with 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