Numerical Integration
(Simpson's Rule)
Objective : - To write a C++ Program for Numerical Integration using Simpson’s Rule
Theory : -
Simpson’s rule is a numerical method that approximates the value of a definite integral by using quadratic polynomials.
Let’s first derive a formula for the area under a parabola of equation y = ax2 + bx + c passing through the three points: (−h, y0), (0, y1), (h, y2).
Since the points (−h, y0), (0, y1), (h, y2) are on the parabola, they satisfy y = ax2 + bx + c.
Therefore,
Algorithm : -
Simpson’s 1/3 Rule
Step-1: Start of the program.
Step-2: Input Lower limit a
Step-3: Input Upper limit b
Step-4: Input number of subintervals n
Step-5: h=(b–a)/n
Step-6: sum=0
Step-7: sum=fun(a)+4*fun(a+h)+fun(b)
Step-8: for i=3; i<n; i + = 2
Step-9: sum + = 2*fun(a+(i – 1)*h) + 4*fun(a+i*h)
Step-10: End of loop i
Step-11: result=sum*h/3
Step-12: Print Output result
Step-13: End of Program
Step-14: Start of Section fun
Step-15: temp = 1/(1+(x*x))
Step-16: Return temp
Step-17: End of Section fun
Simpson’s 3/8 Rule
Step-1: Start of the program.
Step-2: Input Lower limit a
Step-3: Input Upper limit b
Step-4: Input number of sub intervals n
Step-5: h = (b – a)/n
Step-6: sum = 0
Step-7: sum = fun(a) + fun (b)
Step-8: for i = 1; i < n; i++
Step-9: if i%3=0:
Step-10: sum + = 2*fun(a + i*h)
Step-11: else:
Step-12: sum + = 3*fun(a+(i)*h)
Step-13: End of loop i
Step-14: result = sum*3*h/8
Step-15: Print Output result
Step-16: End of Program
Step-17: Start of Section fun
Step-18: temp = 1/(1+(x*x))
Step-19: Return temp
Step-20: End of section fun
Program:
Simpson’s 1/3 Rule
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
#include<math.h>
float fun(float f)
{
float m=1/exp(-f*f);
return m;
}//****** Defining the Function ******
void main( )
{
clrscr( );
float a,b,h,n,sum=0,res;
cout<<setprecision(4);
cout<<"Enter the Values of Lower & Upper Limit"<<endl;
cin>>a>>b;
cout<<"Enter the Number of Intervals"<<endl;
cin>>n;
h=(b-a)/n;
sum=fun(a)+(4*fun(a+h))+fun(b);
for(int i=3;i<n;i=i+2)
{
sum=sum+((2*fun(a+(i-1)*h)+(4*fun(a+(i*h)))));
}
res=(h/3)*sum;
cout<<"The Value of Integaration Is "<<res;
getch( );
}
Program:
Simpson’s 3/8 Rule
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
#include<math.h>
float fun(float f)
{
float m=1/exp(-f*f);
return m;
}//****** Defining the Function ******
void main( )
{
clrscr( );
float a,b,h,n,sum=0,res;
cout<<setprecision(4);
cout<<"Enter the Values of Lower & Upper Limit"<<endl;
cin>>a>>b;
cout<<"Enter the Number of Intervals"<<endl;
cin>>n;
h=(b-a)/n;
sum=fun(a)+fun(b);
for(int i=1;i<n;i++)
{
if((i%3)==0)
{
sum=sum+(2*fun(a+(i*h)));
}
else
{
sum=sum+(3*fun(a+(i*h)));
}
}
res=((3*h)/8)*sum;
cout<<"The Value of Integaration Is "<<res;
getch( );
}