954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Trapezoidal Trap

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?

#include <cmath>
#include <iostream>
#include <iomanip>

using namespace std;

class Trap
{
 private:
 float sidea;//function of time
 float sideb;//function of time
 float width;//representation of time passed
 float area;//water discharged
 public:
 float Y(float);//calculates side lengths
 void Area(float);//durr
 void Width(float nwidth){width = nwidth;};//assings width for sep. runs
 float GetArea(){return area;};
};


int main()
{
 cout << "This program calculates the rate of flow from a 3107g tank\n";
 cout << "over 10000 seconds using the trapezoidal method.\n";
 cout << "The chart below displays the calculated total flow \n";
 cout << "based on 100, 1000, 10000, and 100000 trapezoids.\n\n";

 cout << " Trapezoids  Total Discharge(gallons) Tank Capacity(gallons)\n";

 Trap zoids[100000];
 float Traplimits[5] = {10, 100, 1000, 10000, 100000};

 int trapcount;
 float sum;

 //loops through the calculations with different numbers of trapezoids
 //being calculated for
 for(int i = 0; i < 5; i++)
   {
      trapcount = 0;
      sum = 0;

      //loops through array of trapezoids assigning new width
      //and then calculating area again
      for( int c = 0; c < Traplimits[i]; c++)
      {
           zoids[c].Width(10000/Traplimits[i]);
           zoids[c].Area(trapcount++);
           
      }

      //sums the areas of all calculated trapezoids in this run
      for( int p = 0; p < Traplimits[i]; p++)
           sum += zoids[p].GetArea();

      cout << fixed << showpoint << setprecision(2) << left;
      cout << setw(14) << Traplimits[i];
      cout << setw(27) << sum;
      cout << "3107\n";
      
   }

   cin.get();
   cin.get();
   return 0;

}

float Trap::Y(float time)
{
    return 200 * ( (2/(time + 1)) - exp(-0.693*(time + 1)) );
}

void Trap::Area(float tcount)
{
    // sidea  = Y(time passed until beginning of trapezoid
    sidea = Y(tcount++ * width);

    //sideb = Y(time passed up to end of trapezoid
    sideb = Y(tcount * width);
    
    //formula for area of a trapezoid
    area = 0.5 * (sidea + sideb) * width;
    cout << area << endl;
}
SurviBee
Newbie Poster
16 posts since Sep 2007
Reputation Points: 10
Solved Threads: 0
 

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.

Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
 
dougy83
Posting Whiz in Training
275 posts since Jun 2007
Reputation Points: 85
Solved Threads: 45
 

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

StuXYZ
Practically a Master Poster
680 posts since Nov 2008
Reputation Points: 760
Solved Threads: 138
 

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.

SurviBee
Newbie Poster
16 posts since Sep 2007
Reputation Points: 10
Solved Threads: 0
 

example of the area of trapezoid in a c++ program....plssssssssssssssss........

sweetkim_2008
Newbie Poster
1 post since Nov 2009
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You