Composite Simpson 1/3 Rule

Algorithm : Composite Simpson’s (1/3)rd rule Composite_simpson’s(a,b,n) { //Let us consider a function f(x). We want to find the integrating value of f(x) w.r.t. x from lower limit ‘a’ to upper limit ‘b’ with subdivision n. h=(b-a)/n x=a sum=0 for(i=1 to n-1) { if(i%2==0) { x=a+i*h; sum=sum+(2*h/3)*f(x); //f(x) is a function } which returns the else value of f(x) at x=x { x=a+i*h; sum=sum+(4*h/3)*f(x); } } r=h/3*[f(a)+f(b)]+sum print “r” } C Program For Composite Simpson 1/3 Rule Source Code: #include>stdio.h> #include>conio.h> #include>math.h> void simpson(float,float,int); float f(float); void main() { int n; float a,b; clrscr(); printf("\nCALCULATE :\n"); printf("\n%c\n%cx^3.dx using the given limits.\n",244,245); printf("\nEnter the upper limit of the function:"); scanf("%f",&b); printf("\nEnter the lower limit of the function:"); scanf("%f",&a); printf("\nENTER THE NUMBE3R OF SUB-DIVISIONS(must be even):"); scanf("%d",&n); simpson(a,b,n); getch(); } void simpson(float a,float b,int n) { float h,sum1=0,x,R,sum2=0; int i; h=(b-a)/n; x=a; for(i=1;i<=n-1;i++) { x=a+i*h; if(i%2==0) { sum1=sum1+f(x); } if(i%2!=0) { sum2=sum2+f(x); } } R=(h/3)*(f(a)+f(b))+4*(h/3)*sum2+2*(h/3)*sum1; printf("\n\nValue=%f",R); } float f(float x) { return (x*x*x); }

Share:

0 comments