# Numerical Integration (Simpsons 1/3 Rule) implementation in c programming.

## Simpson's 1/3 Rule

#### Simpson’s 1/3 rule is based on approximating the integrand by a second order polynomial .It is an extension of Trapezoidal rule where, the trapezoidal rule was based on approximating the integrand by a first order polynomial, and then integrating the polynomial over interval of integration.

It calculates the value of the area under any curve over a given interval by dividing the area into equal parts. It follows the method similar to integration by parts.  It is used to estimate the value of a definite integral.

In order to integrate any function f(x) in the interval (a,b), follow the steps given below:
Ø  Select a value for n, which is the number of parts the interval is divided into. Let the value of n be an even number.
Ø  Calculate the width, h = (b−a) / n.
Ø  Calculate the values of x0 to xn as x0 = a, x1 = x0+ h, x n-1 = xn-2 + h, xn = b.
Ø  Consider y = f(x). Now find the values of y (y0 to yn) for the corresponding x(x0 to xn) values.
Ø  Substitute all the above found values in the Simpson's Rule Formula to calculate the integral value.

The code for Simpson’s 1/3 rule implementing in C is given below:
#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];
printf("\n Enter values of x0,xn,h: ");
scanf("%f%f%f",&x0,&xn,&h);
n=(xn-x0)/h;
if(n%2==1)
{
n=n+1;
}
h=(xn-x0)/n;
printf("\n Refined value of n and h are:%d %f\n",n,h);
printf("\n Y values: \n");
for(i=0; i<=n; i++)
{
x[i]=x0+i*h;
y[i]=f(x[i]);
printf("\n %f\n",y[i]);
}
so=0;
se=0;
for(i=1; i<n; i++)
{
if(i%2==1)
{
so=so+y[i];
}
else
{
se=se+y[i];
}

}
ans=h/3*(y[0]+y[n]+4*so+2*se);
printf("\n Final integration is %f",ans);

getch();
}

Input & Output:

Application:
In numerical analysis, Simpson's rules are used in Ship Stability and naval architecture, to calculate the areas and volumes of irregular figures. This rule is also used in computer engineering .It provides the exact result for a quadratic function or parabola.

Numerical Integration (Simpsons 1/3 Rule).Simpson's 1/3 Rule implementation in c programming.