Advertisement
ANTAR_NANDI

Simpson 3/8 Rule

May 23rd, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.60 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. double f(double k){
  4. return 1/(1+k);
  5. }
  6. int main(){
  7. int n,i;
  8. double a,b,h,x,sum=0,integral=0;
  9. printf("\nEnter the no. of sub-intervals(MULTIPLE OF 3): ");
  10. scanf("%d",&n);
  11. printf("\nEnter the initial limit: ");
  12. scanf("%lf",&a);
  13. printf("\nEnter the final limit: ");
  14. scanf("%lf",&b);
  15. h=(b-a)/n;
  16. //cout<<h<<f(a)<<f(b);
  17. sum=f(a)+f(b);
  18. for(i=1;i<n;i++){
  19. x=a+i*h;
  20. if(i%3==0){
  21. sum=sum+2*f(x);
  22. }
  23. else{
  24. sum=sum+3*f(x);
  25. }
  26. }
  27. integral=sum*((3*h)/8);
  28. printf("\nThe integral is: %lf\n",integral);
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement