0
$\begingroup$

I have two values x and y that represent two time values in seconds. Normally both values will be exactly the same, but in some cases y can deviate and be smaller or larger depending on the situation. Both values are decremented each iteration by the same amount until they reach zero. I need to change this iteration so that it decreases both values in such a way that they both reach zero at the same time.

The caveat here is that I can't control the initial decrement of x. Every iteration x and y will be decremented by the same interval, and then I need to calculate some additional value to add or subtract from x to make sure it reaches zero at the same time as y. Additionally, I have no way of knowing how many iterations it will take to reach zero.

So for example, say we have these values:

x = 20, y = 13, i = ?

i is the interval that is decremented each iteration. It is a very small number that can fluctuate in value every iteration depending on various factors. Because of this, it is impossible to predict how many iterations it will take to reach zero. Each iteration this would occur first:

x -= i
y -= i

At this point I need to calculate some new value, d, that can be positive or negative, and add it to x to move it slightly closer to the value of y. I know part of the calculation requires taking the diff of x and y, but I'm not sure what to do with it from there.

d = ?
x += d

And then eventually, after repeating this for however many iterations it takes, they would both end up at zero at the same time.

$\endgroup$

1 Answer 1

1
$\begingroup$

Suppose that you had full control of the rate at which $x$ is decremented, and that $i$ only affected $y$. Then if you were to decrement $x$ by the same proportion as $y$ at each iteration, it would ensure that they reach $0$ at the same time. For example:

$$\begin{array}{c|c|c}x&y&i\\\hline\ 100 & 10 & \\ 90 & 9 & 1 \\ 70 & 7 & 2 \\ 60 & 6 & 1\\ 30 & 3 & 3\\ 20 & 2 & 1\\ 0 & 0 & 2 \end{array}$$

So in this situation you would subtract $\frac{x}{y}\cdot i$ from $x$ in each iteration. If you do this consistently, you will only need to calculate $\frac{x}{y}$ at the beginning (it won't change), and you will be able to avoid the possibility of dividing by zero.

Now since in reality, $x$ is automatically decremented by $i$ as well, you just need to correct for that. You would like $$x\mapsto x-\frac{x}{y}\cdot i=x-i+i-\frac{x}{y}\cdot i=x-i+\left(1-\frac{x}{y}\right) i,$$ which means that you can use a correction factor of $d=\left(1-\frac{x}{y}\right) i$. Then the previous example becomes:

$$\begin{array}{c|c|c|c}x&y&i&d&x+d\\\hline\ 100 & 10 & \\ 99 & 9 & 1 & -9&90\\ 88 & 7 & 2 & -18&70\\ 69 & 6 & 1&-9 &60\\ 57 & 3 & 3&-27 & 30\\ 29 & 2 & 1&-9&20\\ 18 & 0 & 2&-18&0 \end{array}$$

$\endgroup$
3
  • $\begingroup$ This works great, thanks a lot. I have another similar problem where x and y are decremented by two different values at the start. Is there a way to adapt this to deal with that? So for example, in the first step instead of just subtracting i from both x and y. x is subtracted by i, and y is subtracted by i` (eye-prime), which can sometimes be zero. p.s. sorry for not using the same notation as you. I don't know how to show all of those characters and notations. $\endgroup$
    – Justin G
    Mar 3, 2015 at 20:29
  • $\begingroup$ @Shenjoku: In that case, I think you would want $x\mapsto x-i+\left(i-\frac{x}{y}\cdot i'\right)$, so $d$ is the expression in parentheses, $d=i-\frac{x}{y}\cdot i'$. In that case though, $i'$ does all the work and $i$ winds up having no real effect on the system anymore. I'm not sure whether that is important for your situation or not. $\endgroup$
    – AMPerrine
    Mar 3, 2015 at 21:31
  • $\begingroup$ Perfect! Just what I needed. Thanks a lot for the help :) $\endgroup$
    – Justin G
    Mar 4, 2015 at 3:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.