Write a C++ program for the trapezoidal rule.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
#include<iostream> #include<math.h> /* Defining the function*/ #define f(x) 1/(1+pow(x,2)) using namespace std; int main() { float LowerLimit, UpperLimit, integrationValue=0.0, stepSize, k; int i, TotalsubIntervals; cout<<"Please subInterval UpperLimit of integrationValue: "; cin>>LowerLimit; cout<<"Please subInterval UpperLimit of integrationValue: "; cin>>UpperLimit; cout<<"Please subInterval number of sub intervals: "; cin>>TotalsubIntervals; stepSize = (UpperLimit - LowerLimit)/TotalsubIntervals; integrationValue = f(LowerLimit) + f(UpperLimit); for(i=1; i<= TotalsubIntervals-1; i++) { k = LowerLimit + i*stepSize; integrationValue = integrationValue + 2 * (f(k)); } integrationValue = integrationValue * stepSize/2; cout<< endl<<"Required value of integrationValue is: "<< integrationValue; return 0; } |
Output
