C Program to implement Simpson's 1/3 rd Rule








Sumit Kar, Numerical Methods, C, Timus Rak




/*


Simpson's 1/3 Rule


Equation: x/(1+x) dx
lower limit=0 upper limit=1 number of interval=6
*/
#include<conio.h>
#include<stdio.h>
void main()
{
float a,b,h,x,y,so=0;
float x0,y0,yn,xn,r,se=0;;
int i,n;
float f(float);
clrscr();
printf("\nEnter lower limit value:: ");
scanf("%f",&a);
printf("\nEnter upper limit value:: ");
scanf("%f",&b);
printf("\nEnter the number of intervals:: ");
scanf("%d",&n);
h=(b-a)/n;
x0=a;
y0=f(x0);
yn=f(b);
x=x0+h;
for(i=1;i<=n-1;i=i+2)
{
y=f(x);
so=so+y;
x=x+2*h;
}
x=x0+2*h;
for(i=2;i<=n-2;i=i+2)
{
y=f(x);
se=se+y;
x=x+2*h;
}
r=(h/3)*(y0+yn+4*so+2*se);
printf("\nResult is:: %f",r);
getch();
}
float f(float x)
{
return x/(1+x);
}



0 Comments