Exp 8 Simpsonrule
Exp 8 Simpsonrule
Exp 8 Simpsonrule
For this, let’s discuss the C program for Simpson 1/3 rule for easy and accurate
calculation of numerical integration of any function which is defined in program.
As the program gets executed, first of all it asks for the value of lower boundary
value of x i.e. x0, upper boundary value of x i.e. xn and width of the strip, h.
Then the program finds the value of number of strip as n=( x n – x0 )/h and checks
whether it is even or odd. If the value of ‘n’ is odd, the program refines the value
of ‘h’ so that the value of ‘n’ comes to be even.
After that, this C program calculates value of f(x) i.e ‘y’ at different intermediate
values of ‘x’ and displays values of all intermediate values of ‘y’.
After the calculation of values of ‘c’, the program uses the following formula to
calculate the value of integral in loop.
Integral = *((y0 + yn ) +4(y1 + y3 + ……….+ yn-1 ) + 2(y2 + y4 +……….+ yn-2 ))
Finally, it prints the values of integral which is stored as ‘ans’ in the program.
If f(x) represents the length, the value of integral will be area, and if f(x) is area, the output
of Simpson 1/3 rule C program will be volume. Hence, numerical integration can be
carried out using the program below; it is very easy to use, simple to understand, and
gives reliable and accurate results.
f(x) = 1/(1+x)
#include<stdio.h>
#include<conio.h>
float f(float x)
{
return(1/(1+x));
void main()
int i,n;
float x0,xn,h,y[20],so,se,ans,x[20];
scanf("%f%f%f",&x0,&xn,&h);
n=(xn-x0)/h;
if(n%2==1)
n=n+1;
h=(xn-x0)/n;
x[i]=x0+i*h;
y[i]=f(x[i]);
printf("\n %f\n",y[i]);
}
so=0;
se=0;
if(i%2==1)
so=so+y[i];
else
se=se+y[i];
ans=h/3*(y[0]+y[n]+4*so+2*se);
getch();