Trapezoidal Rule for the Evaluation of Definite Integrals in c++ language

  

Solution in C++: 

///******Bismillahir-Rahmanir-Rahim******///

///AH Tonmoy

///Department of CSE,23rd batch

    ///Islamic University,Bangladesh

    #include<bits/stdc++.h>
    using namespace std;
    double givenfuction( double x)
    {
        double a=1/x;// a=f(x) given  function
        return a;
    }
    int main()
    {
        int n,i;
        double a,b,dx,sum=0,integral;
        cout<<"Enter the limits of integration , a= ";
        cin>>a;
        cout<<"Final limit, b=";
        cin>>b;
        cout<<"Enter the no. of subintervals, n= ";
        cin>>n;
        double x[n+1],y[n+1];
        dx=(b-a)/n; // del x value
        for (i=0; i<=n; i++)
        {
            //loop to  and y0,...yn
            x[i]=a+i*dx;  //evaluate x0,x1,x2...xn and store them in x[i] array
            y[i]=givenfuction(x[i]);  // evaluate y0,y1,y2...yn and also store y[i] array
        }
        // f(x)=dx/2.0(f(x0)+2f(x1)+2f(x2)+2f(x3)+..........f(2*x(n-1)+f(x(n)))
        for (i=1; i<n; i++)          //loop to evaluate h*(y1+y2...+yn-1)
        {
            sum=sum+dx*y[i];
        }
        integral=dx/2.0*(y[0]+y[n])+sum;        //h/2*[y0+yn+2(y1+y2+y3+...yn-1)]
        cout<<"The definite integral value  is "<<integral<<endl;
        return 0;
    }


    No comments

    Most View Post

    Recent post

    Codeforces Round 971 (Div. 4) 2009C. The Legend of Freya the Frog Solution

      Problem Link    https://codeforces.com/contest/2009/problem/C S olution in C++: /// Author : AH_Tonmoy #include < bits / stdc ++. h &g...

    Powered by Blogger.