0

I have a kind of problem with a code that I'm writing, and can't come up with a way to solve it.

I have some values defining the speed that can go from $min$ to $max$. For example, the speed in this case can go from 0.2 to 400.

At the same time, I get input from the user which varies from $1$ to $100$.

What I'd like to do is connect these two data, in a way that given the input I can return the optimal speed. For example, is the input is the minimum, $1$, I want to return 0.2. If the input is $100$, I want to return $400$:

So, I need a mathematical formula to connect between input and correspondent speed. I don't know why but I tried a lot and this problem confuses me.

Thanks

ucei
  • 153
  • Why do you think that the question is about propositional logic ? – Mauro ALLEGRANZA Oct 02 '20 at 11:57
  • 1
    So what you want is a function $$ f(x) = kx+b $$ that has the values $f(1) = 0.1$ and $f(100)=400$. Can you figure out the coefficients from this? – Matti P. Oct 02 '20 at 11:57
  • @MattiP. Looking at the problem this way makes much more sense. I think I can solve the problem now. Thank you! – ucei Oct 02 '20 at 11:58
  • 1
    You can also see this similar question: https://math.stackexchange.com/questions/3355027/a-function-or-a-factor-to-scale-a-list-of-real-numbers-from-one-range-to-another/3355071#3355071 – Matti P. Oct 02 '20 at 12:00

2 Answers2

0

There are many such formulas which give you slightly different results. I would be free to propose another one:

$$f(x)=0.2\cdot8000^\frac{x-1}{99}$$

This formula may let people control low speeds at better precision than the linear formula proposed in the comments. Give it a try.

0

Let $min = m$ and $max = M$, also the range given by the user as $(u,U)$. Consider $y=kx+b$ with this values as a system of equations: $$u=km+b $$ $$U=kM+b $$ If you solve it is easy to see that: $$k=\frac{U-u}{M-m} \text{ ; } b=u-\frac{U-u}{M-m}m$$ So the function you need is: $$f(x)=\frac{U-u}{M-m}x+ \left(u-\frac{U-u}{M-m}m\right) $$

TeemoJg
  • 1,069