How do I create an equation that decelerates past a certain value?

142 Views Asked by At

Apologies for my lack of pure maths, I am a programmer!

I currently have an equation in code that states that if a number goes below a certain value (in my case, 0.7) then the difference is dampened:

y = x < 0.7 ? 0.7 - (0.7 - x) / 4.0 : x;

The problem with this is that any value less than 0.7 is suddenly dampened, whereas what I really want is a deceleration curve so that anything less than 0.7 slows gradually, to the point that it doesn't get any lower than, say, 0.5.

I'm guessing I need to do something exponential or logarithmic, I just can't work out what or how!

1

There are 1 best solutions below

3
On BEST ANSWER

I think this is what you're looking for:

$$f(x)=\left\{\begin{array}{cc} 0.5+\frac{0.4}{1+e^{(0.7-x)/0.1}} & \mbox{if} & x<0.7 \\ x & \mbox{otherwise}\end{array}\right.$$

This is how the function looks like:

enter image description here

More generally, if you want your $0.5$ to be any $\alpha<0.7$, then your function would be:

$$f(x)=\left\{\begin{array}{cc} \alpha+\frac{1.4-2\alpha}{1+e^{\dfrac{(0.7-x)}{(0.35-\alpha/2)}}} & \mbox{if} & x<0.7 \\ x & \mbox{otherwise}\end{array}\right.$$

Also if you even want your $0.7$ to be any say $\beta$, you should have:

$$f(x)=\left\{\begin{array}{cc} \alpha+\frac{2\beta-2\alpha}{1+e^{\dfrac{2(x-\beta)}{(\alpha-\beta)}}} & \mbox{if} & x<\beta \\ x & \mbox{otherwise}\end{array}\right.$$

The code would be:

y = (x < 0.7) ? a + (1.4 - 2*a) / (1 + exp((0.7 - x)/(0.35 - a / 2))) : x;

Depending on the language, you might need to use a library for the $exp$ function (like "Math.exp" for Java for example).

Hint:

What you were basically looking for is a function $f$ such that:

  • $f$ has the right "look" (as you thought, the exponential was a good choice)
  • $\displaystyle\lim_{x\rightarrow -\infty}=\alpha$
  • $f(0.7)=0.7$
  • $f'(0.7)=1$

The last two conditions are here to make sure the two functions $f$ and $x\mapsto x$ fit at point $x=0.7$