C++ project - Urget
CMPSC 201 – Summer 2016
Project 2 Worth 45 points
Due Monday 7/11 by 11:00 pm
Computer programs can greatly aid in many methods of numerical analysis. One such method is integration under a curve. The area under a curve can represent stored energy or other important properties. The area under a curve may be approximated by dividing the area into trapezoids, calculating the area of each trapezoid, and then summing those smaller areas. For example, using the diagram below, the area under the curve for the region from a to b could be approximated by dividing the region into 5 smaller areas. The first area can be defined by a, f(a), x1, and f(x1). The area of a trapezoid is given by the equation area = 0.5 base (height1 + height2). In our example of the first trapezoid the base is equal to x1 – a, height1 is equal to f(a), and height2 is equal to f(x1). Therefore the area of the first trapezoid is equal to 0.5(x1 – a)(f(a) + f(x1)). Similarly, the area of the second trapezoid can be calculated as 0.5(x2-x1)(f(x1) + f(x2)) and the area for the third trapezoid is 0.5(x3 – x2)(f(x3) + f(x2)). The total area under the curve from a to b would be the sum of the 5 trapezoids. For this assignment you will find x, y data in a file called xydata.dat. (This is an ASCII file that can be opened with NOTEPAD or WORDPAD.) Copy this file to your computer or space on U-drive where you work. Open the file using notepad so you review the file, but do not edit the file in any manner. Note the file contains two columns of data, one for x and one for y. Rather than using a function to calculate y at various points, the y values have already be measured and recorded for you. Consecutive pairs of x-y data should be used to designate the trapezoids, e.g. the first trapezoid would be defined by x1, y1, x2, and y2; the second trapezoid would be defined as x2, y2, x3, and y3; the third trapezoid by x3, y3, x4, and y4, etc. Start this program early, so if you need help, there is sufficient time for you to see me or the TA for help. 1. For this assignment you are to write a computer program to calculate the area under a curve
for x-y data contained in a file (xydata.dat). This file can be opened using NOTEPAD if you choose to see all file times. Do not rename this file. Develop the algorithm necessary to calculate the area under curve that is represented by this data.
f(b)
f(x1)
f(a)
x4 x3 x2 x1 b a
f(x2) f(x3) f(x4)
x
y
2. Calculate by hand the area of individual trapezoids and the total area using the first 6 x-y sets (creates 5 trapezoids). This information will be your test data.
3. Code your algorithm (make sure you follow the homework guidelines) and do not use any concepts beyond Chapter 5 of your text book.
4. Run the program using the first 6 x-y sets. (Remember to skip the fist line of data using the ignore() function) Check to ensure that the results form program agree with your test data. If the results do not agree, debug your program
5. Rerun the program using all the data sets. Do not use a count-controlled loop, but test for end of file or state of the extraction operator (refer to lecture power points). Print out the total area.
Make sure that you have followed the Assignment Guidelines posted on CANVAS and submit your source code there. If you need help, arrange to meet with Dr. Quick or the TA. Do not search the internet for a solution (it is a violation of academic integrity).
- CMPSC 201 – Summer 2016