Tuesday 22 May 2012

C Program Of NA Using Simpson 3/8 Rule


/*Simpson 3/8 rule */
# include <stdio.h>
# include <conio.h>
# define MAX 20
void main()
{
int j,n=0;
float x[MAX],y[MAX],sum=0,h,res,lb,ub,i;
clrscr();
printf("Solution of 1/(1+x)dx using Simpson's 3/8 rule \n");
printf ("Enter initial value : ");
scanf("%f",&lb);
printf("Enter final value : ");
scanf("%f",&ub);
printf("Enter no. of Subinterval : ");
scanf("%f",&h);
h=(ub-lb)/h;
for(i=lb;i<=ub;i=i+h)
{
x[n]=i;
y[n]=1/(1+x[n]);
n++;
}
printf("No.\t X\t\tY=1/(1+x)\n");
printf("---------------------------------------\n");
sum=y[0]+y[n-1];
for(j=0;j<n;j++)
printf("%d\t%f\t%f\n",j,x[j],y[j]);
printf("---------------------------------------\n");
for(j=1;j<n-1;j++)
{
if(j%3==0)
sum=sum+y[j]*2;
else
sum=sum+y[j]*3;
}
res=((3*h)/8)*sum;
printf("\nIntegral from %f to %f when h = %f is = %f",x[0],x[n-1],h,res);
getch();
}

OUTPUT

Solution of 1/(1+x)dx using Simpson's 3/8 rule
Enter initial value : 6
Enter final value : 15
Enter no. of Subinterval : 8
No. X Y=1/(1+x)
---------------------------------------
0 6.000000 0.142857
1 7.125000 0.123077
2 8.250000 0.108108
3 9.375000 0.096386
4 10.500000 0.086957
5 11.625000 0.079208
6 12.750000 0.072727
7 13.875000 0.067227
8 15.000000 0.062500
---------------------------------------

Integral from 6.000000 to 15.000000 when h = 1.125000 is = 0.817303

No comments:

Post a Comment