I am solving this in Ruby programming language. However, this question is far more math-related than programming related.
I am creating an equation given depth of liquid, diameter of tank, and max tank volume using this equation
def tankvol(height,diameter,vt)
r = diameter/2
d = height
volume = (vt/Math::PI/r**2)*(r**2)*(Math.acos((r-d)/r))-((r-d)*Math.sqrt((2*r*d)-(d**2)))
volume.floor
end
tankvol(50,100,4000)
=> 2000
tankvol(100,100,4000)
=> 4000
tankvol(0,100,4000)
=> 0
tankvol(75,100,4000)
=> 5082
tankvol(90,100,4000)
=> 5200
Note that when it is halfway full, as expected, volume is 2000. When it is filled up (100 depth and 100 diameter) volume is 4000; it shows 0 when it is empty. However when I have inbetween values the calculated volume exceeded max volume (depth 75 gives vol of 5082 > 4000), and so on.
I created another function to figure out acos' behavior:
def acos(d,r)
r = r/2
Math.acos((r-d)/r)
end
acos(50,100)
=> 1.5707963267948966
acos(100,100)
=> 3.141592653589793
acos(0,100)
=> 0.0
acos(75,100)
=> 3.141592653589793
It returns pi (3.14...) when I use 75 (last output). I have tested it using just the function and Ruby's acos function works just fine (it returns values less than one). I assume the fault is not with Ruby's math function but with cosine-inverse behavior that I probably am not aware of.
Math.acos(1)
=> 0.0
Math.acos(0.75)
=> 0.7227342478134157
Why is acos above works but my acos method and the equation didn't work? Is there a special property that I need to be aware of with cosine inverse?
Thanks!
I don't know the Ruby language but it seems you are only multiplying the first part of the volume equation by the length of the cylinder. Presumably an extra pair of parentheses (encompassing everything after the length) would do the trick.