# Getting a real number from a complex number

I'm attempting to program a formula to say how full an horizontal cylinder is with liquid. Here is the formula I am using with variables from measurements I took:

When I use Wolframalpha to solve this I end up getting a complex number, and quite frankly I'm not quite sure what to do with it(link here). I need a real number in cubic inches or gallons to obtain my end goals.

Can someone help me to understand why I a getting a complex number and how I can possibly get a real number? A different formula maybe? This website does the calculations I want, but I have no idea how it does it.

-
If you are computing $\cos^{-1} x$ and $x$ is not between $-1$ and $1$, you are going to get a complex value. The problem is that there is something wrong with your formula. –  Thomas Andrews Jul 12 '12 at 15:52
A similar problem came up in another post a day or so ago. I'd guess that W|A gave you an answer with a very small imaginary term. If that's the case, just ignore the imaginary part as an artifact of what's going on under the W|A hood, so to speak. –  Rick Decker Jul 12 '12 at 15:59
If you look at the Alpha result, the argument of $\cos^{-1}$ is $\frac{1-384\sqrt 2}3$, far outside $[-1,1]$. –  Ross Millikan Jul 12 '12 at 16:12
A good thought, Rick (and my first thought, as well). It turns out in this case that OP's formula is incorrect. –  Cameron Buie Jul 12 '12 at 16:28
@Cameron. You're right--there's a misplaced parenthesis. –  Rick Decker Jul 12 '12 at 17:10

Obviously, the volume is length times cross-sectional area, so we need only determine what that area will be.

For $h<r$ (as in the particular example), you're looking at the area of a circular sector with angle $\theta\in(0,\pi)$ such that $\cos\frac{\theta}{2}=\frac{r-h}{r}$--so given the sign, we have $$\sqrt{\frac{1+\cos\theta}{2}}=\frac{r-h}{r}$$ as the determining equation--less the area of the triangle formed by 2 radii and the chord on that circular sector. The area of the triangle will be $$\frac{1}{2}r^2\sin\theta,$$ and the area of the sector will be $$\frac{1}{2}r^2\theta,$$ so we need only determine $\theta$ and $\sin\theta$ in terms of $r$ and $h$.

$1+\cos\theta=\frac{2(r-h)^2}{r^2}$, so $\cos\theta=\frac{2r^2-4rh+2h^2}{r^2}-1=\frac{r^2-4rh+2h^2}{r^2}$, and so $$\theta=\arccos\left(\frac{r^2-4rh+2h^2}{r^2}\right).$$ Using Pythagorean identity and the fact that $\sin\theta$ is positive for $\theta\in(0,\pi)$, we find also that $\sin\theta=\sqrt{1-\cos^2\theta}$, which through simplification gives us $$\sin\theta=\frac{2\sqrt{2rh-h^2}(r-h)}{r^2}.$$

Thus, our volume will be $$\frac{1}{2}Lr^2\arccos\left(\frac{r^2-4rh+2h^2}{r^2}\right)-L\sqrt{2rh-h^2}(r-h).$$

If you want to extend your answer to the other cases, then obviously, when $r=h$, we have $\frac{1}{2}L\pi r^2$ as the volume. When $r<h\leq 2r$, we will take the whole volume of the tube and subtract a similar volume as we had in the first case, with the one exception being that we'll swap $h$ and $r$ in one term, so that the volume will be $$L\pi r^2-\frac{1}{2}Lr^2\arccos\left(\frac{r^2-4rh+2h^2}{r^2}\right)+L\sqrt{2rh-h^2}(h-r).$$

-

As commented by Thomas and Ross, the values you're putting into the inverse cosine function are not in the interval $[-1,1]$, so you're going to get complex answers out of your formula.

This is one way that I would go about deriving the corrected formula:

Consider a cylindrical water tank (tipped on its side) with length $L$, radius $r$, filled with water to a height $h$, as pictured in the link you provided.

To find the volume of the water, we will want to integrate the area of horizontal cross sections as we go along a vertical axis. For simplicity, let's set up a vertical $y$-axis with 0 located at the center of the circle at the end of the tank. Then the horizontal cross sections of the tank look like rectangles with length $L$, and a width which we can call $w$. You can (kind of) see what this looks like in this poorly drawn diagram:

Now, using the Pythagorean Theorem, we find that at a certain height $y$ we have width $w = 2\sqrt{r^2-y^2}$, and so the cross-sectional area of the tank at height $y$ is given by $A = 2L\sqrt{r^2-y^2}$. Now if we integrate this with respect to $y$ on the interval $[-r,h]$, we should get the volume up to height $h$. We get the integral $$V = \int_{-r}^h 2L\sqrt{r^2-y^2} dy .$$ We can evaluate this by using the trig substitution $y = r\sin \theta$. Making this substitution, get \begin{align*} V &= \int_{-\pi/2}^{\sin^{-1}(h/r)} 2L r\cos \theta \cdot r \cos \theta d\theta \\ &= 2L r^2 \int_{-\pi/2}^{\sin^{-1}(h/r)} \cos^2 \theta d\theta \\ &= 2L r^2 \int_{-\pi/2}^{\sin^{-1}(h/r)} \left( \frac 12 + \frac 12 \cos(2 \theta) \right) d\theta \\ &= 2L r^2 \left( \frac \theta 2 + \frac 14 \sin 2\theta \right) \bigg|_{-\pi/2}^{\sin^{-1}(h/r)} \\ &= L r^2 (\theta + \sin \theta \cos \theta ) \bigg|_{-\pi/2}^{\sin^{-1}(h/r)} \\ &= L r^2 \left( \sin^{-1} \left( \frac hr \right) + \frac \pi 2 - \frac hr \sqrt{r^2-h^2} \right) \\ &= L r^2 \sin^{-1} \left( \frac hr \right) + \frac \pi 2 L r^2 - Lrh \sqrt{r^2-h^2} . \end{align*}

-