// C++ code for the numerical integration (trapezoid method) #include #include using namespace std; double fun(double x); int main() { // variable declarations int n=0; // number of partition elements double a, b; // interval bounds double sum = 0; // integral sum // input parameters for trapezoid rule cout << "This program calculates the integral of e^{-x^2}} on the interval [a,b]" << endl << "Please enter lower bound a = "; cin >> a; cout << "Please enter upper bound (b>a) b = "; cin >> b; cout << "Please enter the number of elements in partition (n>1) n = "; cin >> n; // calculate the length of element double h = (b-a)/(n-1); // initialise x double x = a; // main computation loop for(int i=1 ; i <= (n-1) ; i++) { sum = sum + fun(x) + fun(x+h); x=x+h; } sum = sum*h/2; // output the result cout << "The result of summation is: " << sum << endl; return 0; } // end main() double fun(double x) { return 1.0/exp(x*x); }