Menu Close

Simpson’s Rule C++

#include<iostream>
#include<iomanip>

using namespace std;

float y(float x)
{
return 1/(1+x*x);
}

int main()
{
float x0,xn,h,s;
int i,n;

cout<<"Enter the x0,xn, no. of subintervals"<<endl;
cin>>x0>>xn>>n;
cout<<fixed;

h=(xn-x0)/n;
s=y(x0)+y(xn)+4*y(x0+h);

for(i=3;i<=n-1;i=i+2)
s +=4*y(x0+i*h)+2*y(x0+(i-1)*h);


cout<<"Value of integral is ";
cout<<setw(6)<<setprecision(4);
cout<<(h/3)*s<<endl;
return 0;
}

Output

Enter the x0,xn, no. of subintervals
0 6 6
Value of integral is 1.3662

More Related Stuff