PROGRAM FOR SIMPSON’S 1/3 RULE IN C++

PROGRAM FOR SIMPSON’S 1/3 RULE IN C++



// simpson's one third method to emliment the integral.
#include<iostream>
using namespace std;
#include<math.h>
float function(float x)
{
return(x*x/(1+x*x*x));
}
int main()
{
int i,n;
float h,x[20],y[20],so=0,se=0,result,a,b;
cout<<"enter the lower,upper limit and number of partition"<<endl;
cin>>a>>b>>n;
h=(b-a)/n; 
for(i=0;i<=n;i++)
{
x[i]=a+i*h;
y[i]=function(x[i]);
}
for(i=0;i<n;i++)
{
if(i%2==0)
{
se=se+y[i];
}
else
{
so=so+y[i];
}
}
result=(h/3)*(y[0]+y[n]+4*so+2*se);
cout<<result;
return 0;
}

No comments:

Post a Comment

INSTAGRAM FEED

@maurya_m_j