freesky

task_sem2_5.1

Nov 3rd, 2012
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.35 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. #define e 1e-9
  5.  
  6. double f(double x) {
  7.     return sqrt(x);
  8. }
  9.  
  10. int main(int argc, char *argv[]) {
  11.     double Ix;
  12.     int t;
  13.     double a, b, h;
  14.  
  15.     printf("Интеграл (1 или 2) = "); scanf("%d", &t);
  16.     if (t == 1) {
  17.         Ix = 2.0 / 3.0;
  18.         a = 0;
  19.         b = 1;
  20.     }
  21.     else if (t == 2) {
  22.         Ix = (2.0 / 3.0) * (pow(2.0, 3.0 / 2.0) - 1);
  23.         a = 1;
  24.         b = 2;
  25.     }
  26.  
  27.     putchar('\n');
  28.  
  29.     h = (b - a) / 2;
  30.  
  31.     int n = 1;
  32.     double s1, s2, s3;
  33.  
  34.     s1 = f(a) + f(b);
  35.     s3 = 0;
  36.  
  37.     double I, I_old;
  38.     I_old = 0;
  39.  
  40.     I = s1 * h;
  41.  
  42.     do {
  43.         double Rh = 0;
  44.         Rh = (I_old - I) * 16 / 15;
  45.  
  46.         I_old = I;
  47.         s2 = 0;
  48.  
  49.         for (int i = 1; i <= n; i++)
  50.             s2 += f(a + h * (1 + 2 * (i - 1)));
  51.  
  52.         I = h * (s1 + 4 * s2 + 2 * s3) / 3;
  53.  
  54.         if (fabs(I - I_old) <= e)
  55.             break;
  56.  
  57.         s3 += s2;
  58.         n *= 2;
  59.         h /= 2;
  60.  
  61.         double Rh_2 = 0;
  62.         Rh_2 = (I_old - I) * 16 / 15;
  63.  
  64.         printf("R(h) / 16 = %.10lf; R(h/2) = %.10lf\n", Rh / 16.0, Rh_2);
  65.     } while (1);
  66.  
  67.     printf("\nТочное значение интеграла = %.16e\nВычисленное значение интеграла = %.16e\nЧисло подынтервалов = %d\n", Ix, I, n);
  68.  
  69.     return 0;
  70. }
Advertisement
Add Comment
Please, Sign In to add comment