Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <math.h>
- #define e 1e-9
- double f(double x) {
- return sqrt(x);
- }
- int main(int argc, char *argv[]) {
- double Ix;
- int t;
- double a, b, h;
- printf("Интеграл (1 или 2) = "); scanf("%d", &t);
- if (t == 1) {
- Ix = 2.0 / 3.0;
- a = 0;
- b = 1;
- }
- else if (t == 2) {
- Ix = (2.0 / 3.0) * (pow(2.0, 3.0 / 2.0) - 1);
- a = 1;
- b = 2;
- }
- putchar('\n');
- h = (b - a) / 2;
- int n = 1;
- double s1, s2, s3;
- s1 = f(a) + f(b);
- s3 = 0;
- double I, I_old;
- I_old = 0;
- I = s1 * h;
- do {
- double Rh = 0;
- Rh = (I_old - I) * 16 / 15;
- I_old = I;
- s2 = 0;
- for (int i = 1; i <= n; i++)
- s2 += f(a + h * (1 + 2 * (i - 1)));
- I = h * (s1 + 4 * s2 + 2 * s3) / 3;
- if (fabs(I - I_old) <= e)
- break;
- s3 += s2;
- n *= 2;
- h /= 2;
- double Rh_2 = 0;
- Rh_2 = (I_old - I) * 16 / 15;
- printf("R(h) / 16 = %.10lf; R(h/2) = %.10lf\n", Rh / 16.0, Rh_2);
- } while (1);
- printf("\nТочное значение интеграла = %.16e\nВычисленное значение интеграла = %.16e\nЧисло подынтервалов = %d\n", Ix, I, n);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment