# Solving an optimization problem involving a differential equation?

I am trying to solve what I think is a single variable calculus optimization problem. The actual problem I’m trying to solve is rather hard to describe, but I think it’s isomorphic to this one.

Suppose there are two linked containers, container A and container B. Rain is falling on container A for the next hour, at the rate of $$1$$ liter per hour. (Rain is not falling on container B.) And water flows from container A to container B at a rate of $$r(t) = \frac{q(t)}{1-t}$$, where $$q(t)$$ is the amount of water currently in container A. Now at any time in the next hour, you can dump out all the water currently in container A (after which rain will continue to fall on container A). But you can only do it once. So what is the best time to do it if you want to minimize the amount of water that ends up in container B at the end of the hour?

I think the basic situation is described by the differential equation $$\frac{dq}{dt} = 1 - r = 1 - \frac{q(t)}{1-t}$$ where $$q(0)=0$$, whose solution according to Wolfram Alpha is given by $$q(t)=(t-1)ln(1-t)$$. And if the water in container A is dumped at time $$T$$, then I think the amount of water in container B at the end of the hour is given by $$P(T)= \int_0^T r(t) dt + 1 - q(T) = T - 2q(T) +2$$. And we want to find the time $$T$$ between $$0$$ and $$1$$ which minimizes $$P(T)$$.

Yet there must be an error somewhere, because if I plug in $$q(t)=(t-1)ln(1-t)$$ into the expression for $$P(T)$$, then for any value of $$T$$ between $$0$$ and $$1$$, it looks like $$P(T)>1$$. But that makes no sense, because if only $$1$$ liter of water falls on container A over the course of the hour, there’s no way that there will be more than a liter of water in container B at the end of the hour. So where am I going wrong?

• Is it true that $A$ has no rain at first? – Parcly Taxel Mar 15 at 3:10
• @ParclyTaxel Yes, it’s initially empty. – Keshav Srinivasan Mar 15 at 3:10

The amount of water in $$B$$ after time $$t$$ without any dumps of $$A$$ is given by another differential equation: $$\frac{dB}{dt}=\frac A{1-t}=-\ln(1-t)$$ which can be easily solved to yield $$B=t+(1-t)\ln(1-t)$$ Since we dump $$A$$ once, we are looking for some optimal $$t$$ such that the following expression is at a minimum: $$B(t)+B(1-t)=t+(1-t)\ln(1-t)+(1-t)+t\ln t=1+(1-t)\ln(1-t)+t\ln t$$ Its derivative is $$(1-t)\cdot\frac{-1}{1-t}+(-1)\ln(1-t)+t\cdot\frac1t+\ln t=\ln t-\ln(1-t)=\ln\frac t{1-t}$$ and this is zero when $$\frac t{1-t}=1\implies t=1-t\implies t=\frac12$$ So $$A$$ should be dumped at the halfway mark.
• @KeshavSrinivasan We can treat this as two separate processes. We let the rain move for a time $t$, so $B$ accumulates $B(t)$ rain, then we dump $A$. This restarts the process from zero and from there $B$ accumulates $B(1-t)$ rain. Thus the total amount of rain in $B$ after one hour is $B(t)+B(1-t)$, where we choose $t$. – Parcly Taxel Mar 15 at 3:37