#include<iostream>
#include<math.h>
using namespace std;
float f(float x)
{
return 1/(1+x*x);
}
int main()
{
int i,n;
float x0,xn,h,sum;
cout<<"Enter no of Intervals: ";
cin>>n;
cout<<"Enter x0 & xn: ";
cin>>x0>>xn;
h=(xn-x0)/n;
sum=f(x0)+f(xn)+4*f(x0+h);
for(i=3;i<=n-1;i+=2)
sum+=4*f(x0+i*h)+2*f(x0+(i-1)*h);
cout<<"Integral Value="<<(h/3)*sum;
return 0;
}
#include<math.h>
using namespace std;
float f(float x)
{
return 1/(1+x*x);
}
int main()
{
int i,n;
float x0,xn,h,sum;
cout<<"Enter no of Intervals: ";
cin>>n;
cout<<"Enter x0 & xn: ";
cin>>x0>>xn;
h=(xn-x0)/n;
sum=f(x0)+f(xn)+4*f(x0+h);
for(i=3;i<=n-1;i+=2)
sum+=4*f(x0+i*h)+2*f(x0+(i-1)*h);
cout<<"Integral Value="<<(h/3)*sum;
return 0;
}
No comments:
Post a Comment