0

I'm looking for an algorithm which will allow me to calculate outputs for any given input instead of just the few ones I'm given in advance.

The input is a number from interval $0 - 100$ representing a percentage. I'm given a few of the desired outputs in advance; they correspond to inputs evenly distributed along the whole $0 - 100$ range. For example, if I am given three outputs, A, B and C, they correspond to $100$, $50$ and $0$ respectively. If, instead, I am given five of them, they should correspond to inputs of $100$, $75$, $50$, $25$ and $0$. In between these given values, the output should increase or decrease proportionally. For example, in the first example, the output corresponding to 75 should be right in the middle between A and B (since $75$ is right in the middle between $100$ and $50$ and output $A$ corresponds to input $100$ and output $B$ to input $50$) and the output for $25$ should be right in the middle between $B$ and $C$.

In summary: I'm given an array of values (the "given" outputs) consisting of at least two values. I'm also given an input value in the range $0 - 100$. The algorithm should output a single value corresponding to this input; which satisfies the description above.

Since my background in maths is very limited, I would prefer it if you could explain the mathematical symbols or name the variables something reminiscent of their function.

  • 1
    I'm not sure I understand the first example given. If $75%$ is right in the middle between $100%$ and $50%$. Why should it correspond to $0$ instead of $0.5$ (which is right in the middle between $1$ and $0$; the numbers corresponding to $100%$ and $50%$ respectively)? – Peter Košinár Aug 25 '13 at 17:54
  • If you have two values, they go at $100%$ and $0%$. If you have three values, they go at $100%$, $50%$, and $0%$. If you have four values, they should go at $100%$, $66.7%$, $33.3%$, and $0%$, not at $100%$, $75%$, $50%$, and $25%$, shouldn't they? –  Aug 25 '13 at 22:03
  • WOW. What an improvement. Re-open vote well-deserved. – rurouniwallace Aug 28 '13 at 00:54

1 Answers1

1

Some of your examples don't make sense assuming my interpretation of the problem.

In general, you have a sequence of $n \geq 2$ real numbers $(q_0, q_1,...,\, q_{n-1})$ and your interpolation parameter $t \in [0,1]$. You're only interested in the 2 values whose indices bookend $tn - t$. The interpolated value is: $$ (1 - \alpha) \,q_{\lfloor mt\rfloor}+\alpha \,q_{\lceil mt\rceil} $$ where $m = n - 1$ and $\alpha = mt - \lfloor mt \rfloor$.

Looking at your 2nd example, you have $q = (0.5, 2, 1, 0, 1)$ with $n=5$ and $m=4$. Note that I've flipped your order as traditionally you progress left to right. At $t=0.125$ we have $mt = 4 \times 0.125 = 0.5$ and:

$$ \begin{align} \alpha &= 0.5 - \lfloor 0.5 \rfloor \\ &= 0.5 - 0 \\ &= 0.5 \end{align} $$ which gives us $$ (1-0.5) q_0 + 0.5q_1 = 1.25 $$ Which obviously doesn't equal 1, although it does yield 0 when $t=0.75 $.

EDIT: The question has changed since I provided this answer.

  • Could you please use a less math-heavy definition of what is going on? I do not understand what you are saying at all. – kettlecrab Aug 25 '13 at 21:50
  • You have a 2-step interpolation process. First, you have to find the 2 numeric values in your sequence to interpolate. So what we want is a way to take a percentage and see what index in the sequence it would be. If that index exists, then your value is just the value at that index. If it doesn't, then we find a value between the 2 that surround it.

    There's nothing complicated, I show you the way to calculate it in the first equation of my post. Just take it one step at a time and feel free to ask for help when you're stuck at something specific.

    – Drew Cummins Aug 25 '13 at 23:59