#include<iostream>
#include<math.h>
using namespace std;
float f(float x)
{
return 1/(1+x*x); //Function f(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);
for(i=1;i<=n-1;i++)
{
if(i%3==0)
sum+=2*f(x0+i*h);
else
sum+=3*f(x0+i*h);
}
cout<<"Integral vlaue="<<(3*(h/8))*sum;
return 0;
}
#include<math.h>
using namespace std;
float f(float x)
{
return 1/(1+x*x); //Function f(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);
for(i=1;i<=n-1;i++)
{
if(i%3==0)
sum+=2*f(x0+i*h);
else
sum+=3*f(x0+i*h);
}
cout<<"Integral vlaue="<<(3*(h/8))*sum;
return 0;
}
No comments:
Post a Comment