# softmatter:Euler Algorithm

The Euler algorithm is the simplest of all numerical integration techniques. It uses only the positions and velocities at time t to find those at time t + Δt

## General Algorithm

Expand the position and velocity of a particle at time t in a Taylor series, truncating terms of Δt3and higher in $\mathbf{r}(t)$ and Δt2in $\mathbf{v}(t)$:

$\mathbf{r}(t + \Delta t) = \mathbf{r}(t) + \mathbf{v}(t) \Delta t + \frac{\mathbf{a}(t)}{2} (\Delta t)^2 \qquad \qquad \qquad \qquad \qquad \qquad \qquad \qquad \qquad \qquad \qquad [1]$ $\mathbf{v}(t + \Delta t) = \mathbf{v}(t) + \mathbf{a}(t) \Delta t \qquad \qquad \qquad \qquad \qquad \qquad \qquad \qquad \qquad \qquad \quad \! \qquad \qquad \qquad [2]$

where $\mathbf{r}(t)$, $\mathbf{a}(t)$and $\mathbf{v}(t)$ are the position, velocity, and acceleration of the particle and Δt is the time step of integration.

Truncation error in the velocity is $\mathbf{O}(\Delta t^2)$per time step. Over an interval of length l the truncation error is proportional to lΔt2 times $\frac{l}{\Delta t}$steps. This results in an overall truncation error of $\mathbf{O}(\Delta t)$ in the velocity. This error compounds in the calculation of the position, making matters worse.

### Example Source Code

void Integrate()
{
// Initialize positions and velocities
Initialize();

for (int timestep = _begin; timestep <= _end; timestep++)
{
//compute forces from particle positions
ComputeForce();
for (int i = 0; i < Nparticles; i++)
{
v[i] = v[i] + (f[i] / mass[i]) * delta_t;
x[i] = x[i] + v[i] * delta_t;
}

}
}