please help. comment your code as much as possible. I would appreciate it. Compu
ID: 3565388 • Letter: P
Question
please help. comment your code as much as possible. I would appreciate it.
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 xi - a, height is equal to f(a), and height2 is equal to f(xi). 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 your work. Open the file using notepad so you review the file, but do not edit the file in any manner and do not change the file name. 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 xl, yl, 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. 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. Develop the algorithm necessary to calculate the area under curve that is represented by this data. 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. Code your algorithm (make sure you follow the homework guidelines) and do not use any concepts beyond Chapter 5 of your text book. 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 your program agrees with your calculations. 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. Do not use arrays or declare more than two variables for x-values and two for y-values.Explanation / Answer
#include float start_point, /* GLOBAL VARIABLES */ end_point, total_area; int numtraps; main( ) { void input(void); float find_area(float a,float b,int n); /* prototype */ print(Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.