Advertisement
luizaspan

Integral II (método do trapézio)

Sep 30th, 2015
321
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.48 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. #define N 1000
  5.  
  6. double f(double x)
  7. {
  8.     return 1/x;
  9. }
  10.  
  11. int main(void)
  12. {
  13.     int i;
  14.     double h,soma=0.0f,a=0.1;
  15.  
  16.     h=(1.0-a)/N;
  17.  
  18.     for (i=0;i<N;i++)
  19.     {
  20.         double x1 = a + h*i, x2= a + h*(i+1);
  21.         soma += (h/2)*(f(x1) + f(x2));
  22.     }
  23.  
  24.     double e = fabs((-log(a)-soma)/-log(a));
  25.  
  26.     printf("Valor calculado: %.15e \nValor real: %.15e \n",soma,-log(a));
  27.     printf("Erro: %e % \n",e*100);
  28.  
  29.     return 0;
  30. }
  31.  
  32. // erro = 1/12 * h^2 * [f'(a) - f'(b)]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement