CSC601 Exam. Section C ====================== Sample question. Use of trapezoid rule in numerical integration -------------------------------------------------------------------- Introduction ------------- The trapezoid rule is an important technique for numerical integration of mathematical functions on a given interval [a,b]. It is based on an estimation of the area under a curve of the integrated function f(x) using trapezoids. The estimation of integral[f(x)dx]{from a to b} is approached by first dividing the interval [a,b] into subintervals according to the partition: P = {a = x(1) < x(2) < x(3) < ... < x(n) = b} For each such partition of the interval (in general the partition points x(i) need not be uniformly spaced), an estimation of the integral by the trapezoid rule is obtained. A typical individual trapezoid has the subinterval [x(i), x(i+1)] as its base, and the two vertical sides are f(x(i)) and f(x(i+1)). The areas is equal to the base times the average height: A(i) = .5*(x(i+1)-x(i))*(f(x(i))+f(x(i+1))) The total area of trapezoids for partition P is: T(f,P)= Sum [A(i)] {i=1, ..., n-1} (formula 1) Consider the case of uniform spacing, that is for all i: (x(i+1)-x(i)) is the same. Let (x(i+1)-x(i)) be denoted by h. That is, h = (b-a)/n-1 and therefore x(i) = a + h*(i-1) In this case (formula 1) can be re-written as follows: T(f,P)= .5*h*Sum [f(x(i)+f(x(i+1))] {i=1, ..., n-1} Problem Specification --------------------- Consider the problem of calculating the integral of function f(x)=e^(-x^2) on an interval [a,b]. (1) Outline an algorithm (using design diagrams) for a program which receives two interval points a and b, so that a < b, and the number of partition elements n (n is asked to be sufficiently large in relation to the interval [a,b] inorder to guarantee the reasonable accurracy of this integration method) and computes the estimated value of the integral integral[f(x)dx]{from a to b} using the trapezoid method with uniform spacing. [10 marks] (2) Write a C++ implementation for this algorithm [10 marks]