Numerical integration calculates the approximation of the definite integral of a function numerically. I start with Euler integration series, including explicit, implicit and semi-implicit, and then introduce position and velocity Verlet. Using an initial state and its rate of change, it can also reconstruct a trajectory. The interactive demo compares several numerical integration method in trajectory reconstruction. Moreover, using an undamped harmonic oscillator, I analyze the stability of each method.

Canvas 2D is required for this interactive demo. Enter Fullscreen Leave Fullscreen

Draw a path on the canvas and release the pointer to replay the reconstructed trajectory. Each method starts with the same position and velocity and uses the same time-dependent acceleration from a reference trajectory. Use the colored method toggles to choose which paths to compare. The time step is fixed for Euler, Verlet, and RK4, and is the maximum step for RK45. All calculated step points are connected with straight lines. This post covers the Euler and Verlet methods. The Runge–Kutta series will be introduced in the next post.

Problem Definition

Numerical integration can be used to calculate a trajectory from an initial state and its rate of change. In this post, I explain Euler and Verlet methods for solving the following problem:

\[\dot{\mathbf z}=\frac{d\mathbf z}{dt}=f(t,\mathbf z),\qquad \mathbf z(t_0)=\mathbf z_0.\]

Let the time step be \(h\) and \(t_n=t_0+nh\). We want to calculate the next state, \(\mathbf z_{n+1}\), from the current state, \(\mathbf z_n\). Integrating the above equation gives

\[\mathbf z(t_n+h)=\mathbf z(t_n)+\int_{t_n}^{t_n+h}f(t,\mathbf z(t))\,dt.\]

Each method approximates the rate of change over this interval differently.

To describe the motion in the demo above, let the state vector contain position and velocity:

\[\begin{align} \mathbf z&=\begin{pmatrix}\mathbf x\\\mathbf v\end{pmatrix},\\ f(t,\mathbf z)&=\begin{pmatrix}\mathbf v\\\mathbf a(t,\mathbf x,\mathbf v)\end{pmatrix}. \end{align}\]

In two dimensions, the state contains \(x,y,v_x,v_y\). The corresponding acceleration is analytically computed from the reference trajectory.

Explicit Euler Integration

First, assume that the current rate of change is constant during one step. Then, the next state is calculated as

\[\mathbf z_{n+1}=\mathbf z_n+h f(t_n,\mathbf z_n).\]

This equation comes from the first two terms of the Taylor expansion:

\[\mathbf z(t+h)=\mathbf z(t)+h\dot{\mathbf z}(t)+O(h^2).\]

Thus, the local truncation error is \(O(h^2)\). Under the usual smoothness and stability conditions, the accumulated error over a time interval of length \(T\) is \(T/h \times O(h^2) = O(h)\).

For position and velocity, the update becomes

\[\begin{align} \mathbf x_{n+1}&=\mathbf x_n+h\mathbf v_n,\\ \mathbf v_{n+1}&=\mathbf v_n+h\mathbf a(t_n,\mathbf x_n,\mathbf v_n). \end{align}\]

Here, both position and velocity are calculated from the current state. Thus, the next state can be computed explicitly. Explicit Euler is inexpensive, but small step size is needed for stability as well as accuracy. However, in the undamped harmonic oscillator below, it amplifies errors for every positive step size. A Refer to the example in the last section.

Implicit Euler Integration

Explicit Euler uses the current rate of change to update the state. However, it can produce the divergence of oscillations when the time step is not small enough. We can reduce the time step to avoid this instability, but in a stiff system, the required step may be much smaller than we need to follow the overall motion.

To resolve this instability problem, let’s use the rate of change at the next state instead. Then, the next state is updated with its own rate of change. Thus, the implicit equation is given:

\[\mathbf z_{n+1}=\mathbf z_n+h f(t_n+h,\mathbf z_{n+1}).\]

Since the next state is also included on the right-hand side, we define a residual and solve for the state that makes the residual zero:

\[R(\mathbf u)=\mathbf u-\mathbf z_n-h f(t_n+h,\mathbf u)=0.\]

For a linear equation \(f(t,\mathbf z)=A\mathbf z+\mathbf b(t)\), this becomes a linear system and the next state can be solved simply:

\[\mathbf z_{n+1}=(I-hA)^{-1}\bigl(\mathbf z_n+h\mathbf b(t_n+h)\bigr).\]

For a nonlinear equation, we can use Newton’s method and solve the next state iteratively:

\[\begin{align} \Delta\mathbf u&=-\bigl(I-hJ_f(t_n+h,\mathbf u^{(k)})\bigr)^{-1}R(\mathbf u^{(k)}),\\ \mathbf u^{(k+1)}&=\mathbf u^{(k)}+\Delta\mathbf u. \end{align}\]

When \(\mathbf u^{(k)}\) converges, it becomes \(\mathbf z_{n+1}\). \(J_f\) is the Jacobian with respect to the state.

For position and velocity, the update becomes

\[\begin{align} \mathbf v_{n+1}&=\mathbf v_n+h\mathbf a(t_n+h,\mathbf x_{n+1},\mathbf v_{n+1}),\\ \mathbf x_{n+1}&=\mathbf x_n+h\mathbf v_{n+1}. \end{align}\]

Unlike explicit Euler, both position and velocity are calculated from the next state. Implicit Euler is relatively expensive, but it is unconditionally stable (also known as A-stable) in linear systems. Refer to the example in the last section.

Dahlquist’s Test Problem

Since Implicit Euler is also a first-order method, its local truncation error is \(O(h^2)\) and its global error is \(O(h)\), just like explicit Euler. However, its advantage is stability, rather than accuracy. Using Dahlquist’s test equation, \(\dot{z}=\lambda z\), the amplification factors are

\[\begin{align} \text{Explicit Euler: }&1+h\lambda,\\ \text{Implicit Euler: }&\frac{1}{1-h\lambda}. \end{align}\]

For a real \(\lambda<0\), explicit Euler needs \(\lvert 1+h\lambda\rvert<1\) to guarantee stability. On the other hand, the amplification factor of implicit Euler is less than one for every \(h>0\). This is useful when a stiff system contains rapidly decaying components with a large magnitude of \(\lambda\). However, implicit Euler can introduce numerical damping in the tested system because its amplification factor is always less than one for a negative \(\lambda\). For an undamped oscillator, \(\lambda=\pm i\omega\), so the magnitude is \(1/\sqrt{1+(h\omega)^2}\). This also introduces numerical damping into an oscillation whose exact amplitude is constant.

Semi-implicit Euler Integration

Explicit Euler and implicit Euler are both first-order numerical integration methods, but they exhibit fundamentally different stability. As we mentioned above, explicit Euler tends to amplify error in oscillatory systems, causing numerical instability. Conversely, implicit Euler is unconditionally stable, but numerical damping appears even when the system is conservative. These limitations motivate an alternative approach, semi-implicit Euler.

Instead of updating all state variables using only the current (explicit Euler) or next state (implicit Euler), semi-implicit Euler updates one variable first and uses the newly-computed value to update the other (depending on the structure of the equation).

For position and velocity, we update velocity first using the current acceleration, then calculate position using the updated velocity:

\[\begin{align} \mathbf v_{n+1}&=\mathbf v_n+h\mathbf a(t_n,\mathbf x_n,\mathbf v_n),\\ \mathbf x_{n+1}&=\mathbf x_n+h\mathbf v_{n+1}. \end{align}\]

The method remains first order, with a typical global error of \(O(h)\). For a separable Hamiltonian system with the usual position-dependent forces, \(\mathbf a(t_n,\mathbf x_n)\), this velocity-first update is also known as symplectic Euler.

For oscillatory systems, it can preserve long-term energy behavior better than explicit and implicit Euler methods. Please notice that symplectic does not mean that energy is preserved exactly or that any step size is safe. As shown in the figure below, the exact energy is the total energy of a simple oscillator, and normalized energy is the ratio of the numerical energy to the exact energy. The energy error of semi-implicit Euler method is bounded around the true energy and shows slight fluctuations. Details can be found in the Section Change of Energy.

Verlet Integration

Verlet starts from a second-order equation of motion, \(\ddot{\mathbf x}=\mathbf a\). Expand position forward and backward around the current time:

\[\begin{align} \mathbf x(t+h)&=\mathbf x(t)+h\mathbf v(t)+\frac{h^2}{2}\mathbf a(t)+\frac{h^3}{6}\mathbf x^{(3)}(t)+O(h^4),\\ \mathbf x(t-h)&=\mathbf x(t)-h\mathbf v(t)+\frac{h^2}{2}\mathbf a(t)-\frac{h^3}{6}\mathbf x^{(3)}(t)+O(h^4). \end{align}\]

Adding the two equations eliminates the odd-order terms. Rearranging the result gives position Verlet:

\[\mathbf x_{n+1}=2\mathbf x_n-\mathbf x_{n-1}+h^2\mathbf a_n.\]

Position Verlet stores the current and previous positions. Velocity need not be stored separately, but its information is still present in the differences between positions. Note that position Verlet assumes a constant step size. Since position Verlet is a second-order method, it has a global error of \(O(h^2)\), while its local truncation error is \(O(h^4)\) due to the ellimination of \(\mathbf x^{(3)}(t)\) terms above.

For initialization, we need two positions: \({\mathbf x}_0\) is the position at the initial time, and \({\mathbf x}_1\) is computed by second-order Taylor expansion using \(\mathbf v_0\) and \(\mathbf a_0\):

\[\mathbf x_1 = \mathbf x_0 + h \mathbf v_0 + \frac{1}{2} h^2 \mathbf a_0.\]

After initialization, subsequent positions can be computed using the position Verlet formula.

Velocity Verlet

Velocity Verlet explicitly computes both position and velocity at the same time. When acceleration depends on time and position, but not velocity, its updates are executed sequentially:

\[\begin{align} \mathbf a_n&=\mathbf a(t_n,\mathbf x_n),\\ \mathbf x_{n+1}&=\mathbf x_n+h\mathbf v_n+\frac{h^2}{2}\mathbf a_n,\\ \mathbf a_{n+1}&=\mathbf a(t_n+h,\mathbf x_{n+1}),\\ \mathbf v_{n+1}&=\mathbf v_n+\frac h2(\mathbf a_n+\mathbf a_{n+1}). \end{align}\]

The endpoint acceleration is also the starting acceleration of the next step. The formula above assumes velocity-independent acceleration. Otherwise, velocity-dependent acceleration such as drag force, leads to an implicit equation. Velocity Verlet has local truncation error of \(O(h^3)\), and global error of \(O(h^2)\) for sufficiently smooth problems.

Under constant acceleration, both position Verlet and velocity Verlet reproduce the exact solution, since they are second-order methods. Also, for suitable conservative systems and a fixed step size, Verlet methods have a better long-term energy behavior than Euler methods. Again, this does not mean that they preserve energy exactly. They are explicit methods and still have stability limits on the time step.

Position Verlet eliminates velocity from the integration process by using positions at consecutive time steps. Velocity Verlet, on the other hand, explicitly updates both position and velocity. Despite their different formulations, the two methods produce identical position trajectories under velocity-independent acceleration, because velocity Verlet can be algebraically reduced to Position Verlet.

Deriving Position Verlet from Velocity Verlet

Position Verlet and velocity Verlet use different state representations, but they produce identical position updates when the time step is constant and acceleration is independent of velocity. To verify this, we can derive position Verlet by eliminating the velocity term from the velocity Verlet update equations.

The position and velocity updates of velocity Verlet are given by

\[\mathbf x_{n+1}=\mathbf x_n+h \mathbf v_n+\frac{h^2}{2} \mathbf a_n, \tag{1}\label{eq-verlet-position}\] \[\mathbf v_{n+1}=\mathbf v_n+\frac{h}{2}(\mathbf a_n + \mathbf a_{n+1}), \tag{2}\label{eq-verlet-velocity}\]

where \(h\) is the fixed time step and \(\mathbf a_n=\mathbf a(t_n, \mathbf x_n)\). Shifting the indices of Equation \eqref{eq-verlet-position} backward by one-step gives

\[\mathbf x_n=\mathbf x_{n-1}+h \mathbf v_{n-1}+\frac{h^2}{2} \mathbf a_{n-1}. \tag{3}\label{eq-verlet-position-previous}\]

Similarly, Equation \eqref{eq-verlet-velocity} can be rewritten as

\[\mathbf v_{n-1}=\mathbf v_n-\frac{h}{2}(\mathbf a_{n-1}+\mathbf a_n). \tag{4}\label{eq-verlet-velocity-previous}\]

Substituting Equation \eqref{eq-verlet-velocity-previous} into Equation \eqref{eq-verlet-position-previous}, we obtain

\[\begin{align} \mathbf x_n &=\mathbf x_{n-1}+h\left(\mathbf v_n-\frac{h}{2}(\mathbf a_{n-1}+\mathbf a_n)\right) +\frac{h^2}{2} \mathbf a_{n-1}\\ &=\mathbf x_{n-1}+h \mathbf v_n-\frac{h^2}{2} \mathbf a_n. \end{align}\]

Now, subtracting this result from Equation \eqref{eq-verlet-position} eliminates the velocity term:

\[\mathbf x_{n+1}-\mathbf x_n=\mathbf x_n-\mathbf x_{n-1}+h^2 \mathbf a_n.\]

Rearranging the equation yields

\[\mathbf x_{n+1}=2 \mathbf x_n- \mathbf x_{n-1}+h^2 \mathbf a_n,\]

which is the formula of position Verlet integration.

When acceleration also depends on velocity, position Verlet cannot compute the next position using position values only, requiring velocity information to compute acceleration. Meanwhile, for velocity Verlet, its velocity update equation becomes implicit.

Example: An Undamped Harmonic Oscillator

To illustrate the stability and energy behavior, let’s compare the methods using an undamped harmonic oscillator with natural angular frequency \(\omega\):

\[\ddot{x}=-\omega^2x.\]

Setting \(x(0)=1\) and \(v(0)=0\), the exact solution is

\[x(t)=\cos(\omega t).\]

Its amplitude remains constant, and its period is \(2\pi/\omega\). Unlike the demo at the top where acceleration depends only on time, acceleration in the demo below is evaluated from position: \(a=-\omega^2x\). The exact solution is used only as a reference in the graph.

Canvas 2D is required for this graph. Enter Fullscreen Leave Fullscreen

The dashed line is the exact solution. The demo compares explicit Euler, implicit Euler, semi-implicit Euler, and velocity Verlet. The control panel also offers RK4 and RK45, for your information, which are covered in the next post. The time-step slider changes the fixed step for the Euler and Verlet methods. The \(\omega\) slider adjusts the angular frequency. You can drag horizontally to move the time window, scroll to zoom the position axis, use ctrl+scroll to zoom the time axis, and double-click the graph to restore the initial view.

Change of Energy

In order to analyze the energy behavior of numerical integration methods, we compute the total energy and its change directly from the update equations. Assume a constant step size \(h>0\) and natural angular frequency \(\omega>0\). Then, the energy per unit mass is

\[E_n=\frac12\left(v_n^2+\omega^2x_n^2\right).\]

For the exact solution, this energy is constant because

\[\frac{dE}{dt}=v\dot v+\omega^2x\dot x =v(-\omega^2x)+\omega^2xv=0.\]

To simplify the calculation, let’s define

\[\begin{align} q_n&=\omega x_n, \\ p_n&=v_n, \\ s&=h\omega. \end{align}\]

For your information, the symbols \(q\) and \(p\) represent the generalized coordinate and its conjugate momentum in Hamiltonian mechanics, respectively. Then, the energy becomes

\[E_n=\frac12\left(q_n^2+p_n^2\right).\]

The following equations describe this linear oscillator with a fixed step, rather than arbitrary nonlinear systems.

Explicit Euler

The explicit Euler update equations become

\[\begin{align} q_{n+1}&=q_n+sp_n,\\ p_{n+1}&=p_n-sq_n. \end{align}\]

Substituting these into the energy yields

\[\begin{align} 2E_{n+1} &=(q_n+sp_n)^2+(p_n-sq_n)^2\\ &=(1+s^2)(q_n^2+p_n^2). \end{align}\]

Therefore,

\[E_{n+1}=(1+s^2)E_n,\]

and

\[E_n=(1+s^2)^nE_0.\]

For any \(s>0\) and nonzero initial state, the energy grows without bound as \(n\) increases. Reducing \(h\) can slow this growth, but no positive constant step makes explicit Euler energy stable for this undamped oscillator.

Implicit Euler

Implicit Euler uses the next position and velocity:

\[\begin{align} q_{n+1}&=q_n+sp_{n+1},\\ p_{n+1}&=p_n-sq_{n+1}. \end{align}\]

Solving the two equations gives

\[\begin{align} q_{n+1}&=\frac{q_n+sp_n}{1+s^2},\\ p_{n+1}&=\frac{p_n-sq_n}{1+s^2}. \end{align}\]

The numerators are the same as in explicit Euler, but both components are divided by \(1+s^2\). Thus,

\[E_{n+1}=\frac{E_n}{1+s^2},\]

and

\[E_n=(1+s^2)^{-n}E_0.\]

For every \(s>0\), the energy approaches zero as \(n\) increases. The method stays stable, but introduces unintended numerical damping into a system.

Semi-implicit Euler

For the velocity-first semi-implicit Euler method, its update equations become

\[\begin{align} p_{n+1}&=p_n-sq_n,\\ q_{n+1}&=q_n+sp_{n+1}=(1-s^2)q_n+sp_n. \end{align}\]

Then, the energy is

\[\begin{align} 2E_{n+1}&=\bigl((1-s^2)q_n+sp_n\bigr)^2 +(p_n-sq_n)^2 \\ &=(1-s^2+s^4)q_n^2 + (1+s^2)p_n^2 - 2s^3q_n p_n\\ &=2E_n + (-s^2+s^4)q_n^2 + s^2p_n^2 - 2s^3q_n p_n \end{align}\]

As you can see, semi-implicit Euler does not preserve the original energy exactly. Instead, we consider the following modified energy:

\[\widetilde E_n :=\frac12\left(q_n^2+p_n^2-sq_np_n\right).\]

This is an auxiliary quantity used to analyze the method, not a new definition of the physical energy. Then, substituting the two updates gives

\[\begin{align} 2\widetilde E_{n+1} &=\bigl((1-s^2)q_n+sp_n\bigr)^2 +(p_n-sq_n)^2\\ &\quad-s\bigl((1-s^2)q_n+sp_n\bigr)(p_n-sq_n)\\ &=q_n^2+p_n^2-sq_np_n. \end{align}\]

Therefore,

\[\widetilde E_{n+1}=\widetilde E_n=\widetilde E_0.\]

To connect this conserved quantity to the original energy, use the inequality

\[|q_np_n|\leq\frac12(q_n^2+p_n^2)=E_n.\]

It follows that

\[\left(1-\frac{s}{2}\right)E_n \leq\widetilde E_n \leq\left(1+\frac{s}{2}\right)E_n.\]

When \(0<s<2\), both factors are positive, so

\[\frac{\widetilde E_0}{1+s/2} \leq E_n \leq\frac{\widetilde E_0}{1-s/2}.\]

These bounds do not depend on the number of steps. The energy of semi-implicit Euler can fluctuate, but cannot drift to infinity or decay to zero for a nonzero initial state. For the example’s initial velocity \(p_0=0\), we have \(\widetilde E_0=E_0\), giving

\[\frac{1}{1+s/2} \leq\frac{E_n}{E_0} \leq\frac{1}{1-s/2}.\]

For small \(s\), the relative energy deviation is bounded by a quantity of order \(s=h\omega\). The same bounds apply when \(q_0=0\) with a nonzero initial velocity.

Velocity Verlet

Applying velocity Verlet with \(s=h\omega\) to the same oscillator gives

\[\begin{align} q_{n+1}&=\left(1-\frac{s^2}{2}\right)q_n+sp_n,\\ p_{n+1}&=p_n-\frac{s}{2}(q_n+q_{n+1}), \end{align}\]

and, after substituting \(q_{n+1}\) into the velocity update equation, we obtain

\[p_{n+1} =-s\left(1-\frac{s^2}{4}\right)q_n +\left(1-\frac{s^2}{2}\right)p_n.\]

Let’s define

\[\begin{align} b&:=1-\frac{s^2}{4},\\ c&:=1-\frac{s^2}{2}. \end{align}\]

These coefficients satisfy

\[c^2+s^2b=1.\]

Now define another modified energy:

\[\widehat E_n:=\frac12\left(p_n^2+bq_n^2\right).\]

Substituting the updates and cancelling \(p_n q_n\) terms gives

\[\begin{align} 2\widehat E_{n+1} &=(-sbq_n+cp_n)^2+b(cq_n+sp_n)^2\\ &=(c^2+s^2b)(p_n^2+bq_n^2)\\ &=p_n^2+bq_n^2. \end{align}\]

Thus, we obtain

\[\widehat E_{n+1}=\widehat E_n=\widehat E_0.\]

Same as semi-implicit Euler, when \(0<s<2\), we have \(0<b<1\), so the following inequalities are satisfied:

\[bE_n = \frac{1}{2}\left(bp_n^2+bq_n^2\right) \leq\widehat E_n\leq E_n.\]

Consequently,

\[\widehat E_0\leq E_n\leq\frac{\widehat E_0}{b}.\]

For the initial velocity \(p_0=0\), we have \(\widehat E_0=bE_0\). Therefore,

\[1-\frac{s^2}{4}=b \leq\frac{E_n}{E_0} \leq1.\]

The energy stays bounded, with an order, \(s^2=(h\omega)^2\). This explains why velocity Verlet has a smaller energy fluctuation than semi-implicit Euler for a sufficiently small step. For this particular initial state, its energy fluctuates below the exact value, rather than symmetrically around it. In the figure below, since their energies are computed through a simulation set to \(s=h\omega=0.1\), so the amplitude of energy fluctuations is approximately 0.5 (semi-implicit Euler) and 0.00125 (velocity Verlet).

On the other hand, for the initial position \(q_0=0\), we have \(\widehat{E}_0=E_0\). Therefore,

\[1 \leq \frac{E_n}{E_0} \leq \frac1b = \frac{1}{1-s^2/4}\]

Unlike the case of \(p_0=0\), the energy stays at or above the exact value.

Wrap-up

Method Global error over a fixed interval Update
Explicit Euler \(O(h)\) Calculate every update from the old state
Implicit Euler \(O(h)\) Solve an equation containing the next state
Semi-implicit Euler \(O(h)\) Update velocity first, then position with the new velocity
Position Verlet \(O(h^2)\) Update position from two consecutive positions and acceleration
Velocity Verlet \(O(h^2)\) Update position, then use the new acceleration to update velocity

Euler methods have similar formula, but small modifications produce a different numerical behavior. Explicit Euler is inexpensive, but can amplify oscillations. Implicit Euler can suppress unstable growth, but solving an equation can be expensive and it introduces numerical damping. Semi-implicit Euler improves long-term behavior of energy while keeping the first-order accuracy.

Verlet uses the second-order equation of motion directly. Position Verlet stores consecutive positions, while velocity Verlet keeps position and velocity together. With a constant step and velocity-independent acceleration, the two position updates are equivalent. They provide second-order accuracy and better long-term energy behavior.

While these methods offer different trade-offs between simplicity, stability, and accuracy, they show the limitations of low-order approximations. In the next post, I will explore the Runge–Kutta methods, which improve accuracy by evaluating multiple intermediate slopes within each time step.