Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <iomanip>
- using namespace std;
- // s = 3.059116539645953
- double F(double x)
- {
- return pow(M_E, x) / x; // функция
- }
- double riemann_sum(double a, double b, int n) //прямоугольник
- {
- double s = 0, h, x;
- h = (b - a) / n;
- for (int i = 0; i < n; i++)
- {
- x = a + i * h;
- s += F(x + h/2);
- }
- s *= h;
- //s += ((h * h) / 24) * 1.84726; // R1 считай сам
- return s;
- }
- double trapezoidal_rule(double a, double b, int n) //трапеция
- {
- double s = 0, x1, x2, h;
- h = (b - a) / n;
- for (int i = 0; i < n; i++)
- {
- x1 = a + i * h;
- x2 = x1 + h;
- s += F(x1) + F(x2);
- }
- s *= h / 2;
- //s -= ((h * h) / 12) * 1.84726; // R2 считай сам
- return s;
- }
- double simpsons_rule(double a, double b, int n) // парабола
- {
- double s = 0, x1, x2, x3, h;
- h = (b - a) / n;
- for (int i = 0; i < n / 2; i++)
- {
- x1 = a + 2 * i * h;
- x2 = x1 + h;
- x3 = x2 + h;
- s += F(x1) + 4 * F(x2) + F(x3);
- }
- s *= h / 3;
- //s -= ((h * h * h * h) / 180) * 6.36019; // R3 считай сам
- return s;
- }
- double runge_rule_difference(double (*method)(double, double, int), int n)
- {
- double I1, I2;
- I1 = (*method)(1, 2, n);
- I2 = (*method)(1, 2, n * 2);
- return fabs(I2 - I1);
- }
- int main() {
- double s_riemann, s_trapezoidal, s_simpson, e;
- e = 0.0000001;
- cout << "The program calculates the integration e ^ x / x on the spider [1;2]" << endl
- << "Input e " ;
- //cin >> e;
- cout << endl;
- int n = 1;
- while (e < runge_rule_difference(riemann_sum, n) / 3) { n *= 2; }
- s_riemann = riemann_sum(1, 2 , n * 2);
- cout << "Result for the rectangle method " << s_riemann << endl
- << "n = " << n * 2 << endl
- << "Runge rule "<< runge_rule_difference(riemann_sum, n) / 3 << endl << endl;
- n = 1;
- while (e < runge_rule_difference(trapezoidal_rule, n) / 3) { n *= 2; }
- s_trapezoidal = trapezoidal_rule(1, 2 , n * 2);
- cout << "Result for the trapezoid method " << s_trapezoidal << endl
- << "n = " << n * 2 << endl
- <<"Runge rule " << runge_rule_difference(trapezoidal_rule, n) / 3 << endl << endl;
- n = 1;
- while (e < runge_rule_difference(simpsons_rule, n) / 15) { n *= 2; }
- s_simpson = simpsons_rule(1, 2 , n * 2);
- cout << "Result for Simpson formula " << s_simpson << endl
- << "n = " << n * 2 << endl
- << "Runge rule " << runge_rule_difference(simpsons_rule, n) / 15 << endl << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement