2
$\begingroup$

I have parameters $T$ for tension, $b$ for bounciness and $P_t$ for target value that should be approached as t goes to infinity.

Currently I have written an equation like so:

$\ddot{f}(t)=\frac{T(P_t-f(t))-\ddot{f}(t)}{b}$

Which is taken from code that I have written. However, while the code works, this equation does not get me a function that is similar to the results shown by my code. Where have I gone wrong into translating the python code into a mathematical function of time?

This is my code:

g_velocity = 0.0

# note: fr = from, from is just a taken keyword in python

def lerp(fr, to, amount):
    return fr + (amount * (to - fr))

def spring_interp(fr, to, bounciness, tension):
    global g_velocity
    g_velocity = lerp(g_velocity, (to - fr) * tension, 1.0 / bounciness)
    return fr + g_velocity

# usage (example values):
# the value will then oscillate, dampened over time and approach the target_value

value = 1.0
target_value = 8.0
while True:
    value = spring_interp(value, target_value, 8.0, 0.4)
    print(value)

Edit:
Yes, the correct function I am trying to solve would be
$\ddot{f}(t)=\frac{T(P_t-f(t))-\dot{f}(t)}{b}$

New contributor
Krys is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
$\endgroup$

1 Answer 1

4
$\begingroup$

If I understand correctly, you are trying to solve the initial value problem $$ \ddot{f}(t) = \frac{T (P_t - f(t))}{1 + b}\\ f(0) = v_0\\ \dot{f}(0) = g_0 $$ Note that I re-arranged your equation by collecting all the $\ddot{f}(t)$ terms to the left-hand side. This has the solution $$ f(t) = P_t + (v_0-P_t) \cos\left(t \sqrt{\frac{T}{\sqrt{1+b}}}\right) + \frac{g_0}{T} \sqrt{(b+1) T} \sin\left(t \sqrt{\frac{T}{1+b}}\right) $$ This solution does not damp such that $\lim_{t\rightarrow \infty} f(t) = P_t$, but instead oscillates indefinitely.

I suspect the ODE problem you might have been expecting to solve is $$ \ddot{f}(t) = \frac{T(P_t - f(t)) - \dot{f}(t)}{b}\\ f(0) = v_0\\ \dot{f}(0) = g_0 $$ This system does have the property that $\lim_{t\rightarrow \infty} f(t) = P_t$

Here's a numerical solution for this second system I obtained using scipy's solve_ivp: enter image description here

And here's the first 100 iterations of your code:

enter image description here

I think you're numerical implementation might be a forward Euler method with $\Delta t = 1$?

$\endgroup$

Your Answer

Krys is a new contributor. Be nice, and check out our Code of Conduct.

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.