Variational integrators are numerical integrators for Hamiltonian systems derived from the Euler–Lagrange equations of a discretized Hamilton's principle. Variational integrators are momentum-preserving and symplectic.
Consider a mechanical system with a single particle degree of freedom described by the Lagrangian
                    L        (        t        ,        q        ,        v        )        =                              1            2                          m                  v                      2                          −        V        (        q        )        ,                where                     m                 is the mass of the particle, and                     V                 is a potential. To construct a variational integrator for this system, we begin by forming the discrete Lagrangian. The discrete Lagrangian approximates the action for the system over a short time interval:
                                                                                          L                                      d                                                  (                                  t                                      0                                                  ,                                  t                                      1                                                  ,                                  q                                      0                                                  ,                                  q                                      1                                                  )                                                            =                                                                                                    t                                                  1                                                                    −                                              t                                                  0                                                                                      2                                                                    [                  L                                      (                                          t                                              0                                                              ,                                          q                                              0                                                              ,                                                                                                                        q                                                          1                                                                                −                                                      q                                                          0                                                                                                                                                            t                                                          1                                                                                −                                                      t                                                          0                                                                                                                                            )                                    +                  L                                      (                                          t                                              1                                                              ,                                          q                                              1                                                              ,                                                                                                                        q                                                          1                                                                                −                                                      q                                                          0                                                                                                                                                            t                                                          1                                                                                −                                                      t                                                          0                                                                                                                                            )                                    ]                                                                                                                  ≈                                  ∫                                                            t                                              0                                                                                                                        t                                              1                                                                                                            d                t                                L                (                t                ,                q                (                t                )                ,                v                (                t                )                )                .                                                            Here we have chosen to approximate the time integral using the trapezoid method, and we use a linear approximation to the trajectory,
                    q        (        t        )        ≈                                                            q                                  1                                            −                              q                                  0                                                                                    t                                  1                                            −                              t                                  0                                                                    (        t        −                  t                      0                          )        +                  q                      0                                  between                               t                      0                                   and                               t                      1                                  , resulting in a constant velocity                     v        ≈                  (                      q                          1                                −                      q                          0                                )                          /                          (                      t                          1                                −                      t                          0                                )                        . Different choices for the approximation to the trajectory and the time integral give different variational integrators. The order of accuracy of the integrator is controlled by the accuracy of our approximation to the action; since
                              L                      d                          (                  t                      0                          ,                  t                      1                          ,                  q                      0                          ,                  q                      1                          )        =                  ∫                                    t                              0                                                                        t                              1                                                            d        t                L        (        t        ,        q        (        t        )        ,        v        (        t        )        )        +                              O                          (                  t                      1                          −                  t                      0                                    )                      2                          ,                our integrator will be second-order accurate.
Evolution equations for the discrete system can be derived from a stationary-action principle. The discrete action over an extended time interval is a sum of discrete Lagrangians over many sub-intervals:
                              S                      d                          =                  L                      d                          (                  t                      0                          ,                  t                      1                          ,                  q                      0                          ,                  q                      1                          )        +                  L                      d                          (                  t                      1                          ,                  t                      2                          ,                  q                      1                          ,                  q                      2                          )        +        ⋯        .                The principle of stationary action states that the action is stationary with respect to variations of coordinates that leave the endpoints of the trajectory fixed. So, varying the coordinate                               q                      1                                  , we have
                                                        ∂                              S                                  d                                                                    ∂                              q                                  1                                                                    =        0        =                              ∂                          ∂                              q                                  1                                                                              L                      d                                    (                      t                          0                                ,                      t                          1                                ,                      q                          0                                ,                      q                          1                                )                +                              ∂                          ∂                              q                                  1                                                                              L                      d                                    (                      t                          1                                ,                      t                          2                                ,                      q                          1                                ,                      q                          2                                )                .                Given an initial condition                     (                  q                      0                          ,                  q                      1                          )                , and a sequence of times                     (                  t                      0                          ,                  t                      1                          ,                  t                      2                          )                 this provides a relation that can be solved for                               q                      2                                  . The solution is
                              q                      2                          =                  q                      1                          +                                                            t                                  2                                            −                              t                                  1                                                                                    t                                  1                                            −                              t                                  0                                                                    (                  q                      1                          −                  q                      0                          )        −                                            (                              t                                  2                                            −                              t                                  0                                            )              (                              t                                  2                                            −                              t                                  1                                            )                                      2              m                                                            d                          d                              q                                  1                                                                    V        (                  q                      1                          )        .                We can write this in a simpler form if we define the discrete momenta,
                              p                      0                          ≡        −                              ∂                          ∂                              q                                  0                                                                              L                      d                          (                  t                      0                          ,                  t                      1                          ,                  q                      0                          ,                  q                      1                          )                and
                              p                      1                          ≡                              ∂                          ∂                              q                                  1                                                                              L                      d                          (                  t                      0                          ,                  t                      1                          ,                  q                      0                          ,                  q                      1                          )        .                Given an initial condition                     (                  q                      0                          ,                  p                      0                          )                , the stationary action condition is equivalent to solving the first of these equations for                               q                      1                                  , and then determining                               p                      1                                   using the second equation. This evolution scheme gives
                              q                      1                          =                  q                      0                          +                                                            t                                  1                                            −                              t                                  0                                                      m                                    p                      0                          −                                            (                              t                                  1                                            −                              t                                  0                                                            )                                  2                                                                    2              m                                                            d                          d                              q                                  0                                                                    V        (                  q                      0                          )                and
                              p                      1                          =        m                                                            q                                  1                                            −                              q                                  0                                                                                    t                                  1                                            −                              t                                  0                                                                    −                                                            t                                  1                                            −                              t                                  0                                                      2                                                d                          d                              q                                  1                                                                    V        (                  q                      1                          )        .                This is a leapfrog integration scheme for the system; two steps of this evolution are equivalent to the formula above for                               q                      2