The Chained Trapezoid Rule for some function.
There are a wide variety of applications in which it is desirable to calculate the area bounded by some curve. Using integral calculus we can usually directly compute the answer by taking the curve's Antiderivative, but there are some equations for which no simple antiderivative exists. One such equation is:
$$\Huge f(x) = e^{-x^{2}}$$
Written in plaintext as f[x] = exp(-x ^ 2), where exp(-x ^ 2) is equal to Euler's Number
raised to the power of -x ^ 2. As additional motivation, writing a program to transform functions per their antiderivatives can get very complicated,
so it would be great to somehow compute the area more directly. So, how do we calculate the area bounded by such a curve? One method is to
approximate the are using the Chained Trapezoidal Rule. Here's how it works:
First let's define that we want to compute the area underneath the curve in the range between two values x=a and x=b, bounded by the curve and by
the x-axis itself. We then split the region into n sub-regions, equally along the x-axis. Usually the more sub-regions we use, the more accurate
our approximation will be. We then evaluate f[x] for each x at the boundary of each subregion, and when a vertical line is drawn from each point to
the x-acis this creates a series of trapezoids. The area of any trapezoid between two consecutive points at x=i and x=j(where i<j) can be
calculated with the function A = (j - i) * (f[i] + f[j]) / 2. We then take the sum of all trapezoids in our range to get our approximation.
Note that any points below the x-axis will contribute negative area. For this function however, no points will be negative.
Input Data
The first line will contain Q, the quantity of testcases.
Q lines will follow, each containing 3 space-separated values: a b n.
Answer
Should contain Q space-separated values, each corresponding to the area under the curve f[x] = exp(-x ^ 2) between the values of x=a and x=b, approximated by using n trapezoids.
Error should be less than 1e-6.
Example
input data:
2
0.8 1 1
-1.84 0.12 14
answer:
0.089517 0.996853