# How long until my variable reaches zero?

I am trying to figure out how many seconds it takes for my value to reach zero.

x is a non-negative number.
a is a non-negative number.
b is a non-negative number.
bMax is a non-negative number.

Every second, x is reduced by a.

Every second, x is increased by b, up to the cumulative total of bMax.

So if x = 10, a = 2, b = 1, and bMax = 5:

After 0 Seconds, x == 10
(Starting Value)

After 1 Second, x == 9
(x = 10 - 2 + 1)

After 2 Seconds, x == 8
(x = 9 - 2 + 1)

After 3 Seconds, x == 7
(x = 8 - 2 + 1)

After 4 Seconds, x == 6
(x = 7 - 2 + 1)

After 5 Seconds, x == 5
(x = 6 - 2 + 1)
bMax was reached, so b will no longer be subtracted from x

After 6 Seconds, x == 3
(x = 5 - 2)

After 7 Seconds, x == 1
(x = 3 - 2)

After 7.5 Seconds, x == 0
(x = 3 - 2 * 0.5)

F(x, a, b, bMax) = Seconds
F(10, 2, 1, 5) = 7.5

I can figure out the formula if b == 0 or bMax == 0:

F(x, a, 0, 0) = x / a

But I cannot figure out how to include b in the equation.

If a == b && b <= bMax, then I believe the following formula works:

F(x, a, b, bMax) = x / (a - b)

But this does formula does not work otherwise.

Any ideas on how to create this formula?

You need to handle the two components separately. The state of your system is $$F(t) = \begin{cases} x + (b-a)t \qquad \quad 0\leq t < \frac{bMax}{b} \\ x - at + bMax \qquad t \geq \frac{bMax}{b} \end{cases}$$ So if it does not reach zero within the first interval, it will have a zero findable by the second equation.

So, either $\dfrac{x}{a-b} < \dfrac{bMax}{b}$, in which case $\dfrac{x}{a-b}$ is your time, or else it is $\dfrac{x+bMax}{a}$.

Hope this helps!

• I don't quite understand it (yet), but I am testing it out. Thanks! (I may come back with a follow up question if I still need help understanding it after playing with it) – Evorlor Jun 3 '17 at 17:24

The value is: $$y(t)=x-at+\text{min}\{bt,b_{max}\}$$

it will reach zero at: $$t=\text{max}\{{x \over a-b},{b_{max} \over a-b} +{x+b_{max} \over a}\}$$

• time is the unknown I am trying to solve for. Sorry, I am new to asking questions here. Can I rephrase the question somehow? – Evorlor Jun 3 '17 at 17:19
• Check it now... – Brethlosze Jun 3 '17 at 17:25
• Thanks! I am studying it now. – Evorlor Jun 3 '17 at 17:27