Tweet
// Simpson's 1/3rd rule
#include<stdio.h>
float f(float x)
{
return 1/(1+x);
}
main()
{
int i,n;
float a,b,h,s,x,y[20],s1=0,s2=0;
printf("\nProgram to implement simpson's 1/3rd rule : \n");
printf("\nEnter the lower limit : ");
scanf("%f",&a);
printf("\nEnter the upper limit : ");
scanf("%f",&b);
printf("\nEnter the no. of sub-intervals : ");
scanf("%d",&n);
h=(b-a)/n;
x=a;
for(i=0;i<=n;i++)
{
y[i]=f(x);
x=x+h;
}
for(i=1;i<n;i+=2)
s1=s1+4*y[i];
for(i=2;i<=n-2;i+=2)
s2=s2+2*y[i];
s=(h/3)*(y[0]+y[n]+s1+s2);
printf("\nThe result is : %6.4f\n\n",s);
}
Simpson's 1/3rd Rule in C
Posted by
LAHAUL SETH
~
Simpson's 1/3rd Rule in C
2011-12-23T08:48:00+05:30
LAHAUL SETH
Numerical Methods
|
Programming in C
|
Comments
Simpson's 1/3rd Rule in C
2011-12-23T08:48:00+05:30
LAHAUL SETH
Numerical Methods
|
Programming in C
|