upsidedown

trapezoidal method

Oct 14th, 2011
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.76 KB | None | 0 0
  1. #include<stdio.h>
  2.  
  3. float f(float x)
  4. {
  5.     return (1.0/(1+x*x));
  6. }
  7.  
  8.  
  9.  
  10. int main()
  11. {
  12.     int i,n;
  13.     float h,xn,x0,a,sum,temp;
  14.  
  15.  
  16.     printf("\nEnter the upper and lower limits");
  17.     scanf("%f %f",&xn,&x0);
  18.     printf("\nEnter the no of intervals");
  19.     scanf("%d",&n);
  20.  
  21.     printf("\n\na\t\tf(a)");
  22.    
  23.     h=(xn-x0)/n;
  24.     sum=f(x0)+f(xn);
  25.     for(i=1;i<=n-1;i++)
  26.     {
  27.         a=x0+i*h;
  28.         sum+=2*f(a);
  29.         printf("\n%f\t%f",a,f(a));
  30.     }
  31.  
  32.     temp=(h/2)*sum;
  33.  
  34.     printf("\nThe ans is %.6f\n",temp);
  35. }
  36.  
  37.  
  38.  
  39.  
  40.  
  41.        
  42.  
  43.  
  44.  
  45. OUTPUT:
  46. Enter the upper and lower limits1 0
  47.  
  48. Enter the no of intervals6
  49.  
  50.  
  51. a               f(a)
  52. 0.166667        0.972973
  53. 0.333333        0.900000
  54. 0.500000        0.800000
  55. 0.666667        0.692308
  56. 0.833333        0.590164
  57. The ans is 0.784241
  58. Press any key to continue
  59.  
Advertisement
Add Comment
Please, Sign In to add comment