Advertisement
labyyysosaaat

Untitled

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