6

Suppose I have the current position, velocity and acceleration of a particle. As I see it, the best estimate of its velocity and position at a time $\Delta t$ from now should be $$\vec{v}(t+\Delta t) = \vec{v}(t) + \vec{a}\Delta t$$ $$\vec{s}(t+\Delta t) = \vec{s}(t) + \vec{v}(t)\Delta t + \frac{1}{2} \vec{a} \Delta t^2$$ However, by doing some simulations, I've noticed that an extremely more accurate method would be to consider $$\vec{v}(t+\Delta t) = \vec{v}(t) + \vec{a}\Delta t$$ $$\vec{s}(t+\Delta t) = \vec{s}(t) + \vec{v}(t)\Delta t + \vec{a} \Delta t^2$$ Which is almost the same, except by the missing $1/2$ factor. The position equation is also equivalent to $$\vec{s}(t+\Delta t) = \vec{s}(t) + \vec{v}(t+\Delta t)\Delta t$$ To show how better it is, here is the absolute deviation to the analytical solution with both methods for a simple harmonic oscillator

enter image description here

So the error or deviation of the second method stays almost the same while the first one increases much faster, more specifically it seems the error of the first method increases quadratically and the second linearly. I've also got this result by simulating a gyroscope and a wave propagating through a string.

What explains this counterintuitive result?

Edit

To add more details to the question, here is the implementation for the oscillator example:

  • The position and velocity are initially equal to 2 constants x0 and v0 defined in the code
  • The acceleration is given by $-\omega^2x$, according to the harmonic oscillator differential equation, where $x$ is the current position and $\omega$ a constant that is related to the mass and elastic constant.
  • Each iteration of method 1 is given by
dv = acc(x)*dt
x += dt*(v + 1/2*dv)
v += dv
  • Each iteration of method 2 is given by
v += acc(x)*dt
x += v*dt
WordP
  • 467
  • We can't really comment without knowing your simulation parameters. – davidlowryduda Nov 11 '23 at 15:58
  • @davidlowryduda My simulation is just to exemplify my point. The result I showed appears for every initial condition I've tested for many different kinds of simulations I've coded. – WordP Nov 11 '23 at 16:05
  • 1
    One would need to see the actual code, one common occurrence is that accidentally the symplectic or semi-implicit Euler method was implemented. – Lutz Lehmann Nov 11 '23 at 16:28
  • What $\vec {a}$ means in this algorithm? If it is acceleration, then it depends on s and v. Could you show this function? – Alex Trounev Nov 11 '23 at 16:48
  • Yes, as predicted method 2 is the symplectic Euler method, which with a slight change in initial conditions is the second order leapfrog Verlet method. – Lutz Lehmann Nov 12 '23 at 17:50
  • I tried implementing your two methods for harmonic motion, and I do find the second method's maximum error after multiple cycles is less, but it still grows with each cycle, unlike your graph. – David K Nov 12 '23 at 19:19
  • For what it's worth, I tried implementing the Verlet method, and got the same results as your method 2 within some reasonable multiple of machine precision. That is, they were practically identical. – David K Nov 12 '23 at 21:11
  • Related: https://scicomp.stackexchange.com/a/10330/1030 – John Alexiou Nov 16 '23 at 14:35
  • Also this paper might contain your answer. – John Alexiou Nov 16 '23 at 14:38

2 Answers2

1

In order to model the motion and acceleration of a particle you have to use following equations

$$ \begin{align} \text{(i)} \quad \frac{dx}{dt} &= f(t,v),\\ \text{(ii)} \quad \frac{dv}{dt} &= g(t,x) \stackrel{const}{\equiv} a. \end{align} $$

A correct symplectic Euler discretization would result in

$$ \begin{align} \text{(i)} \quad x_{n+1} &= x_n + \Delta t \cdot f(t_n, v_n) ,\\[0.3ex] \text{(ii)} \quad v_{n+1} &= v_n + \Delta t \cdot g(t_n, x_{n+1}) , \end{align} $$

or

$$ \begin{align} \text{(ii)} \quad v_{n+1} &= v_n + \Delta t \cdot g(t_n, x_n) ,\\[0.3em] \text{(i)} \quad x_{n+1} &= x_n + \Delta t \cdot f(t_n, v_{n+1}). \end{align} $$

which is similar to your second method. An alternative would be a Verlet integration method. Your first discretization seems to be wrong or is at least a mix of different discretization methods, as mentioned in the comments.

For more informations read:

Edit: (see comments)

Starting form (ii) we can write

$$ \begin{align} \frac{dv}{dt} & = a,\\[0.5em] dv & = a\cdot dt ,\\[0.5em] \int_{v_0}^v dv & =\int_{t_0}^t a \cdot dt , \\[0.5em] \left( v- v_0 \right) & =a \left(t - t_0 \right) , \\[0.5em] (1) \quad \rightarrow \quad v &= a \left(t - t_0 \right) + v_0 , \quad | \quad \text{insert (i)} \\[0.5em] \frac{dx}{dt} &= a \left(t - t_0 \right) + v_0 , \\[0.5em] dx &= \left[ a \left(t - t_0 \right) + v_0\right]dt , \\[0.5em] \int_{x_0}^x dx & = \int_{t_0}^t \left[ a \left(t - t_0 \right)+ v_0\right]dt , \\[0.5em] x -x_0 & = \left[\frac{1}{2} a \left(t - t_0 \right)^2 - a\cdot t_0\left(t - t_0 \right)+ v_0\left(t - t_0 \right) \right] , \\[0.5em] (2) \quad \rightarrow \quad x & = \frac{1}{2} a \left(t - t_0 \right)^2 - a\cdot t_0\left(t - t_0 \right)+ v_0\left(t - t_0 \right) + x_0. \\[0.5em] \end{align} $$

Using your notation

$$ \begin{align} (1) \quad v (t+\Delta t) &= v(t) + a \Delta t \\[0.5em] (2) \quad x (t+\Delta t) & =x(t) + \frac{1}{2} a \Delta t^2 - a \cdot t \Delta t + v(t)\Delta t . \end{align} $$

For $~t_0=0~$ we get

$$ \begin{align} v &= a t + v_0 , \\[0.5em] x & = \frac{1}{2} a t^2 + v_0 t+x_0 .\Box \end{align} $$

This also holds for $t \rightarrow t + \Delta t$.

ConvexHull
  • 504
  • 3
  • 10
  • That does seems like my second method. I've added more details that explain how it was implemented, if you could take a look. But I guess the most important question would be why the first method is "wrong", because it is simply the equations of motion for constant acceleration. – WordP Nov 11 '23 at 17:14
  • Of course, it is identical to your second method. – ConvexHull Nov 11 '23 at 17:17
  • Moreover, your first equation system is definetly wrong, see https://en.wikipedia.org/wiki/Newton%27s_laws_of_motion. – ConvexHull Nov 11 '23 at 17:31
  • I don't see how it is wrong, and the article you linked is Newton's second law, not the time-dependent equations of motion for constant acceleration. See https://en.wikipedia.org/wiki/Acceleration#Uniform_acceleration – WordP Nov 11 '23 at 17:36
  • Your are missing the fact that you should write $ \mathbf{s}(t) = \mathbf{s}_0 + \mathbf{v}_0 t + \frac{1}{2} \mathbf{a}(t)t^2 $, where $s_0$ and $v_0$ are initial values and not time dependent. Moreover, this equation is valid for constant acceleration without further equations for $t_0=0$. You can derive this special equation from (i) and (ii). – ConvexHull Nov 11 '23 at 17:42
  • My equations are the same as yours but shifted in time; there is no difference. In fact, for constant acceleration, the second method yields worse results. This is expected because the first method is precisely what describes uniformly accelerated motion. – WordP Nov 11 '23 at 17:47
  • Please see my edit. – ConvexHull Nov 11 '23 at 18:05
  • 1
    Please make the substitution $t \rightarrow t+\Delta t$ and you will verify that the equations are the same. – WordP Nov 11 '23 at 18:13
  • Sure. I think you either start with (i), (ii) and apply a symplectic integrator. Or you use the (correct) acceration equations, see the second last equation in my edit. It seems you are missing $a \cdot t_0 (t-t_0)$. – ConvexHull Nov 11 '23 at 19:25
  • I'm afraid that term doesn't remain in the equation after simplifying the substitution. – WordP Nov 11 '23 at 20:47
  • I am open for discussion, so please prove your claim. Note that discretizating (i), (ii) is the usual way for particle simulations. There is plenty literature available in the world-wide-web. – ConvexHull Nov 11 '23 at 20:59
  • Method 1 is not wrong, it is just an unusual mix of improved Euler or explicit midpoint method in the position and simple explicit Euler in the velocity. It would be better to apply a second order method consistently in both components. – Lutz Lehmann Nov 12 '23 at 17:55
  • @LutzLehmann D'accord. – ConvexHull Nov 12 '23 at 18:58
  • @LutzLehmann Could you elaborate? This seems like a potential answer to my question.

    ConvexHull, I sent a comment in the discussion page https://chat.stackexchange.com/rooms/149656/discussion-between-wordp-and-convexhull

    – WordP Nov 13 '23 at 01:56
1

There is a variant to implement the velocity Verlet method that looks quite similar to method one.

The "half-step" variant of its implementation can look like

x(t+dt/2) = x(t) + v(t)*dt/2
a(t+dt/2) = accel(x(t+dt/2))
v(t+dt/2) = v(t) + a(t+dt/2)*dt/2

v(t+dt) = v(t+dt/2) + a(t+dt/2)dt/2 x(t+dt) = x(t+dt/2) + v(t+dt)dt/2 = x(t) + v(t)dt/2 + (v(t)+a(t+dt/2)dt)dt/2 = x(t) + v(t)dt + a(t+dt/2)*dt^2/2

One can now eliminate the middle step as far as possible to get

a = accel(x+0.5*v*dt)
x = x + v*dt + a*dt^2/2
v = v + a*dt

Note that the acceleration is still evaluated at the halfway point of the step. This variant is now second order and should be more precise than the method 2, especially when the energy errors are compared.

Lutz Lehmann
  • 126,666