Equation to accelerate an object along a known curve until it reaches a target distance

118 Views Asked by At

This problem is deceptively simple, but it's been driving me mad:

I have a function, $f(x)$. At $f(0)$, it returns $0$ -- at $f(1)$, it returns $MaxSpeed$.

I have an object at rest and a target point 1 KM away. My goal is to move the object along a single dimension such that its speed always matches the output of that function. For example, when it's halfway there I'd expect $Speed = f(0.5)$ and when it hits the 1 KM goal, it would have $Speed = f(1) = MaxSpeed$.


My problem: $f(0)$ (initial spot for the object) gives a speed of $0$... so the object never moves. Giving the feedback loop a kick using something like Speed = f(0.001) does eventually do the proper thing, but it takes ages.

My gut says that I need to bring time into the problem somehow... but I'm fairly certain I can choose any 2 of "reasonable time," "reaches speed exactly at given distance," or "acceleration is determined by a function" and not all 3. Any ideas?

1

There are 1 best solutions below

0
On BEST ANSWER

The problem here is that you're using a first-order ordinary differential equation (ODE) to describe a process that is fundamentally second-order in nature. (See: Newton's Second Law.) Instead of a function that gives speed/velocity, you need one that gives acceleration.

Let $x(t)$ represent position as a function of time $t$, and let $v(t)$ be velocity as a function of time. You're going to have a pair of coupled ODEs like this: \begin{align*} x'(t) &\;=\; v(t)\\[0.1in] v'(t) &\;=\; a\bigl(x(t), v(t)\bigr) \end{align*} But what is the acceleration function $a(x,v)$? That's the tricky part. It's by no means trivial to allow an arbitrary function that does what you need it to. So I'm going to give you one special form for $a(x,v)$ that will work.

Say we start at initial position $x(0) = x_i$ with initial velocity $v(0) = v_i$. These are the initial conditions for your ODEs. Our goal is to arrive at a final position $x_f$ with some final velocity $v_f$. We don't care when we get there, we just want to have the specified velocity at that location. The rule $a(x,v)$ will follow is that given the current position $x$ and current velocity $v$, it will return that acceleration which, if held constant from that moment on, will deliver you to $x_f$ with velocity $v_f$. An introductory physics text will tell you that the function which accomplishes this is: $$ a(x,v) \;=\; \frac{{v_f}^2 - v^2}{2(x_f - x)} $$ This will - it turns out - give you a rather boring solution: The acceleration will remain constant, at its initial value, for the whole journey.

If you want a broader set of possible acceleration functions $a(x,v)$, you're going to have to apply some extra conditions. These may require more of the actual physics of whatever your object is (I think you said a train) in order to be consistent, so this may be a little bit more involved than it seemed at first.

ETA: If you're willing to accept some "dither" in the final speed, you can incorporate the force F(t) (the "thrust" of the engine) as a separate variable (along with some friction) with a rule whereby the force tries to adjust itself to change the speed of the train toward what it thinks the speed should currently be. This is more complicated, but gives cool results when I try it out in Mathematica. The following works very nicely, for instance: \begin{align*} x'(t) &\;=\; v(t)\\[0.1in] v'(t) &\;=\; -\gamma\, v(t) \;+\; F(t)\\[0.1in] F'(t) &\;=\; \beta\bigl(v_f \,-\, v(t)\bigr) \end{align*} Here $\gamma\geq 0$ is a friction/drag coefficient, and $\beta > 0$ is a sort of "train operator reaction time," i.e. it governs how fast the engineer realizes "Oh, I'm going faster/slower than I should be" and adjusts the engine thrust to compensate.