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;
}