Tuesday, 22 May 2012

C Program Of NA Using Simpson 1/3 Rule


/*Simpson 1/3 Rule */
# include <stdio.h>
# include <conio.h>
# define MAX 20
void main()
{
int i,j,n;
float x[MAX],y[MAX],sum=0,h,res;
clrscr();
printf("Solution of 1/(1+x)dx using Simpson's 1/3 rule \n");
printf("Enter Number of data points : ");
scanf("%d",&n);
printf("Enter value of table value set by set\n");
for(i=1;i<=n;i++)
{
scanf("%f",&x[i]);
y[i]=1/(x[i]+1);
}
printf("X \t\t Y\n");
for(i=1;i<=n;i++)
printf("%f\t%f\n",x[i],y[i]);
h=x[2]-x[1];
printf("\nSegment Interval `h':=0.25\n");
sum=sum+y[1]+y[n];
for(i=2;i<n;i++)
{
if(i%2==0)
sum=sum+y[i]*4;
else
sum=sum+y[i]*2;
}
res=sum*(0.25/3);
printf ("Integral from %3.2f to %3.2f is : = %f",x[1],x[n],res);
getch();
}
OUTPUT
Solution of 1/(1+x)dx using Simpson's 1/3 rule
Enter Number of data points : 5
Enter value of table value for x column
0 .25 .50 .75 1
X Y
0.000000 1.000000
0.250000 0.800000
0.500000 0.666667
0.750000 0.571429
1.000000 0.500000

Segment Interval `h':=0.25
Integral from 0.00 to 1.00 is : = 0.693254

No comments:

Post a Comment

Post a Comment