Trapezoidal Rule Using C++ with Output
C++ program for evaluating definite integral of a given function using Trapezoidal rule or method.
C++ Program
#include<iostream>
#include<math.h>
/* Define function here */
#define f(x) 1/(1+pow(x,2))
using namespace std;
int main()
{
float lower, upper, integration=0.0, stepSize, k;
int i, subInterval;
/* Input */
cout<<"Enter lower limit of integration: ";
cin>>lower;
cout<<"Enter upper limit of integration: ";
cin>>upper;
cout<<"Enter number of sub intervals: ";
cin>>subInterval;
/* Calculation */
/* Finding step size */
stepSize = (upper - lower)/subInterval;
/* Finding Integration Value */
integration = f(lower) + f(upper);
for(i=1; i<= subInterval-1; i++)
{
k = lower + i*stepSize;
integration = integration + 2 * (f(k));
}
integration = integration * stepSize/2;
cout<< endl<<"Required value of integration is: "<< integration;
return 0;
}
Trapezoidal Method C++ Program Output
Enter lower limit of integration: 0 Enter upper limit of integration: 6 Enter number of sub intervals: 6 Required value of integration is: 1.4108
Recommended Readings
- Numerical Integration Trapezoidal Method Algorithm
- Numerical Integration Using Trapezoidal Method Pseudocode
- Numerical Integration Using Trapezoidal Method C Program
- Trapezoidal Rule Using C++ with Output
- Numerical Integration Using Simpson 1/3 Method Algorithm
- Numerical Integration Using Simpson 1/3 Method Pseudocode
- Numerical Integration Using Simpson 1/3 Method C Program
- Simpson 1/3 Rule Using C++ with Output
- Numerical Integration Using Simpson 3/8 Method Algorithm
- Numerical Integration Using Simpson 3/8 Method Pseudocode
- Numerical Integration Using Simpson 3/8 Method C Program
- Simpson 3/8 Rule Using C++ with Output