Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- using namespace std;
- double function(double x);
- void inputdata(int& a, int& b, int& n, double& eps);
- double middle(int a, int b, int n);
- double tropetion(int a, int b, int n);
- double Simpson(int a,int b,int n);
- double automatic_1(int a, int b, double eps);
- double automatic_2(int a, int b, double eps);
- int main()
- {
- int a, b, n;
- double eps = 0;
- setlocale(LC_ALL, "ru");
- cout << "Функция: 10) x^3-5x^2" << endl;
- inputdata(a, b, n, eps);
- cout << endl << endl;
- cout << "Результат средних: " << middle(a, b, n) << endl << "Результат трапеций: " << tropetion(a, b, n) << endl <<
- "Результат Симпсон: " << Simpson(a, b, n) << endl;
- cout << "Результат автомат_1: " << automatic_1(a, b, eps) << endl << "Результат автомат_2: " << automatic_2(a, b, eps);
- }
- double function(double x)
- {
- return x * x * x - 5 * x * x;
- }
- void inputdata(int& a, int& b, int& n, double& eps)
- {
- cout << "Начало интервала: ";
- cin >> a;
- do
- {
- cout << "Введите конец интервала: ";
- cin >> b;
- } while (b < a);
- do
- {
- cout << "Введите число разбиений: ";
- cin >> n;
- } while (n <= 0);
- do
- {
- cout << "Введите точность: ";
- cin >> eps;
- } while (eps <= 0);
- }
- double tropetion(int a, int b, int n)
- {
- double result = 0;
- double h = (double)(b - a) / n;
- for (double x = a; x <= b; x += h)
- {
- result += (function(x) + function(x+h)) / 2;
- }
- return result * h;
- }
- double Simpson(int a, int b, int n)
- {
- double result = 0;
- double h = (double)(b - a) / n;
- double x = a;
- while(x<=b)
- {
- result += function(x) + 4 * function(x + h / 2) + function(x + h);
- x += h;
- }
- return result * h / 6;
- }
- double automatic_1(int a, int b, double eps)
- {
- int n = 1;
- while (fabs(Simpson(a, b, n) - Simpson(a, b, n * 2)) >= eps)
- n *= 2;
- return Simpson(a, b, n * 2);
- }
- double automatic_2(int a, int b, double eps)
- {
- int n = 1;
- while (fabs(tropetion(a, b, n) - middle(a, b, n)) >= eps)
- n *= 2;
- return (tropetion(a, b, n) + 2 * middle(a, b, n)) / 3;
- }
- double middle(int a, int b, int n)
- {
- double result = 0;
- double h = (double)(b - a) / n;
- for (double x = a + h / 2; x <= b; x += h)
- {
- result += function(x);
- }
- return result * h;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement