Advertisement
Guest User

Untitled

a guest
Dec 14th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.72 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <cmath>
  4.  
  5.  
  6.  
  7. double f1(double x)
  8. {
  9. return x;
  10. }
  11. double f2(double x)
  12. {
  13. return (sin(22 * x));
  14. }
  15. double f3(double x)
  16. {
  17. return (x * x * x * x);
  18.  
  19. }
  20. double f4(double x)
  21. {
  22. return (atan(x));
  23. }
  24.  
  25. double zn1(double a, double b)
  26. {
  27. return ((b * b - a * a) / 2.0);
  28. }
  29.  
  30. double zn2(double a, double b)
  31. {
  32. return((cos(a * 22.0) - cos(b * 22.0)) / 22.0);
  33. }
  34. double zn3(double a, double b)
  35. {
  36. return ((b * b * b * b * b - a * a * a * a * a) / 5.0);
  37. }
  38. double zn4(double a, double b)
  39. {
  40. return (b * atan(b) - a * atan(a) - (log(b * b + 1) - log(a * a + 1)) / 2.0);
  41. }
  42.  
  43.  
  44. typedef double (*TPF)(double);
  45.  
  46. double IntWorkRect(TPF f, double a, double b, double dx) {
  47. double s = 0;
  48. for (double x = a; x < b; x += dx) {
  49. s += f(x + dx / 2);
  50. }
  51. return s * dx;
  52. }
  53.  
  54. double IntRect(TPF f, double a, double b, double eps, int& n) {
  55. double dx = (b - a) / 2;
  56. double s1 = IntWorkRect(f, a, b, dx);
  57. double s2 = IntWorkRect(f, a, b, dx / 2);
  58. while (abs(s1 - s2) > eps) {
  59. dx /= 2;
  60. s1 = s2;
  61. s2 = IntWorkRect(f, a, b, dx / 2);
  62. n *= 2;
  63. }
  64. return s2;
  65. }
  66.  
  67.  
  68. double IntWorkTrap(TPF f, double a, double b, double dx) {
  69. double s = 0;
  70. for (double x = a; x < b; x += dx) {
  71. s += (f(x) + f(x + dx)) / 2;
  72. }
  73. s *= dx;
  74. return s ;
  75. }
  76.  
  77. double IntTrap(TPF f, double a, double b, double eps, int& n) {
  78. double dx = (b - a) / 2;
  79. double s1 = IntWorkTrap(f, a, b, dx);
  80. double s2 = IntWorkTrap(f, a, b, dx / 2);
  81. while (abs(s1 - s2) > eps) {
  82. dx /= 2;
  83. s1 = s2;
  84. s2 = IntWorkTrap(f, a, b, dx / 2);
  85. n *= 2;
  86. }
  87. return s2;
  88. }
  89. #include <iostream>
  90. #include <iomanip>
  91. #include <cmath>
  92. #include "прототипы.h"
  93. using namespace std;
  94.  
  95. struct I_print { //данные для печати результатов интегрирования
  96. const char* name;//название функции
  97. double sum; //значение интегральной суммы
  98. double zn; //точное значение интеграла
  99. int n; //число разбиений области интегрирования
  100. //при котором достигнута требуемая точность
  101. };
  102.  
  103. void print(I_print h[])
  104. {
  105. const int m = 4;//число столбцов таблицы
  106. int wn[m] = { 20,20,20,20 };//ширина столбцов таблицы
  107. const char* title[m] = {"Function","Integral","IntSum","N "};
  108. int size[m];
  109. for (int i = 0; i < m; i++)
  110. size[i] = strlen(title[i]);
  111. //шапка таблицы
  112. cout << char(218) << setfill(char(196));
  113. for (int j = 0; j < m - 1; j++)
  114. cout << setw(wn[j]) << char(194);
  115. cout << setw(wn[m - 1]) << char(191) << endl;
  116.  
  117. cout << char(179);
  118. for (int j = 0; j < m; j++)
  119. cout << setw((wn[j] - size[j]) / 2) << setfill(' ') << ' ' << title[j]
  120. << setw((wn[j] - size[j]) / 2) << char(179);
  121. cout << endl;
  122. for (int i = 0; i < 4; i++)
  123. {//заполнение таблицы
  124. cout << char(195) << fixed;
  125. for (int j = 0; j < m - 1; j++)
  126. cout << setfill(char(196)) << setw(wn[j]) << char(197);
  127. cout << setw(wn[m - 1]) << char(180) << setfill(' ') << endl;
  128.  
  129. cout << char(179) << setw((wn[0] - strlen(h[i].name)) / 2) << ' ' << h[i].name
  130. << setw((wn[0] - strlen(h[i].name)) / 2) << char(179);
  131. cout << setw(wn[1] - 1) << setprecision(10) << h[i].zn << char(179)
  132. << setw(wn[2] - 1) << h[i].sum << setprecision(6) << char(179)
  133. << setw(wn[3] - 1) << h[i].n << char(179) << endl;
  134. }
  135. //низ таблицы
  136. cout << char(192) << setfill(char(196));
  137. for (int j = 0; j < m - 1; j++)
  138. cout << setw(wn[j]) << char(193);
  139. cout << setw(wn[m - 1]) << char(217) << setfill(' ') << endl;
  140. }
  141.  
  142.  
  143. int main()
  144. {
  145.  
  146. double a = -1;
  147. double b = 3;
  148. cout << "from " << a << " to " << b << endl;
  149. int n = 8;
  150. double eps = 0.01;
  151. double (*F[])(double) = { f1, f2, f3, f4 };
  152.  
  153. I_print m[4];
  154.  
  155. m[0].zn = zn1(a, b);
  156. m[1].zn = zn2(a, b);
  157. m[2].zn = zn3(a, b);
  158. m[3].zn = zn4(a, b);
  159.  
  160. m[0].name = "y = x ";
  161. m[1].name = "y = sin(22x)";
  162. m[2].name = "y = x^4 ";
  163. m[3].name = "y = arctan(x) ";
  164. cout << "Rectangle" << endl;
  165. while (eps > 0.000001)
  166. {
  167. cout << eps << endl;
  168. for (int i = 0; i < 4; i++)
  169. {
  170. m[i].sum = IntRect(F[i], a, b, eps, n);
  171. m[i].n = n;
  172. n = 1;
  173. }
  174. print(m);
  175. eps /= 10;
  176. }
  177. I_print h[4];
  178.  
  179. h[0].zn = zn1(a, b);
  180. h[1].zn = zn2(a, b);
  181. h[2].zn = zn3(a, b);
  182. h[3].zn = zn4(a, b);
  183.  
  184. h[0].name = "y = x ";
  185. h[1].name = "y = sin(22x)";
  186. h[2].name = "y = x^4 ";
  187. h[3].name = "y = arctan(x) ";
  188. eps = 0.01;
  189. cout << "Trapeze" << endl;
  190. while (eps > 0.000001)
  191. {
  192. cout << eps << endl;
  193. for (int i = 0; i < 4; i++)
  194. {
  195. h[i].sum = IntTrap(F[i], a, b, eps, n);
  196. h[i].n = n;
  197. n = 1;
  198. }
  199. print(h);
  200. eps /= 10;
  201. }
  202.  
  203.  
  204. return 0;
  205. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement