(I am using Python to demonstrate this question)
I have two functions, x and y. They should apply a damping factor of k to velocity v over the time t. The first function works fine, and you can see that I am using the power operator to achieve an accurate result. I am trying to rewrite the function to use acceleration instead, but the second function is not giving me the exact same results. Notably, the higher the k value, the less accurate the second function is.
import math
timestep = 0.01
def x(k, v, t):
for i in range(math.floor(t / timestep)):
v *= (1 - k) ** timestep
return v
def y(k, v, t):
for i in range(math.floor(t / timestep)):
a = - k * v
v += a * dt
return v
What changes can I make to the acceleration value a = - k * v to obtain the correct result? Or is it not possible to do this using acceleration? The idea is that a only depends on the variables k and v.
I understand that the function well never be 100% accurate because it is not continuous, but it should converge to the correct result as the timestep decreases, but this is not currently happening.
Note: This "answer" is really two different answers, one of which applies to a particular definition of "damping factor", while the other applies to a different definition. Choose which part applies according to what kind of "damping factor" you really want to be using.
If your objective is to compute the differential equation
$$ \frac{dv}{dt} = -kv, \tag1$$
then your first function most definitely does not "work fine." For $0 < k < 1$, after one unit of time the velocity returned by this function will be reduced by $kv$ units of speed, just as if the deceleration $-kv$ were applied uniformly over the entire first unit of time (instead of decaying as the velocity decays). So at the end of one unit of time you have a velocity of $(1 - k)v$.
After two units of time you will have $(1-k)^2 v$, after three units of time you will have $(1-k)^3 v$, and so forth; in fact the velocity is decaying exponentially, but it is not decaying with damping factor $k$ according to Equation $(1)$ above. Instead it is decaying with damping factor $(-\ln(1-k)).$
If you put $k = 1$ in your first function, the velocity goes instantly to zero at the first timestep. Is that correct behavior?
Try $k = 1.1.$ Running your first function in Python 3.8.3 with $k=1.1,$ $v = 100,$ and $t = 0.6$ I get
(-7.762155952763054+23.88945958880576j)as a result. What do you get? Is your result good?The exact solution for Equation $(1)$ is $v(t) = e^{-kt} v_0$ where $v_0$ is the initial velocity and $v(t)$ is the velocity after time $t.$ You could compute this result almost exactly in timesteps like so:
Your second function is reasonably close to this for values of $kt$ that are not too large; its final velocity is about $\frac12\%$ too small for $k = 1,$ $t =1$ and this error compounds as you increase $t$. It's an approximate solution using the Euler method, and its error will increase as you increase $k$, or the timestep, or the total time elapsed, but the error is not nearly as much as the error of your first method.
If your objective in applying a "damping factor $k$" is that after one unit of time the velocity should be $1-k$ times the initial velocity, then you can still approximate it via the Euler method as in your second function, but you need to use $\ln(1-k)$ instead of $(-k)$ as a factor.
That is, this definition of a damping factor will produce a velocity that is a solution to the differential equation
$$ \frac{dv}{dt} = v \ln(1-k), $$
assuming that everything is expressed in compatible units.
Because the the logarithm of a negative number is not real, this formula is valid only for $k < 1,$ but of course if you are using this definition of "damping factor" then you would naturally have the restriction that $k < 1$ anyway.
I observe that when you see "damping factor" or "damping coefficient" in the literature, the term is usually associated with damped harmonic motion, which does not seem to be the application here. So it's up to you to determine which definition is correct for the particular application you require.