Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include<stdio.h>
- #include<math.h>
- using namespace std;
- double f(double x) {
- return x * sqrt(1+x);
- }
- //решение методом трапеции
- double trapezoidal(double f(double x), double a, double b, int n) {
- double x, h, sum = 0, integral;
- int i;
- h = fabs(b - a) / n;
- for (i = 1; i < n; i++) {
- x = a + i * h;
- sum = sum + f(x);
- }
- integral = (h / 2)*(f(a) + f(b) + 2 * sum);
- return integral;
- }
- int main() {
- int n, i = 2;
- double a, b, h, eps, sum = 0, integral, integral_new;
- cout << "Enter the initial limit:\n";
- cin >> a;
- cout << "enter the final limit:\n";
- cin >> b;
- cout << "Enter the desired accuracy:\n";
- cin >> eps;
- integral_new = trapezoidal(f, a, b, i);
- do {
- integral = integral_new;
- i++;
- integral_new = trapezoidal(f, a, b, i);
- } while (fabs(integral_new - integral) >= eps);
- cout << "The integral is: " << integral_new << "\twith intervals: " << i <<"\n";
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement