PA 2.1: Area under the curve
- Due Sep 28, 2021 by 11:59pm
- Points 10
- Submitting a file upload
- File Types cpp
- Available after Sep 14, 2021 at 2pm
When completing this programming assignment, you may work with up to one other person. No groups of more than two are permitted.
There are many applications for which we must calculate the area under a function (in the mathematical sense). If you've taken a Calculus II course, you'll know that this is done using integration when possible. However, we can also approximate the area under a function. In this programming assignment, you will create a program that will approximate the area under 2-degree polynomial functions of the form: y = a + bx + cx^{2} between some starting and ending value of x. For example, here is the polynomial y = 1 + 2x + 3x^{2}:
The area between the interval starting at x = 0.1 and ending at x = 3 is shaded in blue here:
To calculate the area, you will divide the interval into thirds. In each third, you will compute the area of the trapezoid that fits the points (x1, 0), (x1, y1), (x2, y2), (x2, 0):
Computing the area of a trapezoid with a square end involves splitting it into a rectangle and triangle, computing the area of each of those, and adding them together (as covered in zyLab 2.38). Note that we need to allow for trapezoids angled both upwards and downwards (e.g., y = 1 + 2x + 3x^{2} curves upward while y = 1 + 2x - 3x^{2 }curves downward as x increases).
After computing the area of the trapezoid for each third of the interval, they can be summed to produce the approximate area under the polynomial within that interval:
The total approximate area for this polynomial between x=0.1 and x=3 is 40.244 (when not limited by the rounding shown in the figure above).
Write your program in using VS Code. Your program should ask the user for each of the coefficients in the polynomial (i.e., in a + bx + cx^{2}, the coefficients are a, b, and c) and the beginning and end of the interval. The program should compute the approximate area under the polynomial within that interval using three non-overlapping trapezoids of equal width, then output the polynomial equation, interval, and area approximate area. For example, in the example above, the output might look like this:
Approximate area under y = 1 + 2x + 3x^2 between x = [0.1, 3] is 40.244
Copy the following header and specifications checklist and paste it at the top of your source code. As you complete the specifications, fill in the [ ] next to that specification in the check list (e.g., like this: [x]). I will not grade submissions with missing specifications; please do not submit if you cannot check all of them off. This specs checklist is essentially a way for you to self grade before you submit your program.
// Name:
// Date:
// Partner:
//
// Specifications checklist for PA Option 2.1
//
// General specs:
// [ ] the header includes your name and anyone you worked with
// [ ] the header includes this specifications checklist with all completed
// specifications checked off: [x]
// [ ] the code is indented properly (inside of every block, code is indented
// one more tab)
// [ ] each chunk of code that "hangs together" (works toward a higher level goal):
// [ ] includes a brief, useful comment above it
// [ ] is separated from the next chunk by a blank line
// [ ] there are no really long lines of code or comments (if you have long
// lines, split them across multiple shorter lines)
// [ ] identifiers are well named
// [ ] the program compiles
// [ ] the program runs without crashing or hanging
// [ ] all output and prompts look clean (correct spelling, capitalization,
// nothing squished)
// [ ] all prompts make it clear what data the user is expected to enter and in
// what format and work as expected
//
// Specific specs:
// [ ] the program prompts and reads from the user the following information:
// [ ] a, b, c in the polynomial y = a + bx + cx^2
// [ ] the starting and ending interval of x to calculate the area between
// [ ] the program approximates the area under the polynomial within the given
// interval using three trapezoids, each a non-overlapping third of the interval
// [ ] the program outputs the approximate area at the end with a label (e.g.,
// "Approximate area under y = 1 + 2x + 3x^2 between x = [0.1, 3] is 40.244")
// [ ] the program is accurate -- your program's approximations match the expect
// results
Here are the expected results for a number of polynomials:
Polynomial | Interval start | Interval end | Approximate area under |
1 + 2x + 3x^{2} | 0.1 | 3 | 40.2439 |
1 + 2x + 3x^{2} | 0 | 10 | 1165.56 |
5 - x + 2x^{2} (note: the second coefficient is -1) |
0 | 5 | 100.463 |
1 + x + x^{2} | 0 | 0.5 | 0.668981 |
3 + 4x - 0.1x^{2} | 10 | 30 | 778.519 |
If you are working with a partner, both of you must submit individually.