Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <fstream>
- using namespace std;
- double functionUnderIntegral(double x) {
- return (2 * (x*x*x) + cos(x));
- }
- double rightRectangleMethod(double a, double b, double n, double h) {
- double x;
- double sum = 0;
- for (int i = 1; i <= n; i++) {
- x = a + i*h;
- sum += h*functionUnderIntegral(x);
- }
- return sum;
- }
- double leftRectangleMethod(double a, double b, double n, double h) {
- double x;
- double sum = 0;
- for (int i = 0; i <= (n - 1); i++) {
- x = a + i*h;
- sum += h*functionUnderIntegral(x);
- }
- return sum;
- }
- double centralRectangleMethod(double a, double b, double n, double h) {
- double x;
- double sum = 0;
- for (int i = 0; i <= (n - 1); i++) {
- x = a + i*h;
- sum += h*functionUnderIntegral(x + (h / 2));
- }
- return sum;
- }
- double trapeziumMethod(double a, double b, double n, double h) {
- double x;
- double sum1 = 0;
- double sum2 = 0;
- for (int i = 1; i <= (n - 1); i++) {
- x = a + i*h;
- sum1 += functionUnderIntegral(x);
- }
- sum2 = (h / 2)*(functionUnderIntegral(a) + functionUnderIntegral(b)) + h*sum1;
- return sum2;
- }
- double simpsonMethod(double a, double b, double n, double h) {
- double x;
- double sum1 = 0;
- double sum2 = 0;
- double sum3 = 0;
- for (int i = 1; i <= (n - 1); i++) {
- if ((i % 2) != 0) {
- x = a + i*h;
- sum1 += functionUnderIntegral(x);
- }
- }
- for (int i = 2; i <= (n - 2); i++) {
- if ((i % 2) == 0) {
- x = a + i*h;
- sum2 += functionUnderIntegral(x);
- }
- }
- sum3 = (h / 3)*(functionUnderIntegral(a) + functionUnderIntegral(b) + 4 * sum1 + 2 * sum2);
- return sum3;
- }
- double analyticalMethod() {
- return 2 * pow(3.14,3.0) - 1 ;
- }
- int main()
- {
- double a = 0;
- double b = (3.14 / 2);
- double n;
- double h;
- cout << "n := "; cin >> n;
- cout << endl;
- h = (b - a) / n;
- cout << "Right Rectangle Method := " << rightRectangleMethod(a, b, n, h) << endl; cout << endl;
- cout << "Central Rectangle Method := " << centralRectangleMethod(a, b, n, h) << endl; cout << endl;
- cout << "Left Rectangle Method := " << leftRectangleMethod(a, b, n, h) << endl; cout << endl;
- cout << "Trapezium Method := " << trapeziumMethod(a, b, n, h) << endl; cout << endl;
- cout << "Simpson Method := " << simpsonMethod(a, b, n, h) << endl; cout << endl;
- cout << "Analytical Method := " << analyticalMethod() << endl; cout << endl;
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement