Sunday 18 August 2019

Programming for integration by simpson's 1/3 rule


/*integration by simpson's 1/3 rule*/
#include<stdio.h>
#include<math.h>
float f(float x)
{
float d;
d=(x/(1+pow(x,5)));
return d;
}
void
main()
{
int i,n;
float a,b,h,s=0.0,c;
printf("enter the value of lower and upper limit:");
scanf("%f %f",&a,&b);
printf("enter the number of intervals:");
scanf("%d",&n);
label:
h=(b-a)/n;
c=s;
s=f(a)+f(b)+4*f(a+h);
for(i=3;i<=(n-1);i=i+2)
{
                s=s+(4*f(a+i*h))+(2*f(a+(i-1)*h));
}
s=(h/3.0)*s;
if(fabs (s-c)/s>0.001)
{
                n=n+10;
                goto label;
}
printf("the value of the interval is %.3f and is converged %d intervals\n",s,n);
}
RESULT:
enter the value of lower and upper limit:0 3
enter the number of intervals:10
the value of the interval is 0.648 and is converged 20 intervals
Press any key to continue

No comments:

Post a Comment

Thanks for comment.