Advertisement
luizaspan

Integral V (método do trapézio extendido)

Oct 2nd, 2015
387
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.75 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. #define del 1e-5
  5.  
  6. double f(double x)
  7. {
  8.     return 1/x;
  9. }
  10.  
  11. int main(void)
  12. {
  13.     int N = 10;
  14.     double h, I, IOld, eps;
  15.  
  16.     // ------------------------- I_1 -------------------------
  17.  
  18.     int i;
  19.     double soma=0.0f,a=0.1;
  20.  
  21.     h=(1.0-a)/N;
  22.  
  23.     for (i=0;i<N;i++)
  24.     {
  25.         double x1 = a + h*i, x2= a + h*(i+1);
  26.         soma += (h/2)*(f(x1) + f(x2));
  27.     }
  28.  
  29.     I = soma;
  30.  
  31.     // ------------------------- END -------------------------
  32.  
  33.     do {
  34.         IOld = I;
  35.         N = 2*N;
  36.         h = h/2;
  37.  
  38.         double soma = 0;
  39.         for(i = 1; i < N; i += 2) {
  40.             soma += f(a + i*h);
  41.         }
  42.  
  43.         I = 0.5*IOld + h*soma;
  44.  
  45.         eps = 1.0/3 * fabs(I - IOld);
  46.  
  47.         printf("%d %e\n", N, eps);
  48.     } while (eps > del);
  49.  
  50.     return 0;
  51. }
  52.  
  53. // erro = 1/12 * h^2 * [f'(a) - f'(b)]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement