Advertisement
luizaspan

Integral I (método retangular)

Sep 30th, 2015
362
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;
  15.  
  16.     h=(1.0-0.1)/N;
  17.  
  18.     for (i=0;i<N;i++)
  19.     {
  20.         double x = 0.1 + h*i;
  21.         soma += h*f(x);
  22.     }
  23.  
  24.     double e = fabs((-log(0.1)-soma)/-log(0.1));
  25.  
  26.     printf("Valor calculado: %.15e \nValor real: %.15e \n",soma,-log(0.1));
  27.     printf("Erro: %e % \n",e*100);
  28.  
  29.     return 0;
  30. }
  31.  
  32. // I(a,b) = h*sum_i ( f(x_i) )
  33. // erro = h/2 * [f(b)-f(a)]
  34. // erro ~ 1/N
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement