All Programs are Written and Compiled in Dev C++. So, it may generate some error in case of other compilers and may need some modifications in program. Download Dev C++

Wednesday 22 April 2015

Simpson's 3/8th Rule to find Integration of Function

#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;
}

No comments:

Post a Comment