SHARE
TWEET

Simpson 3/8 Rule

ANTAR_NANDI May 23rd, 2019 64 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top