upsidedown

trapezoidal method

Oct 14th, 2011
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.74 KB | None | 0 0
  1. #include<stdio.h>
  2. double f(double x)
  3. {
  4.     return (1/(1+(x*x)));
  5. }
  6. void main()
  7. {
  8.     double x0,xn,sum,a,temp,h;
  9.     int n,i;
  10.     printf("enter the limits of the integral x0 and xn\n");
  11.     scanf("%lf %lf",&x0,&xn);
  12.     printf("\nenter the number of intervals\n");
  13.     scanf("%d",&n);
  14.     h=(xn-x0)/n;
  15.     sum=f(x0)+f(xn);
  16.     for(i=1;i<=n-1;i++)
  17.     {
  18.         a=x0+(i*h);
  19.         sum=sum+(2*f(a));
  20.         printf("%.6lf \t %.6lf\n",a,f(a));
  21.     }
  22.     temp=(h/2)*sum;
  23.     printf("the answer is %.6lf\n",temp);
  24. }
  25.  
  26. OUTPUT:
  27. enter the limits of the integral x0 and xn
  28. 0
  29. 1
  30.  
  31. enter the number of intervals
  32. 6
  33. 0.166667         0.972973
  34. 0.333333         0.900000
  35. 0.500000         0.800000
  36. 0.666667         0.692308
  37. 0.833333         0.590164
  38. the answer is 0.784241
  39. Press any key to continue
  40.  
Advertisement
Add Comment
Please, Sign In to add comment