Advertisement
luizaspan

Integral III (método de Simpson)

Sep 30th, 2015
367
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.58 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=1;i<=N-1;i+=2)
  19.     {
  20.         double x1,x2,x3; // ou definir apenas x=a+h*i e em x1 -> x-h, x2 -> x+h
  21.         x1=a+h*(i-1);
  22.         x2=a+h*(i);
  23.         x3=a+h*(i+1);
  24.  
  25.         soma += (1./3)*h*(f(x1)+4*f(x2)+f(x3));
  26.     }
  27.  
  28.     double e = fabs((-log(a)-soma)/-log(a));
  29.  
  30.     printf("Valor calculado: %.15e \nValor real: %.15e \n",soma,-log(a));
  31.     printf("Erro: %e % \n",e*100);
  32.  
  33.     return 0;
  34. }
  35.  
  36.  
  37. // erro = 1/90 * h^4 * [f'''(a) - f'''(b)]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement