# C programming integral problem

*label*Programming

*timer*Asked: Nov 27th, 2013

*account_balance_wallet*$20

### Question Description

Write a program integral.c that calculates rectangular and trapezoidal integral approximations for a function. Your program will ask the user for the starting and ending time-points of the integration and the number of time points (polygons) to use in the approximation. In addition to printing out the estimated integral, the program will provide the error in the approximation.

In the ﬁgure on the left, when we integrate a function we are taking the area under the curve between t = a and t = b (in blue), with the caveat that areas calculated for ranges of t for which f(t) < 0 are subtracted from areas calculated for ranges of t for which f(t) > 0. Thus, in the ﬁgure, the integral of f(t) over the range a to b is equal the blue area labeled 2 subtracted from the blue area labeled 1.

We can approximate this integral computationally (instead of using analytical integration for an exact answer, i.e. what you learn in calculus) by using a number of methods. Two straightforward methods of approximation are the rectangle and trapezoid methods. With these methods, the integration range is split into n segments and the area of these segments is approximated with a simple polygon. The estimated areas of all of the n segments are signed as either positive or negative (based upon whether they are above or below the t-axis) and are summed.

The center ﬁgure (green) demonstrates the rectangle method for n = 4. The width of the rectangle is 4t = (b − a)/n. The height of the rectangle is f(t) evaluated at the midpoint of the rectangle, so for the ﬁrst rectangle, the height is f(a + 4t/2). Thus, the area of the ﬁrst rectangle (with the appropriate +/- sign) is f(a + 4t/2) 4 t.

The right ﬁgure (yellow) demonstrates the trapezoid method for n = 4. Each trapezoid has two dif- ferent heights (h1 and h2) and one width (w). The width, w, is also 4t = (b − a)/n. The heights are found by evaluating f(t) at the vertical edges of the trapezoid. So for the ﬁrst trapezoid, heights are h1 = f(a) and h2 = f(a + 4t). For the second trapezoid the heights are h1 = f(a + 4t) and h2 = f(a + 2 ∗ 4t) (shown on ﬁgure). Thus, the area of each trapezoid (with the appropriate +/- sign) is (h1 + h2) ∗ w/2

For this program you will calculate these approximations for the function f(t) = t3 − 2t2 − 10t + 10, you’ll ask the user for the integral range (a and b) and the number of polygons (n) to use. To calculate the error, evaluate the analytical integral of f(t) and subtract each approximation from it. The integral is: R b a f(t)dt = (b4 4 − 2b3 3 − 10b2 2 + 10b) − (a4 4 − 2a3 3 − 10a2 2 + 10a) (plug in a and b to calculate the integral over the range a to b). Your program must include the following functions:

• double calcTrap(double t, double dt) - Calculates and returns the area of the trapezoid between t and t+dt.

• double f(double t) - Calculates and returns the value of f(t) evaluated at t and might be useful for implementing the above functions!

• double fIntegral(double a, double b) - Calculates and returns the integral of f(t) over the range a to b using the analytical integral described above (used to check the error in the approximations).

We suggest that you build and debug the basic functionality of these functions before using them to complete the full program. Example execution:

(˜)$ a.out

Enter the integral range (a,b): 0,1

Enter the number of polygons to use: 1

Rectangle Estimate of 4.625000 is off by 0.041667

Trapezoidal Estimate of 4.500000 is off by -0.083333

(˜)$ a.out Enter the integral range (a,b): 0,1

Enter the number of polygons to use: 10

Rectangle Estimate of 4.583750 is off by 0.000417

Trapezoidal Estimate of 4.582500 is off by -0.000833

(˜)$ a.out Enter the integral range (a,b): -1,1

Enter the number of polygons to use: 100

Rectangle Estimate of 18.666800 is off by 0.000133

Trapezoidal Estimate of 18.666400 is off by -0.000267

(˜)$ a.out Enter the integral range (a,b): 5,7

Enter the number of polygons to use: 100

Rectangle Estimate of 198.665600 is off by -0.001067

Trapezoidal Estimate of 198.668800 is off by 0.002133

(˜)$ a.out Enter the integral range (a,b): 10,100

Enter the number of polygons to use: 10000

Rectangle Estimate of 24282899.900980 is off by -0.099020

Trapezoidal Estimate of 24282900.198047 is off by 0.198047

## Tutor Answer

Brown University

1271 Tutors

California Institute of Technology

2131 Tutors

Carnegie Mellon University

982 Tutors

Columbia University

1256 Tutors

Dartmouth University

2113 Tutors

Emory University

2279 Tutors

Harvard University

599 Tutors

Massachusetts Institute of Technology

2319 Tutors

New York University

1645 Tutors

Notre Dam University

1911 Tutors

Oklahoma University

2122 Tutors

Pennsylvania State University

932 Tutors

Princeton University

1211 Tutors

Stanford University

983 Tutors

University of California

1282 Tutors

Oxford University

123 Tutors

Yale University

2325 Tutors