Trapezoidal rule can be applied to find the value of a limiting integer programmatically in both cases whether individual functional values are given or not. A complete algorithm follows the both process below.
/*********
Trapezoidal rule complete algorithm
1. start
2. read n= no of intervals
3. read initial and final values ie a,b
4. h=(b-a)/n;
5. set x[0]=a; x[n]=b;
6. next element
i= 1 to n-1
x[i]=x[i-1]+h
7. evaluating/reading function
i=0 to n
f[i]=function or input functional values as cin<
8. 1st &last terms
sum=f[0]+f[n]
9. i= 1 to n-1
sum= sum + 2 * ( f[i] )
10. sum=h/2*sum
11. display sum
12. stop
***********/
//To calculate the value of an integer using Trapeziodal Rule (no corressponding functional values provided)
#include
#include
#include
#include
void main()
{
double n,i,a,b;
double x[20],f[20], sum;
float h;
clrscr();
cout<<<“Enter no of intervals: “;
cin>>n;
cout<<<“Enter limiting values:\t”;
cin>>a>>b;
h=(b-a)/n;
x[0]=a;
x[n]=b;
for(i=1;i<=n-1;i++)
{
x[i]=(x[i-1]+h);
}
for(i=0;i<=n;i++)
{
f[i]=exp(x[i]*tan(x[i]));
}
sum=f[0]+f[n];
for(i=1;i<=n-1;i++)
{
sum=sum+(2*f[i]);
}
sum=(h/2.0)*sum;
cout<<<“The required integral value = “<
getch();
}
/* To evaluate value of an integral using Trapezoidal Rule when the functional values are given */
#include
#include
#include
#include
void main()
{
double n,a,b,h,sum,x[20],i,f[20];
clrscr();
cout<<“To Calculate The Integral Value Using Trapezoidal Rule”<
cout<
cout<<“The given functional vaue is e^(x*tanx) \n”<
cout<<“Enter the Number of interval “;
cin>>n;
cout<<“There are “<<<” intervals\n”<
cout<<“Enter the initial point\t”<
cin>>a;
cout<<“Enter the final point\t”<
cin>>b;
cout<<“The limit of integration is from “<<<“to”<<<
h=(b-a)/n;
x[0]=a;
x[n]=b;
cout<
for(i=1;i<=n-1;i++)
{
x[i]=x[i-1]+h;
}
cout<<“Enter the functional value accordingly \n”;
for(i=0;i<=n;i++)
{
cout<<“f”<<<“= “;
// f[i]=exp(x[i]*tan(x[i])) ;
cin>>f[i];
}
sum=f[0]+f[n];
for(i=1;i<=n-1;i++)
{
sum=sum+2*f[i];
}
sum=(h/2)*sum;
cout<<“The required value of integration is”<
getch();
}
To calculate the value of an integer using Trapezoidal rule both when functional values are given and values NOT given in C++ Programming Language for Numerical Methods for Engineering Students