Advertisement
DarkDevourer

Практика - задание 2

Jun 28th, 2020
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include "graphics.h"
  3. #include <iostream>
  4. #include <math.h>
  5. #include <cstdlib>
  6. #include <string>
  7. #include <fstream>
  8.  
  9. using namespace std;
  10.  
  11. double func(double x); //Вычисляет значение функции f(x)=x*e^(x)*sin(x)
  12.  
  13. double f2(double x, double h); //Вторая производная
  14.  
  15. int main(int argc, char * argv[])
  16. {
  17. int n = 48;
  18. double a = 0, b = 1, h1, e = 0.001, S, f2max;
  19. double i;
  20. int w = 1900, h = 1000;
  21. int height = h - 100;
  22. int length = w - 50;
  23. setlocale(LC_ALL, "RUSSIAN");
  24. cout << "Нахождение определенного интеграла функции f(x)=x*e^(x)*sin(x) на отрезке [0;1]. Отрезок разбивается на 48 частей." << endl;
  25. while (1)
  26. {
  27. h1 = (b - a) / n;
  28. S = h1 * (func(a) + func(b)) / 2;
  29. for (i = 0; i < n; i++)
  30. {
  31. S = S + func(a + h1 * i)*h1;
  32. }
  33. f2max = f2(a, h1);
  34. for (i = 0; i < n; i++)
  35. {
  36. if (f2(a + h1 * i, h1) > f2max) f2max = f2(a + h1 * i, h1);
  37. }
  38. double check = f2max * pow(b - a, 3) / (12 * n*n);
  39. if (check > e)
  40. {
  41. n = n + 5;
  42. }
  43. else
  44. {
  45. cout << "Значение интеграла = " << S << endl;
  46. break;
  47. }
  48.  
  49. }
  50. cout << "Нажмите Enter, чтобы вывести диаграмму." << endl;
  51. system("pause");
  52. int mastx = length / n;
  53. double maxfunc = func(a);
  54. for (i = 1; i < n; i++)
  55. {
  56. if (func(a + i * h1) > maxfunc)
  57. maxfunc = func(a + i * h1);
  58. }
  59. if (func(b) > maxfunc)
  60. maxfunc = func(b);
  61. int masty = height / maxfunc;
  62. char conv[128];
  63. initwindow(w, h, "Diagrama");
  64. bar(0, 0, w, h);
  65. rectangle(0, 0, w, h);
  66. setcolor(0);
  67. setbkcolor(15);
  68.  
  69. line(30, 30, 30, 30 + height);
  70. line(30, 30, 25, 40);
  71. line(30, 30, 35, 40);
  72. line(30, 30 + height, 20 + length, 30 + height);
  73. line(20 + length, 30 + height, length + 15, 25 + height);
  74. line(20 + length, 30 + height, length + 15, 35 + height);
  75. outtextxy(length + 20, 45 + height, "x");
  76. outtextxy(40, 30, "f(x)");
  77. for (i = 0; i < 1; i+=0.2)
  78. {
  79. line(30 + i * length, 20 + height, 30 + i * length, 40 + height);
  80. sprintf(conv, "%f", i);
  81. outtextxy(30 + i * length, 45 + height, conv);
  82. double k = 30 + height - func(i) * masty;
  83. line(20, 30 + height - func(i) * masty, 40, 30 + height - func(i) * masty);
  84. sprintf(conv, "%f", func(i));
  85. outtextxy(5, 30 + height - func(i) * masty, conv);
  86.  
  87. }
  88. for (i = 0; i < n; i++)
  89. {
  90. line(30 + i * mastx, 30 + height, 30 + i * mastx, 30 + height - func(a+i*h1) * masty);
  91. line(30 + i * mastx, 30 + height - func(a + i * h1) * masty, 30 + (i + 1) * mastx, 30 + height - func(a + i * h1) * masty);
  92. line(30 + (i + 1) * mastx, 30 + height, 30 + (i + 1) * mastx, 30 + height - func(a + i * h1) * masty);
  93. }
  94. getch();
  95. while (!kbhit())
  96. {
  97. delay(200);
  98. }
  99. getch();
  100. closegraph();
  101.  
  102. cout << "Нажмите Enter, чтобы вывести график функции." << endl;
  103. system("pause");
  104. initwindow(w, h, "Diagrama");
  105. bar(0, 0, w, h);
  106. rectangle(0, 0, w, h);
  107. setcolor(0);
  108. setbkcolor(15);
  109.  
  110. line(30, 30, 30, 30 + height);
  111. line(30, 30, 25, 40);
  112. line(30, 30, 35, 40);
  113. line(30, 30 + height, 20 + length, 30 + height);
  114. line(20 + length, 30 + height, length + 15, 25 + height);
  115. line(20 + length, 30 + height, length + 15, 35 + height);
  116. outtextxy(length + 20, 45 + height, "x");
  117. outtextxy(40, 30, "f(x)");
  118. for (i = 0; i < 1; i += 0.2)
  119. {
  120. line(30 + i * length, 20 + height, 30 + i * length, 40 + height);
  121. sprintf(conv, "%f", i);
  122. outtextxy(30 + i * length, 45 + height, conv);
  123. double k = 30 + height - func(i) * masty;
  124. line(20, 30 + height - func(i) * masty, 40, 30 + height - func(i) * masty);
  125. sprintf(conv, "%f", func(i));
  126. outtextxy(5, 30 + height - func(i) * masty, conv);
  127.  
  128. }
  129. for (i = 0; i < n; i++)
  130. {
  131. line(30 + i * mastx, 30 + height - func(a + i * h1) * masty, 30 + (i + 1) * mastx, 30 + height - func(a + (i+1) * h1) * masty);
  132. }
  133. getch();
  134. while (!kbhit())
  135. {
  136. delay(200);
  137. }
  138. getch();
  139. closegraph();
  140. system("pause");
  141. return 0;
  142. }
  143. double func(double x) //Вычисляет значение функции f(x)=x*e^(x)*sin(x)
  144. {
  145. return x * exp(x)*sin(x);
  146. }
  147.  
  148. double f2(double x, double h) //Вторая производная
  149. {
  150. return (func(x + h) - 2 * func(x) + func(x - h)) / (h * h);
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement