/*To integrate a fn. by Simpson's 1/3 Rule*/
#include<stdio.h>
#include<math.h>
/*Define your function here*/
float f(float x)
{
float d;
d=x/1+x;
return (d);
}
int main ()
{
/*denoting a and b as integral lower and upper limits
respectively*/
float a, b, h, s;
int i, n;
printf("Input values of a, b and no. of steps:\n");
scanf("%f%f%d",&a,&b,&n);
h=(b-a)/n;
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)*s;
printf("\nValue of the integral =%f",s);
return (0);
}
#include<stdio.h>
#include<math.h>
/*Define your function here*/
float f(float x)
{
float d;
d=x/1+x;
return (d);
}
int main ()
{
/*denoting a and b as integral lower and upper limits
respectively*/
float a, b, h, s;
int i, n;
printf("Input values of a, b and no. of steps:\n");
scanf("%f%f%d",&a,&b,&n);
h=(b-a)/n;
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)*s;
printf("\nValue of the integral =%f",s);
return (0);
}
Comments
Post a Comment