Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <math.h>
- double f(double x)
- {
- return 1/x;
- }
- int main(void)
- {
- int i, N;
- for(N = 1; N < 1e7; N*=2)
- {
- double a=0.1, h=(1.0-a)/N, soma=0.0f; // Importante zerar soma!
- for (i=1;i<=N-1;i+=2)
- {
- double x1,x2,x3; // ou definir apenas x=a+h*i e em x1 -> x-h, x2 -> x+h
- x1=a+h*(i-1);
- x2=a+h*(i);
- x3=a+h*(i+1);
- soma += (1./3)*h*(f(x1)+4*f(x2)+f(x3));
- }
- double e = fabs((-log(a)-soma)/-log(a));
- printf("%d %e\n", N, e);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement