Advertisement
Arnab_Manna

trapizoidal rule.c

Jan 19th, 2021
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.83 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<conio.h>
  4. //float *coll;
  5. //coll=(float*) malloc(n+1 * sizeof(float));
  6. //float del = (b-a)/n;
  7.      
  8. float f(float x)
  9. {
  10.     return ((3*x*x)+(2*x)-5);
  11. }
  12.  
  13. int main()
  14. {
  15.     float a=0.0,b=2.0,n=6.0,sum=0,result;
  16.     float coll[7],fn[7];
  17.     int i,j,k,g;
  18.     float del = (b-a)/n;
  19.    
  20.     for(i=0;i<=n;i++)
  21.     {
  22.         if(i==0)
  23.         { coll[i]=a;}
  24.         else if(i==n)
  25.         { coll[i]=b; }
  26.         else
  27.         { coll[i]= coll[i-1]+del;}
  28.     }
  29.     for(j=0;j<=n;j++)
  30.     {
  31.         if(j==0 || j==n)
  32.         { fn[j] = f(coll[j]);}
  33.         else
  34.         { fn[j] = 2*f(coll[j]);}
  35.     }
  36.     printf("intervals are :\n");
  37.     for(g=0;g<=n;g++)
  38.     {
  39.         printf("%f,",coll[g]);
  40.     }
  41.     printf("\n");
  42.     for(k=0;k<=n;k++)
  43.     {
  44.                 sum = sum+fn[k];
  45.                 printf("\nf(%d) = %f",k,fn[k]);
  46.     }
  47.     result = (del/2.0) * sum;  
  48.     printf("\ntrapizoidal rule result ==>%f",result);
  49.     return 0;
  50.     }  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement