Advertisement
luizaspan

Integral IV (método de Simpson - Variando N)

Sep 30th, 2015
372
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.51 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. double f(double x)
  5. {
  6.     return 1/x;
  7. }
  8.  
  9. int main(void)
  10. {
  11.     int i, N;
  12.  
  13.     for(N = 1; N < 1e7; N*=2)
  14.     {
  15.         double a=0.1, h=(1.0-a)/N, soma=0.0f; // Importante zerar soma!
  16.  
  17.         for (i=1;i<=N-1;i+=2)
  18.         {
  19.             double x1,x2,x3; // ou definir apenas x=a+h*i e em x1 -> x-h, x2 -> x+h
  20.             x1=a+h*(i-1);
  21.             x2=a+h*(i);
  22.             x3=a+h*(i+1);
  23.  
  24.             soma += (1./3)*h*(f(x1)+4*f(x2)+f(x3));
  25.         }
  26.  
  27.         double e = fabs((-log(a)-soma)/-log(a));
  28.  
  29.         printf("%d %e\n", N, e);
  30.     }
  31.  
  32.     return 0;
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement