Advertisement
tampurus

Unit 4.3 Simpson's 3/8 Integration

May 8th, 2022 (edited)
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.39 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3. #define f(x) 1/(1+x*x)
  4. int main()
  5. {
  6.     float b,a,h;
  7.     int n;
  8.     printf("Enter the values of upper limit,lower limit and h\n");
  9.     scanf("%f %f %f",&b,&a,&h);
  10.     n = (b-a)/h +1 ;
  11.     float x[n];
  12.     for(int i=0 ; i<n ; i++){
  13.         x[i] = a+ (i*h);
  14.     }
  15.     float div_3=0,nondiv_3=0;
  16.     for(int i=1 ; i<n-1 ; i++){
  17.         if(i%3==0)
  18.             div_3 += f(x[i]);
  19.         else
  20.             nondiv_3 += f(x[i]);
  21.     }
  22.     float ex_ord = f(x[0]) + f(x[n-1]); // extreme ordinates
  23.     float ans = (3*h)/8 *  ( ex_ord + (3*nondiv_3) + (2*div_3) ) ;
  24.     printf ("\nFinal answer is %f",ans);
  25.  
  26.     return 0;
  27. }
  28. /*
  29. Enter the values of upper limit,lower limit and h
  30. 1 0 0.166666
  31. Final answer is 0.785394
  32. */
  33.  
  34. // Algorithm
  35.  
  36. define function
  37. read a,b and h , where a lower , b upper limit and h size of interval
  38. n = (b-a)/h + 1
  39. declare float array x of size of n
  40.  
  41. start a loop from i=0 to n-1
  42.     x[i] = a + (i*h)
  43. end i loop
  44.    
  45. calulating the sum of ordinate divisible by 3 and not divisible by 3  
  46. declare div_3 = 0 and nondiv_3 = 0
  47. start loop from i=1 to n-2
  48.     check if i%3==0
  49.     if yes
  50.     then
  51.         div_3 = div_3 + f(x[i])
  52.     if no
  53.     else
  54.         nondiv_3 = nondiv_3 + f(x[i])
  55. end i loop
  56.  
  57. ex_ord = f(x[0]) + f(x[n-1]) , sum of extreme ordinates
  58.  
  59. ans = (3*h)/8 *  ( ex_ord + (3*nondiv_3) + (2*div_3) )
  60.  
  61. print ans
  62. stop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement