Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- #include <cmath>
- double f1(double x)
- {
- return x;
- }
- double f2(double x)
- {
- return (sin(22 * x));
- }
- double f3(double x)
- {
- return (x * x * x * x);
- }
- double f4(double x)
- {
- return (atan(x));
- }
- double zn1(double a, double b)
- {
- return ((b * b - a * a) / 2.0);
- }
- double zn2(double a, double b)
- {
- return((cos(a * 22.0) - cos(b * 22.0)) / 22.0);
- }
- double zn3(double a, double b)
- {
- return ((b * b * b * b * b - a * a * a * a * a) / 5.0);
- }
- double zn4(double a, double b)
- {
- return (b * atan(b) - a * atan(a) - (log(b * b + 1) - log(a * a + 1)) / 2.0);
- }
- typedef double (*TPF)(double);
- double IntWorkRect(TPF f, double a, double b, double dx) {
- double s = 0;
- for (double x = a; x < b; x += dx) {
- s += f(x + dx / 2);
- }
- return s * dx;
- }
- double IntRect(TPF f, double a, double b, double eps, int& n) {
- double dx = (b - a) / 2;
- double s1 = IntWorkRect(f, a, b, dx);
- double s2 = IntWorkRect(f, a, b, dx / 2);
- while (abs(s1 - s2) > eps) {
- dx /= 2;
- s1 = s2;
- s2 = IntWorkRect(f, a, b, dx / 2);
- n *= 2;
- }
- return s2;
- }
- double IntWorkTrap(TPF f, double a, double b, double dx) {
- double s = 0;
- for (double x = a; x < b; x += dx) {
- s += (f(x) + f(x + dx)) / 2;
- }
- s *= dx;
- return s ;
- }
- double IntTrap(TPF f, double a, double b, double eps, int& n) {
- double dx = (b - a) / 2;
- double s1 = IntWorkTrap(f, a, b, dx);
- double s2 = IntWorkTrap(f, a, b, dx / 2);
- while (abs(s1 - s2) > eps) {
- dx /= 2;
- s1 = s2;
- s2 = IntWorkTrap(f, a, b, dx / 2);
- n *= 2;
- }
- return s2;
- }
- #include <iostream>
- #include <iomanip>
- #include <cmath>
- #include "прототипы.h"
- using namespace std;
- struct I_print { //данные для печати результатов интегрирования
- const char* name;//название функции
- double sum; //значение интегральной суммы
- double zn; //точное значение интеграла
- int n; //число разбиений области интегрирования
- //при котором достигнута требуемая точность
- };
- void print(I_print h[])
- {
- const int m = 4;//число столбцов таблицы
- int wn[m] = { 20,20,20,20 };//ширина столбцов таблицы
- const char* title[m] = {"Function","Integral","IntSum","N "};
- int size[m];
- for (int i = 0; i < m; i++)
- size[i] = strlen(title[i]);
- //шапка таблицы
- cout << char(218) << setfill(char(196));
- for (int j = 0; j < m - 1; j++)
- cout << setw(wn[j]) << char(194);
- cout << setw(wn[m - 1]) << char(191) << endl;
- cout << char(179);
- for (int j = 0; j < m; j++)
- cout << setw((wn[j] - size[j]) / 2) << setfill(' ') << ' ' << title[j]
- << setw((wn[j] - size[j]) / 2) << char(179);
- cout << endl;
- for (int i = 0; i < 4; i++)
- {//заполнение таблицы
- cout << char(195) << fixed;
- for (int j = 0; j < m - 1; j++)
- cout << setfill(char(196)) << setw(wn[j]) << char(197);
- cout << setw(wn[m - 1]) << char(180) << setfill(' ') << endl;
- cout << char(179) << setw((wn[0] - strlen(h[i].name)) / 2) << ' ' << h[i].name
- << setw((wn[0] - strlen(h[i].name)) / 2) << char(179);
- cout << setw(wn[1] - 1) << setprecision(10) << h[i].zn << char(179)
- << setw(wn[2] - 1) << h[i].sum << setprecision(6) << char(179)
- << setw(wn[3] - 1) << h[i].n << char(179) << endl;
- }
- //низ таблицы
- cout << char(192) << setfill(char(196));
- for (int j = 0; j < m - 1; j++)
- cout << setw(wn[j]) << char(193);
- cout << setw(wn[m - 1]) << char(217) << setfill(' ') << endl;
- }
- int main()
- {
- double a = -1;
- double b = 3;
- cout << "from " << a << " to " << b << endl;
- int n = 8;
- double eps = 0.01;
- double (*F[])(double) = { f1, f2, f3, f4 };
- I_print m[4];
- m[0].zn = zn1(a, b);
- m[1].zn = zn2(a, b);
- m[2].zn = zn3(a, b);
- m[3].zn = zn4(a, b);
- m[0].name = "y = x ";
- m[1].name = "y = sin(22x)";
- m[2].name = "y = x^4 ";
- m[3].name = "y = arctan(x) ";
- cout << "Rectangle" << endl;
- while (eps > 0.000001)
- {
- cout << eps << endl;
- for (int i = 0; i < 4; i++)
- {
- m[i].sum = IntRect(F[i], a, b, eps, n);
- m[i].n = n;
- n = 1;
- }
- print(m);
- eps /= 10;
- }
- I_print h[4];
- h[0].zn = zn1(a, b);
- h[1].zn = zn2(a, b);
- h[2].zn = zn3(a, b);
- h[3].zn = zn4(a, b);
- h[0].name = "y = x ";
- h[1].name = "y = sin(22x)";
- h[2].name = "y = x^4 ";
- h[3].name = "y = arctan(x) ";
- eps = 0.01;
- cout << "Trapeze" << endl;
- while (eps > 0.000001)
- {
- cout << eps << endl;
- for (int i = 0; i < 4; i++)
- {
- h[i].sum = IntTrap(F[i], a, b, eps, n);
- h[i].n = n;
- n = 1;
- }
- print(h);
- eps /= 10;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement