c++matharduinoarduino-esp32cylindrical

How do I calculate the volume of a horizontal cylindrical tank in c++?


I have been trying to use a code to calculate the volume of the liquid in a horizontal cylindrical tank.

I will use an esp32 micro controller to take sensor data and calculate the volume of water in my tank.

All measurements in centimeters.

This is what I have been trying from other formulas from the internet, but keep getting way out calculations.

double radius=100;
double height=50; // the depth of the water in the tank this will be my sensor reading
double length=450;
double volume;
volume = (3.14*radius*radius)-((radius*radius)*acos((radius-height)/radius))+((radius-height)*
           (sqrt(2*radius*height-(height*height))));`

When using an online calculator, the volume generated from this formula and the one from online calculator differ (all measurements in CM).


Solution

  • enter image description here

    Half cross-sectional area occupied by water = area of segment – area of triangle

    So,

    enter image description here

    volume = cross-sectional area x length

    So,

    enter image description here

    where

    enter image description here

    #include <iostream>
    #include <cmath>
    using namespace std;
    
    double partFilledCylinderVolume( double r, double h, double L )
    {
       double theta = acos( 1 - h / r );
       return L * r * r * ( theta - cos( theta ) * sin( theta ) );
    }
    
    int main()
    {
       double radius = 100;
       double height = 50;
       double length = 450;
    
       cout << partFilledCylinderVolume( radius, height, length ) << " cubic centimetres" << '\n';
    }
    

    Output:

    2.76383e+06 cubic centimetres