943,601 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1774
  • C++ RSS
Dec 11th, 2008
0

Trapezoidal Trap

Expand Post »
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?

c++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
SurviBee is offline Offline
16 posts
since Sep 2007
Dec 11th, 2008
0

Re: Trapezoidal Trap

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.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Dec 12th, 2008
0

Re: Trapezoidal Trap

-
Last edited by dougy83; Dec 12th, 2008 at 8:41 am. Reason: wrote some rubbish
Reputation Points: 85
Solved Threads: 45
Posting Whiz in Training
dougy83 is offline Offline
275 posts
since Jun 2007
Dec 12th, 2008
0

Re: Trapezoidal Trap

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.
Reputation Points: 732
Solved Threads: 134
Practically a Master Poster
StuXYZ is offline Offline
659 posts
since Nov 2008
Dec 14th, 2008
0

Re: Trapezoidal Trap

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
SurviBee is offline Offline
16 posts
since Sep 2007
Nov 25th, 2009
0

c++

example of the area of trapezoid in a c++ program....plssssssssssssssss........
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sweetkim_2008 is offline Offline
1 posts
since Nov 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: File Reading problem
Next Thread in C++ Forum Timeline: Trouble with fstream, not able to write properly to a file to exact position as speci





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC