Advertisement
labyyysosaaat

Untitled

Oct 21st, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.98 KB | None | 0 0
  1. #include <iostream>
  2. #include<stdio.h>
  3. #include<math.h>
  4. using namespace std;
  5. double f(double x) {
  6. return x * sqrt(1+x);
  7. }
  8. //решение методом трапеции
  9. double trapezoidal(double f(double x), double a, double b, int n) {
  10. double x, h, sum = 0, integral;
  11. int i;
  12. h = fabs(b - a) / n;
  13. for (i = 1; i < n; i++) {
  14. x = a + i * h;
  15. sum = sum + f(x);
  16. }
  17. integral = (h / 2)*(f(a) + f(b) + 2 * sum);
  18. return integral;
  19. }
  20.  
  21. int main() {
  22. int n, i = 2;
  23. double a, b, h, eps, sum = 0, integral, integral_new;
  24.  
  25.  
  26. cout << "Enter the initial limit:\n";
  27. cin >> a;
  28. cout << "enter the final limit:\n";
  29. cin >> b;
  30. cout << "Enter the desired accuracy:\n";
  31. cin >> eps;
  32. integral_new = trapezoidal(f, a, b, i);
  33.  
  34. do {
  35. integral = integral_new;
  36. i++;
  37. integral_new = trapezoidal(f, a, b, i);
  38. } while (fabs(integral_new - integral) >= eps);
  39.  
  40. cout << "The integral is: " << integral_new << "\twith intervals: " << i <<"\n";
  41. system("pause");
  42. return 0;
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement