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

Trapezoidal Rule to find integration of a Function

#include<iostream>
#include<math.h>

using namespace std;
float f(float x)
{
    //return exp(x*x); // function f(x)
    return (x*x)/(1+x*x*x); //Function f(x)
    //return exp(-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;
    cout<<"\nh="<<h;
    sum=f(x0)+f(xn);
    for(i=0;i<n;i++)
        sum+=2*f(x0+i*h);
    cout<<"\nIntergral Value="<<(h/2)*sum;
    return 0;
}

No comments:

Post a Comment