### Calculating Area Under Curve

Pages: 12
Okay through all of the posts you guys made I have my program working (mainly based on Chervil's updates). Now I just need to have the program ask the user if they want to calculate the area using rectangles, trapezoids or both. The code I have uses the area for trapezoids. What would be the most efficient way to loop it? Here is my current code.

 ``123456789101112131415161718192021222324252627282930313233343536`` `````` #include using namespace std; double f(float); int main(){ double a; double b; int n; double h; double x; double area; cout << "What is the left x value? "; cin >> a; cout << "What is the right x value? "; cin >> b; cout << "How many pieces do you want to use? "; cin >> n; h = (b-a)/n; area = (f(a)+f(b))/2; for(int i=1;i

Thanks for any help.
I don't think the OP needs this, is more background information on the trapezium algorithm. Please see the diagram: http://oi49.tinypic.com/2rm4bdf.jpg

The area under the curve is divided into a series of vertical strips.
If we assume the width of each one is h, then the area of the first one is
h * (a + b) / 2
where a and b are the heights (value of the function) at the left and right edges of the trapezium.

The areas of the others are similar. Thus the total area is:
h * (a + b) / 2
h * (b + c) / 2
h * (c + d) / 2
h * (d + e) / 2

That is in fact one of the algorithms I used. But it is inefficient, since the right-hand edge of one strip is the same as the left-hand edge on the next.

It can be simplified as follows:
Total area =
h * (a + b + b + c + c + d + d + e) / 2
=
h * ((a + e) / 2 + (b + c + d))
and this appears to be the algorithm used by the OP.
1. calculate (a + e) / 2
2. use a loop to calculate (b + c + d)
3. Finally, multiply the total from parts 1 and 2 by h.
Chervil wrote:
 It can be simplified as follows: Total area = h * (a + b + b + c + c + d + d + e) / 2 = h * ((a + e) / 2 + (b + c + d)) and this appears to be the algorithm used by the OP. 1. calculate (a + e) / 2 2. use a loop to calculate (b + c + d) 3. Finally, multiply the total from parts 1 and 2 by h.

That is the gist of what I didn't realize in my previous posts. I went back to an old calculus textbook and re-read the sections on Reimann sums (the introduction to integration). It indirectly discussed what Chervil posted above. That lead me to realize that term in line 21 of the OP's code, the (a + e) / 2 in Chervil's post, is needed to arrive at the exact area under a curve when the heights of the left or right edges of the rectangle slices are used to compute their areas.

Pondering this thread is not how I had envisioned spending most of my Sunday. But I learned something new and improved my programming skills. I thank the OP for his post and Chervil for his important input.
Chervil wrote:
 It can be simplified as follows: Total area = h * (a + b + b + c + c + d + d + e) / 2 = h * ((a + e) / 2 + (b + c + d)) and this appears to be the algorithm used by the OP. 1. calculate (a + e) / 2 2. use a loop to calculate (b + c + d) 3. Finally, multiply the total from parts 1 and 2 by h.

That is the gist of what I didn't realize in my previous posts. I went back to an old calculus textbook and re-read the sections on Reimann sums (the introduction to integration). It indirectly discussed what Chervil posted above. That lead me to realize that term in line 21 of the OP's code, the (a + e) / 2 in Chervil's post, is needed to arrive at the exact area under a curve when the heights of the left or right edges of the rectangle slices are used to compute their areas.

Pondering this thread is not how I had envisioned spending most of my Sunday. But I learned something new and improved my programming skills. I thank the OP for his post and Chervil for his important input.
G3PO wrote:
Now I just need to have the program ask the user if they want to calculate the area using rectangles, trapezoids or both. The code I have uses the area for trapezoids. What would be the most efficient way to loop it?

I don't know how important it is to perform this efficiently (probably a matter of opinion). The main priority I think is getting the correct answer.

My own version (written without reference to any of the code in this thread) uses two short functions. One calculates the area of a trapezium. The other calculates the area of a rectangle.

My functions are called like this:
 ``12345678910`` `````` double increment = (end-start) / steps; double Area1 = 0.0; double Area2 = 0.0; for (int i=0; i

This gets results in Area1 and Area2 for the rectangle and trapezium methods. It could be regarded as inefficient, but it generates results which seem sensible.

Now the above doesn't really match the structure of the code previously posted. I don't recommend discarding your existing code which works. Instead, I'd suggest adding a separate loop to calculate the area using the Rectangle algorithm. If the user wants both, the execution time will be longer (I tried mine with 1,000,000,000 slices and it took several minutes to complete. But that's on a fairly ordinary laptop computer).

In fact' to keep the code clean and well-structured, I'd suggest taking the entirety of your current calculation, this block,
 ``1234567`` `````` h = (b-a)/n; area = (f(a)+f(b))/2; for(int i=1;i

and make it into a separate function which takes parameters a, b and n and returns the area.

The do something similar for the rectangle algorithm.

Finally add the code in main to select one or both depending on user input.

Those are just my thoughts of course, you don't have to follow them.
Last edited on
 I don't know how important it is to perform this efficiently (probably a matter of opinion). The main priority I think is getting the correct answer.
That's right.

However, forget the trapeziums as they become insignificant, just use rectangles. The method is called a Riemann Sum. As the rectangles get narrower, the answer becomes more accurate.

And from a math point of view, as the number of rectangles approach infinity, it becomes an integral.
Topic archived. No new replies allowed.
Pages: 12