Last active
January 5, 2022 21:08
-
-
Save Yegorsh/cc36fee932d7dcf9eeea45860d16dc99 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| #include <cmath> | |
| using namespace std; | |
| double Function(double x){ | |
| return sin(x); | |
| } | |
| double Trapezoid_1 (double a, double b, double N){ //методом трапеций 1 | |
| double Integral = 0, delta_x = (b - a) / N; | |
| double SubSum = 0; | |
| for (int i=1; i<N; i++){ | |
| SubSum += Function(a + i*delta_x); | |
| } | |
| return Integral = delta_x * (SubSum + (Function(b) + Function(a))/2 ); | |
| } | |
| double Trapezoid_2 (double a, double b, double N){ //методом трапеций 2 | |
| double Integral = 0, delta_x = (b - a) / N; | |
| for (int i=0; i<N; i++){ | |
| Integral += delta_x * ((Function(a + i*delta_x)+ Function(a+ (i+1)*delta_x))/2); | |
| } | |
| return Integral; | |
| } | |
| double LeftRiemann (double a, double b, double N){ //Left Riemann sum | |
| double Integral = 0, delta_x = (b - a) / N; | |
| for (int i=0; i<N; i++){ | |
| Integral += delta_x * Function(a + i * delta_x); | |
| } | |
| return Integral; | |
| } | |
| double RightRiemann (double a, double b, double N){ //Right Riemann sum | |
| double Integral = 0, delta_x = (b - a) / N; | |
| for (int i=1; i<=N; i++){ | |
| Integral += delta_x * Function(a + i * delta_x); | |
| } | |
| return Integral; | |
| } | |
| double Midpoint (double a, double b, double N){ //Midpoint method | |
| double Integral = 0, delta_x = (b - a) / N; | |
| for (int i=1; i<N; i++){ | |
| Integral += delta_x * Function(a + (2*i-1) * (delta_x)/2); | |
| } | |
| return Integral; | |
| } | |
| double Simpson (double a, double b, double N){ //Simpson's method | |
| double Integral = 0, delta_x = (b - a) / N; | |
| double SubSum_1 = 0, SubSum_2 = 0; | |
| for (int i=1; i<=(N/2 - 1); i++){ | |
| SubSum_1 += Function(a + 2*i*delta_x); | |
| } | |
| for (int i=1; i<=(N/2); i++){ | |
| SubSum_2 += Function(a + (2*i - 1)*delta_x); | |
| } | |
| return Integral = (delta_x/3) * (Function(a) + 2*SubSum_1 + 4*SubSum_2 + Function(b)); | |
| } | |
| int main(){ | |
| double a, b; | |
| int N, t; | |
| cout << "Правый конец = " << endl; | |
| cin >> a; | |
| cout << "Левый конец = " << endl; | |
| cin >> b; | |
| cout << "Количество разбиений N" << endl; | |
| cin >> N; | |
| cout << "Посчитать интеграл методом " << endl << "1. Трапеций 1" << endl << "2. Левая риманова сумма" << endl << "3. Правая риманова сумма" << endl << "4. Средняя точка" << endl << "5. Simpson's method" << endl << "6. Трапеций 2" << endl; | |
| cin >> t; | |
| cout << endl; | |
| switch (t){ | |
| case 1:{ | |
| cout << "Integral of f(x) from a to b = " << Trapezoid_1(a, b, N); | |
| break; | |
| } | |
| case 2:{ | |
| cout << "Integral of f(x) from a to b = " << LeftRiemann(a, b, N); | |
| break; | |
| } | |
| case 3:{ | |
| cout << "Integral of f(x) from a to b = " << RightRiemann(a, b, N); | |
| break; | |
| } | |
| case 4:{ | |
| cout << "Integral of f(x) from a to b = " << Midpoint(a, b, N); | |
| break; | |
| } | |
| case 5:{ | |
| again: | |
| if (N%2 == 0) { | |
| cout << "Integral of f(x) from a to b = " << Simpson(a, b, N); | |
| } | |
| else{ | |
| cout << "N должно быть четным!" << endl; | |
| cout << "Количество разбиений N" << endl; | |
| cin >> N; | |
| goto again; | |
| } | |
| break; | |
| } | |
| case 6:{ | |
| cout << "Integral of f(x) from a to b = " << Trapezoid_2(a, b, N); | |
| break; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment