Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <iostream>
- #include <math.h>
- using namespace std;
- double gauss_def(double a, double b, float y, float res, double integral);
- double simpson_def(double a, double b, float x, float y, float res, double integral, int n);
- double trapezoid(double a, double b, float x, float y, float res, double integral, int n);
- double three8(double a, double b, float x, float y, float res, double integral, int n);
- double def_N2(double a, double b, float x, float y, float res, double integral, int n);
- int main()
- {
- double a = 0.5, b = 2.5;
- int n = 0, menu = 0;
- double integral = 1.493270847223976;
- float y = 0, res = 0, x = a;
- cout << "Menu\n1.Gauss\n2.Simpson\n3.Trapezoid\n4.3/8\n5.2N\n6.Exit\nEnter: "; cin >> menu; cout << endl;
- if (menu == 1) gauss_def(a, b, y, res, integral);
- if (menu == 2) simpson_def(a, b, x, y, res, integral, n);
- if (menu == 3) trapezoid(a, b, x, y, res, integral, n);
- if (menu == 4) three8(a, b, x, y, res, integral, n);
- if (menu == 5) def_N2(a, b, x, y, res, integral, n);
- //system("pause");
- return 0;
- }
- double gauss_def(double a, double b, float y, float res, double integral)
- {
- double t[] = { -0.90617985 , -0.53846931, 0, 0.53846931, 0.90617985 };
- double A[] = { 0.23692688 , 0.47862868, 0.56888889, 0.47862868, 0.23692688 };
- cout << "| N = 5 |" << endl;
- cout << "Integration function: " << integral << endl;
- cout << "Interval from " << a << " to " << b << endl;
- double xi;
- for (int i = 0; i < 5; i++)
- {
- xi = ((b + a) / 2) + ((b - a) / 2) * t[i];
- //y = 1;
- y = sin((2 * xi) / (pow(xi, 2)));
- res += y * A[i];
- }
- res *= (b - a) / 2;
- double r = integral - res;
- cout << "Integrand: sin((2 * x) / (pow(x, 2)))" << endl;
- cout << "Gauss: " << res << " | " << "Error: " << r << endl;
- return res;
- }
- double simpson_def(double a, double b, float x, float y, float res, double integral, int n)
- {
- cout << "Enter the number of splits per line: "; cin >> n;
- while (n % 2 != 0)
- {
- cout << "#Error# ~Invalid number~ | Re-enter: "; cin >> n;
- }
- cout << "Interval from " << a << " to " << b << endl;
- float h = (b - a) / n;
- for (int i = 0; i <= n; i++)
- {
- x = a + h * i;
- //y = 1;
- y = sin((2 * x) / (pow(x, 2)));
- if (i % 2 == 0 && i != 0 && i != n) res += y * 2;
- else if (i % 2 != 0 && i != 0 && i != n) res += y * 4;
- else res += y;
- }
- res *= h / 3;
- double r = integral - res;
- cout << "Integrand: sin((2 * x) / (pow(x, 2)))" << endl;
- cout << "Simpson: " << res << " | " << "Error: " << r << endl;
- return res;
- }
- double trapezoid(double a, double b, float x, float y, float res, double integral, int n)
- {
- cout << "Enter the number of splits per line: "; cin >> n;
- cout << "Interval from " << a << " to " << b << endl;
- float h = (b - a) / n;
- for (int i = 0; i <= n; i++)
- {
- x = a + h * i;
- //y = 1;
- y = sin((2 * x) / (pow(x, 2)));
- if (i == 0 || i == n) res += y / 2;
- else res += y;
- }
- res *= h;
- double r = integral - res;
- cout << "Integrand: sin((2 * x) / (pow(x, 2)))" << endl;
- cout << "Trapezoid: " << res << " | " << "Error: " << r << endl;
- return res;
- }
- double three8(double a, double b, float x, float y, float res, double integral, int n)
- {
- cout << "Enter the number of splits per line: "; cin >> n;
- while (n % 3 != 0)
- {
- cout << "#Error# ~Invalid number~ | Re-enter: "; cin >> n;
- }
- cout << "Interval from " << a << " to " << b << endl;
- float h = (b - a) / n;
- for (int i = 0; i <= n; i++)
- {
- x = a + h * i;
- //y = 1;
- y = sin((2 * x) / (pow(x, 2)));
- if (i % 3 == 0 && i != 0 && i != n) res += y * 2;
- else if (i % 3 != 0 && i != 0 && i != n) res += y * 3;
- else res += y;
- }
- res *= (3 * h) / 8;
- double r = integral - res;
- cout << "Integrand: sin((2 * x) / (pow(x, 2)))" << endl;
- cout << "3/8: " << res << " | " << "Error: " << r << endl;
- return res;
- }
- double def_N2(double a, double b, float x, float y, float res, double integral, int n) //Доработать
- {
- int schot = 0;
- double res2 = 0, e = 0;
- cout << "Enter precision: "; cin >> e; cout << endl;
- cout << "Enter the number of splits per line: "; cin >> n;
- while (n % 2 != 0)
- {
- cout << "#Error# ~Invalid number~ | Re-enter: "; cin >> n;
- }
- cout << "Interval from " << a << " to " << b << endl;
- float h = (b - a) / n;
- do
- {
- for (int i = 0; i <= n; i++)
- {
- x = a + h * i;
- //y = 1;
- y = sin((2 * x) / (pow(x, 2)));
- if (i % 2 == 0 && i != 0 && i != n) res += y * 2;
- else if (i % 2 != 0 && i != 0 && i != n) res += y * 4;
- else res += y;
- }
- res *= h / 3;
- n *= 2;
- cout << "2N: " << n << endl;
- for (int i = 0; i <= n; i++)
- {
- x = a + h * i;
- //y = 1;
- y = sin((2 * x) / (pow(x, 2)));
- if (i % 2 == 0 && i != 0 && i != n) res2 += y * 2;
- else if (i % 2 != 0 && i != 0 && i != n) res2 += y * 4;
- else res2 += y;
- }
- res2 *= h / 3;
- schot += 1;
- } while (abs(res - res2) > e);
- cout << "~~~~~~~~" << schot << endl;
- double r = integral - res;
- cout << "Integrand: sin((2 * x) / (pow(x, 2)))" << endl;
- cout << "N: " << res << " | " << "2N: " << res2 << " | " << "Error: " << r << endl;
- return res;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement