Pages

Subscribe:

About Template

Random Posts

C++ Program of The Definite Integral


Name: Definite Integral Calculator
By: Marco Scandurra
Description of the Program: This code calculates definite integrals numerically using the Riemann sum.

The program is set to a default integrand function F(x)=Exp[x/3+2]. You can change this function to whatever you want in the code. The code returns the numerical value of the integral of F(x) between 'a' and 'b' with 5 decimal digits. 
In the screenshot below, I entered the first limit  of this integrand function from 3 to 5 which is equal to 57.1073 and then, I reversed the limit of this function from 5 to 3 which is just the negative of the first result. 
//Below are the Codes of the Program:

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

const double n=1000000;
double f(double x);
//double upp1(double p, double q);
//double upp2(double p, double q);
int main()
{
char answ;
double t,j;
double a,b;
cout<<"\t______________________________________________\n";
cout<<"\t***DEFINITE INTEGRAL CALCULATOR***\n";
cout<<"\t  \n";
cout<<"\t   By: Marco Scandurra\n";
cout<<"\t______________________________________________\n";
cout<<endl;
cout<<endl;
cout<<"\t This program estimates definite integrals\n";
cout<<"\t numerically using the Riemann sum. \n";
cout<<endl;
cout<<"\t The integrand is now set to F(x)= exp(x/3 + 2)"<<endl;
cout<<"\t You can chance the integrand in the code.\n";
cout<<endl;
do{
cout<<"\t Enter integration limits from 'a' to 'b': ";
cin>>a;cin>>b;cin.ignore();
double g=0;
for(j=1;j<=n;j++)
{
 g=g+( f(a+(b-a)/n*j) * (b-a)/n );
}
cout<<endl;
cout<<"\a \t Integral= "<<g<<endl;
cout<<endl;
cout<<"Again? (y/n): ";cin>>answ;
}while(answ=='y');

 cin.get();
 return 0;
}
double f(double x)// This is the integrand function, feel free to change it!
{
return ( exp(x/3+2) );
}
//double upp1(double p, double q)
//{
//return( floor( top*(q-p)/f(q) ) );
//}
//double upp2(double p, double q)
//{
//return( floor( top*f(q)/(q-p) ) );
//}