Advertisement
luizaspan

[PROVA FSC_COMP] Integração Numérica (Retangular)

Nov 3rd, 2015
364
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.68 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. #define N 1000 // número de "retângulos"
  5.  
  6. double f(double x)
  7. {
  8.     return 1/x; // função a ser integrada
  9. }
  10.  
  11. int main(void)
  12. {
  13.     int i;
  14.     double h,soma=0.0f; // importante zerar soma!!
  15.  
  16.     h=(1.0-0.1)/N; // vendo pelos limites de integração
  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)); // erro da integração - comparando com o valor real da função integrada
  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