Trapezoidal Trap

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Sep 2007
Posts: 16
Reputation: SurviBee is an unknown quantity at this point 
Solved Threads: 0
SurviBee's Avatar
SurviBee SurviBee is offline Offline
Newbie Poster

Trapezoidal Trap

 
0
  #1
Dec 11th, 2008
I'm attempting to implement the trapezoidal method of calculating the integral of a curve. It's supposed to be the measure of water flow out of a tank's release valve.
The water flow is a function of time in seconds where Y is the rate of discharge and X is time.

Y = 200 * ( (2 / X + 1) - e(raised to the power of) -.693(X - 1)

The idea is to calculate total discharge and compare it to the tanks capacity. My code spits out an abominably huge number fr this, way over the purported 3107 gallon capacity.

My question is, have I made a beginner's mistake in my coding, or are my calculus skills really this rusty?

  1.  
  2. #include <cmath>
  3. #include <iostream>
  4. #include <iomanip>
  5.  
  6. using namespace std;
  7.  
  8. class Trap
  9. {
  10. private:
  11. float sidea;//function of time
  12. float sideb;//function of time
  13. float width;//representation of time passed
  14. float area;//water discharged
  15. public:
  16. float Y(float);//calculates side lengths
  17. void Area(float);//durr
  18. void Width(float nwidth){width = nwidth;};//assings width for sep. runs
  19. float GetArea(){return area;};
  20. };
  21.  
  22.  
  23. int main()
  24. {
  25. cout << "This program calculates the rate of flow from a 3107g tank\n";
  26. cout << "over 10000 seconds using the trapezoidal method.\n";
  27. cout << "The chart below displays the calculated total flow \n";
  28. cout << "based on 100, 1000, 10000, and 100000 trapezoids.\n\n";
  29.  
  30. cout << " Trapezoids Total Discharge(gallons) Tank Capacity(gallons)\n";
  31.  
  32. Trap zoids[100000];
  33. float Traplimits[5] = {10, 100, 1000, 10000, 100000};
  34.  
  35. int trapcount;
  36. float sum;
  37.  
  38. //loops through the calculations with different numbers of trapezoids
  39. //being calculated for
  40. for(int i = 0; i < 5; i++)
  41. {
  42. trapcount = 0;
  43. sum = 0;
  44.  
  45. //loops through array of trapezoids assigning new width
  46. //and then calculating area again
  47. for( int c = 0; c < Traplimits[i]; c++)
  48. {
  49. zoids[c].Width(10000/Traplimits[i]);
  50. zoids[c].Area(trapcount++);
  51.  
  52. }
  53.  
  54. //sums the areas of all calculated trapezoids in this run
  55. for( int p = 0; p < Traplimits[i]; p++)
  56. sum += zoids[p].GetArea();
  57.  
  58. cout << fixed << showpoint << setprecision(2) << left;
  59. cout << setw(14) << Traplimits[i];
  60. cout << setw(27) << sum;
  61. cout << "3107\n";
  62.  
  63. }
  64.  
  65. cin.get();
  66. cin.get();
  67. return 0;
  68.  
  69. }
  70.  
  71. float Trap::Y(float time)
  72. {
  73. return 200 * ( (2/(time + 1)) - exp(-0.693*(time + 1)) );
  74. }
  75.  
  76. void Trap::Area(float tcount)
  77. {
  78. // sidea = Y(time passed until beginning of trapezoid
  79. sidea = Y(tcount++ * width);
  80.  
  81. //sideb = Y(time passed up to end of trapezoid
  82. sideb = Y(tcount * width);
  83.  
  84. //formula for area of a trapezoid
  85. area = 0.5 * (sidea + sideb) * width;
  86. cout << area << endl;
  87. }
Last edited by SurviBee; Dec 11th, 2008 at 6:15 pm.
Bazoo will prevail!!
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,678
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 263
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Trapezoidal Trap

 
0
  #2
Dec 11th, 2008
Debug it by throwing a bunch of output statements in the code to see where it deviates from what you expect or use the debugger that probably came with your compiler to monitor variable values as you step through the code.
Klatu Barada Nikto
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 275
Reputation: dougy83 is on a distinguished road 
Solved Threads: 45
dougy83 dougy83 is offline Offline
Posting Whiz in Training

Re: Trapezoidal Trap

 
0
  #3
Dec 12th, 2008
-
Last edited by dougy83; Dec 12th, 2008 at 8:41 am. Reason: wrote some rubbish
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 392
Reputation: StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light 
Solved Threads: 72
StuXYZ StuXYZ is offline Offline
Posting Whiz

Re: Trapezoidal Trap

 
0
  #4
Dec 12th, 2008
Hi,

I am amazed at this code. The normal way to do a trapezium integral is simply to loop over the value like this
[code=c++]
const int N(500);
const double startTime(0.0);
const double endTime(100.0);
double area(func(startTime)+func(endTime));
area/=2.0;
for(int i=1;i<N-1;i++)
{
area+=func(i);
}
area*=(endTime-startTime)/N;
[code]

What you are doing creating 100000 objects to do that is absolutely beyond me. However, I think you have the code mostly right ! So well done

BUT the problem is MATHEMATICALLY very ill-conditioned. I suspect that is why it was set. I guess that you will use Simpson next and then
runge-kutta on the underlying differential equation. (I will comment just about ANY monotonically decreasing/increasing function is ill-conditioned under this integral method.


If you REALLY want to get the right answer with a trapiz. method then do the problem in log coordinates.
Last edited by StuXYZ; Dec 12th, 2008 at 9:52 am. Reason: Slightly more accurate code.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 16
Reputation: SurviBee is an unknown quantity at this point 
Solved Threads: 0
SurviBee's Avatar
SurviBee SurviBee is offline Offline
Newbie Poster

Re: Trapezoidal Trap

 
0
  #5
Dec 14th, 2008
Thanks so much, and yes never done this kind of math before with c++ so i thought there might be some kind of misconception on my part. Thanks for the reply, i appreciate not telling me to use my debugger lol.
Bazoo will prevail!!
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 1
Reputation: sweetkim_2008 is an unknown quantity at this point 
Solved Threads: 0
sweetkim_2008 sweetkim_2008 is offline Offline
Newbie Poster

c++

 
0
  #6
3 Days Ago
example of the area of trapezoid in a c++ program....plssssssssssssssss........
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC