---
name: gauss-legendre-4
description: "Follow a conservative system with fourth-order fixed-step accuracy while preserving its canonical Hamiltonian geometry, including when position and momentum are coupled. Two-stage Gauss–Legendre collocation requires a coupled implicit solve and preserves quadratic invariants with exact solves."
---

# Fourth-order Gauss–Legendre collocation

## 1. What this solves

Large phase errors can obscure a long conservative trajectory. Two-stage Gauss–Legendre uses two slopes inside each step and gives fourth-order endpoint accuracy. With accurately solved stages it is reversible and symplectic on canonical Hamiltonian systems, including nonseparable ones. It preserves quadratic first integrals in exact arithmetic.

## 2. When to use

Use a fixed step for a smooth canonical ODE with coupled position and momentum where long-run geometry and phase accuracy matter. Supply $f(t,y)$ and a Jacobian or another reliable stage solver. Strongly oscillatory modes can be linearly stable with a large step while their phase still needs resolution.

## 3. When not to use (and what to use instead)

| Situation | Use instead |
|---|---|
| Second-order accuracy suffices for a canonical problem | `implicit-midpoint` solves only one stage. |
| Smooth separable mechanics with cheap force evaluations | `stormer-verlet`, or `yoshida4` when fourth order is useful. |
| Fast decay should vanish over a large step | `bdf2` for an accurate fixed grid or `radau-iia-5` for adaptive steps. |
| Exact position and velocity constraints | `rattle` when its mechanical assumptions apply. |
| General nonstiff adaptive integration | `dormand-prince-rk45`. |

## 4. Algorithm

Let $d=\sqrt3/6$. Use $c=(1/2-d,1/2+d)$, $b=(1/2,1/2)$ and

```
A = [[1/4,     1/4 - d],
     [1/4 + d, 1/4    ]]
```

Find both stage states $Y_i=y+h\sum_j A_{ij} f(t+c_jh,Y_j)$ together. Start with both stages equal to $y$. Form the stage residuals $r_i=Y_i-y-h\sum_j A_{ij} f(t+c_jh,Y_j)$ and the Newton blocks $\delta_{ij}I-h A_{ij}J_f(t+c_jh,Y_j)$. Solve for the corrections and subtract them from the stage states. Evaluate the slopes at the converged stages and return

$$y_1=y+\tfrac h2\big[f(t+c_1h,Y_1)+f(t+c_2h,Y_2)\big].$$

The weights satisfy $b_i A_{ij}+b_j A_{ji}=b_i b_j$. This yields symplecticity in canonical coordinates and exact preservation of quadratic invariants in exact arithmetic. The two nodes and weights give a symmetric map, so reversing the sign of $h$ reverses the step on the same root branch. For $z=h\lambda$ the scalar stability factor is $(1+z/2+z^2/12)/(1-z/2+z^2/12)$. It has modulus at most one in the left half-plane, exactly one on the imaginary axis, and tends to one in the stiff limit.

## 5. Parameters

| Parameter | How to choose it |
|---|---|
| Signed fixed step $h$ | Choose it for phase and trajectory accuracy. Refine on a fixed time interval and check the observed order. A zero step returns the input. |
| Newton controls | The reference allows 12 updates and bounds each stage correction component by `max(tol * scale, 4 * ulp(scale))`. For each component, `scale` is its largest absolute value in the step input, the old stage, and the updated stage. Here `ulp` is floating-point spacing and the default is `tol=2e-14`. Compare invariants after tightening the solve. A singular matrix, nonfinite values or an exhausted limit is a failed step. |

## 6. Applying it to your code

Implement the two stages with the row and column ordering shown above. Evaluate Jacobians at each stage time and state. For larger systems reuse a suitable nonlinear and sparse linear solver. Use a prescribed fixed step and the locally continuous root when reversible geometry matters. Add the code comment `gauss-legendre-4 v0.1.0, Butcher (1964)`.

## 7. Verification

Export `step(fun, jac, t, y, h) -> y_next` in `port_adapter.py`, with `jac` rows indexed by output component. Run `python <skill>/checks/check_port.py` from the adapter directory and show the output. Other languages can use `checks/cases.json`. The checker tests order, signed-step reversal, and two quadratic invariants of nonlinear rigid-body rotation.

On your model compare fixed-time errors with `implicit-midpoint` on the same grids. Track early and late peak energy errors on a long well-resolved run. Nonquadratic energy is generally approximate even with exact stage solves.

## 8. Failure signatures

| Symptom | Likely cause |
|---|---|
| Order drops to two or errors depend strongly on forcing time | Check the off-diagonal tableau signs, both stage times and the final weights. |
| Quadratic invariants or reversibility drift | Verify that both coupled stages converged, the root branch is consistent, and the force corresponds to your equations. |
| Rapid decay persists | The A-stable formula passes far-left modes with a factor near one. Use an L-stable solver for decay. |
| Stable trajectory has the wrong period | Reduce the step to resolve the oscillation. |

## 9. Files and commands

`reference/gauss_legendre_4.py` implements the fixed step. `uv run examples/canonical.py` compares with an exact solution obtained using shifted momentum. `uv run verify/check_cases.py` checks the port including mixed-scale stages, long-time energy, comparison with midpoint, oscillatory stability, and three broken ports. `uv run checks/make_cases.py` prints the fixture JSON.

## 10. Sources

- Butcher analyzes implicit Runge–Kutta formulas based on Gaussian quadrature and notes that the two-stage case had been proposed by Hammer and Hollingsworth (1955). [Butcher (1964)](https://doi.org/10.1090/S0025-5718-1964-0159424-9)
- Hairer, Lubich and Wanner prove that the Gauss methods conserve quadratic invariants (Thm. IV.2.1), are symmetric (Thm. V.2.1) and are symplectic (Thm. VI.4.2). [Geometric Numerical Integration (2006)](https://doi.org/10.1007/3-540-30666-8)
