c++matharduinoarduino-esp32cylindrical

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


I have been trying to use code to calculate the volume of the liquid in a horizontal cylindrical tank. I will use a esp32 micro contoller to take sensor data and calculate the volume of water in my tank.

All measurement 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 a online calculator the volume getting from this formula and the one from online calculator differs (all measurement 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