Tuesday, October 21, 2014

Simpson's 1/3 rule

#include<iostream.h>
float f(float x)
{
    return(x/(1+x));
}

void main()
{
    float x0,xn,h,s;
    int i,n;
    cout<<"\nEnter values of x0,xn and interval=";
    cin>>x0>>xn>>n;
    h=(xn-x0)/n;
    s=f(x0)+f(xn)+4*f(x0+h);
    for(i=3;i<=n-1;i+=2)
    {
        s+=4*f(x0+i*h)+2*f(x0+(i-1)*h);
    }
   cout<<"\nThe Integration is="<<h*s/3;
}

No comments:

Post a Comment