Advertisement
Nur_Mohammed_Mehedy

Simpson's one third

Apr 12th, 2021
1,204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.82 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<conio.h>
  3. float f(float x)
  4. {
  5.     return(1/(1+x));
  6. }
  7. void main()
  8. {
  9.     int i,n;
  10.     float x0,xn,h,y[20],so,se,ans,x[20];
  11.  
  12.     printf("\n Enter values of x0,xn,h: ");
  13.  
  14.     scanf("%f%f%f",&x0,&xn,&h);
  15.  
  16.     n=(xn-x0)/h;
  17.  
  18.     if(n%2==1)
  19.     {
  20.         n=n+1;
  21.     }
  22.  
  23.     h=(xn-x0)/n;
  24.  
  25.     printf("\n Refined value of n and h are:%d %f\n",n,h);
  26.  
  27.     printf("\n Y values: \n");
  28.  
  29.     for(i=0; i<=n; i++)
  30.     {
  31.         x[i]=x0+i*h;
  32.         y[i]=f(x[i]);
  33.         printf("\n %f\n",y[i]);
  34.     }
  35.  
  36.     so=0;
  37.     se=0;
  38.  
  39.     for(i=1; i<n; i++)
  40.     {
  41.         if(i%2==1)
  42.         {
  43.             so=so+y[i];
  44.         }
  45.         else
  46.         {
  47.             se=se+y[i];
  48.         }
  49.  
  50.     }
  51.  
  52.     ans=h/3*(y[0]+y[n]+4*so+2*se);
  53.  
  54.     printf("\n Final integration is %f",ans);
  55.  
  56.     getch();
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement