Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <vector>
- #include <cstdio>
- #include <cmath>
- double f(double x) {
- return (cos(x) - x * sin(x)) / pow(cos(x), 2);
- }
- double F(double x) {
- return -x / cos(x) - 2 * log(cos(x * 0.5) - sin(x * 0.5)) + 2 * log(sin(x * 0.5) + cos(x * 0.5));
- }
- double myintegral(double a, double b, int n) {
- double sigma1 = 0;
- double sigma2 = 0;
- double h = (b - a) / (double) n;
- for (int i = 1; i < n; i++) {
- if (i % 2 == 0) sigma2 += f(a + i * h);
- else sigma1 += f(a + i * h);
- }
- return h / 3.0 * (f(a) + f(b) + 4.0 * sigma1 + 2.0 * sigma2);
- }
- int main() {
- std::vector<double> errors;
- double a = -1.5;
- double b = 1.5;
- double I = F(b) - F(a);
- double q = 180.0 / ((b - a) * 20000000.0);
- printf("\nsimpson\n| given error | step | value | received error |\n");
- for (double eps = 1e-2; eps >= 1e-12; eps *= 1e-2) {
- int n = (int((b - a) / pow(eps * q, 0.25)) >> 1) << 1;
- double value = myintegral(a, b, n);
- errors.push_back(fabs(I - value));
- printf("| %.5e | %.10e | %13.13f | %.8e |\n", eps, (b - a) / (double) n, value, errors.back());
- }
- printf("\nrunge\n| given error | step | received error |\n");
- for (const double &error : errors) {
- int n = (int((b - a) / pow(error, 0.25)) >> 1) << 1;
- double R = 0;
- double I2n = myintegral(a, b, n);
- do {
- double In = I2n;
- I2n = myintegral(a, b, n <<= 1);
- R = fabs(In - I2n) / 15.0;
- } while (R > error);
- printf("| %.5e | %.10e | %.8e |\n", error, (b - a) / (double)n, R);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement