Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <math.h>
- using namespace std;
- const double lowestLimits[3] = {0, 0, -2};
- const double highestLimits[3] = {1.7, 2, -1.2};
- double (*integralFunctions[3])(double, double) = {
- [](double x, double step)
- { return 0.5 * step * (x * atan(x) + (x + step) * atan(x + step)); },
- [](double x, double step)
- { return 0.5 * step * (1 / (1 + sqrt(x)) + (1 / (1 + sqrt(x + step)))); },
- [](double x, double step)
- { return 0.5 * step * (2 / (1 - 4 * x) + (2 / (1 - 4 * (x + step)))); }
- };
- double integral(int n, int indexOfIntegral)
- {
- double step = abs(highestLimits[indexOfIntegral] - lowestLimits[indexOfIntegral]) / n;
- double result = 0, sum = 0, x = lowestLimits[indexOfIntegral];
- for (int i = 0; i < n; i++)
- {
- sum = (integralFunctions[indexOfIntegral](x, step));
- x += step;
- result += sum;
- }
- return result;
- }
- int main()
- {
- double acc;
- cout << "Input accuracy: ";
- cin >> acc;
- cout << endl;
- for (int i = 0; i < 3; i++)
- {
- int n = 1;
- double previousResult, currentResult = integral(n, i);
- do
- {
- ++n;
- previousResult = currentResult;
- currentResult = integral(n, i);
- } while (n < INT_MAX && !(abs(currentResult - previousResult) <= acc));
- cout << "### - " << i + 1 << endl;
- cout << "amount of trapezoids = " << n << endl;
- cout << "integral of n = " << previousResult << endl;
- cout << "integral of n + 1 = " << currentResult << endl;
- cout << "delta = " << abs(currentResult - previousResult) << endl
- << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement