---
name: implicit-trapezoidal
description: "Advance an ODE by averaging the slopes at the beginning and end of each fixed step. The implicit trapezoidal rule, called Crank–Nicolson in time for heat equations, is second order and A-stable. Resolve fast modes or damp unresolved transients before using it."
---

# Implicit trapezoidal rule (Crank–Nicolson in time)

## 1. What this solves

Use both endpoint slopes to advance a smooth time-dependent problem at second order. For a heat equation already discretized in space, this is the Crank–Nicolson time update. The method here takes the resulting ODE as input. Choice and implementation of a spatial discretization are outside its scope.

## 2. When to use

Use a fixed grid for a smooth ODE when an implicit endpoint solve and second-order accuracy suit the problem. A-stability is useful for diffusion, especially after the initial transient is resolved or damped. Supply the right-hand side $f(t,y)$ and a Jacobian, or provide your own endpoint solver.

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

| Situation | Use instead |
|---|---|
| Fast decay should disappear over a large step | `backward-euler` for rough results or startup damping, `bdf2` for second-order fixed steps, or `radau-iia-5` for adaptive high accuracy. |
| Symplecticity or exact quadratic invariants matter | `implicit-midpoint` or `gauss-legendre-4` for canonical systems. |
| Smooth general nonstiff dynamics with tolerance control | `dormand-prince-rk45`. |
| Positive or TVD states are required | Use `ssprk33` when forward Euler with your spatial operator preserves the property at a usable step. |

## 4. Algorithm

Advance from $(t,y)$ to $(t+h,Y)$ by solving

$$Y=y+\tfrac h2 [f(t,y)+f(t+h,Y)].$$

Cache $f(t,y)$ during the solve. Form $a=y+\tfrac h2f(t,y)$ and solve $Y-a-\tfrac h2 f(t+h,Y)=0$ by Newton, starting at $Y=y$. Use the matrix $I-\tfrac h2 J_f(t+h,Y)$, and subtract the computed correction. The update reverses when the endpoints swap and $h$ changes sign.

`implicit-midpoint` instead uses the single slope $f(t+h/2,(y+Y)/2)$. They give identical steps for autonomous linear or affine systems $f(y)=Ay+b$. A time-dependent linear RHS can already give different steps, and nonlinear systems generally do. The trapezoidal map in the original coordinates is generally not symplectic and does not preserve quadratic invariants in general. The checker includes a nonlinear rotation that changes its squared norm under trapezoid while midpoint preserves it.

## 5. Parameters and stiff startup

Use a signed fixed step and check second-order accuracy at a common physical time. The dense reference bounds each Newton correction by `max(tol * scale, 4 * ulp(scale))`. For each component, `scale` is its largest absolute value in the step input and the old and updated iterates, and `ulp` is floating-point spacing. The default is `tol=1e-13` with up to 20 updates. A singular matrix, nonfinite arithmetic or exhausted limit fails the step. A zero step returns the input.

For scalar decay with $z=h\lambda$, the factor $(1+z/2)/(1-z/2)$ tends to $-1$ as $z$ tends to $-\infty$. Steps with $z<-2$ alternate in sign. At $z=-1000$, the mode begins $1,-0.996008,0.992032,\dots$. Replacing the first full step by two `backward-euler` half steps reduces the unresolved mode before resuming trapezoid on the original grid. A few `bdf2` steps initialized by backward Euler can also damp the mode before switching. If forcing or boundaries keep exciting fast modes, continue with an L-stable method or decrease the step so decay is resolved. A damped startup reduces the size of ringing and can still leave sign changes. If positivity is mandatory, verify it for the actual time method, operator and step.

## 6. Applying it to your code

Use endpoint times consistently. For an affine linear system, solve the shifted linear system directly and reuse a factorization if the matrix and step stay fixed. For large nonlinear systems, use your sparse or structured solver. Add the code comment `implicit-trapezoidal v0.1.0, Crank & Nicolson (1947)`.

## 7. Verification

Export `step(fun, jac, t, y, h) -> y_next` in `port_adapter.py`. Run `python <skill>/checks/check_port.py` from the adapter directory and show its output. The fixed cases in `checks/cases.json` give the equations for ports in other languages. The checker covers signed steps, endpoint averaging, order, and ringing on a very fast scalar mode.

On your system, compare at fixed physical times after halving $h$. Inspect peaks just after startup or a discontinuity. Compare plain steps with a damped restart if the solution displays alternating spatial or temporal components. Use an independent check of your chosen spatial discretization.

## 8. Failure signatures

| Symptom | Likely cause |
|---|---|
| Alternating sign with a slowly shrinking envelope on fast decay | The far-left stability factor is close to $-1$. Damp the initial mode or switch to L-stable integration. |
| Linear fixtures pass while nonlinear fixtures fail | Check whether the code evaluates only one slope at the midpoint. |
| Newton fails on forced or coupled cases | Check Jacobian orientation, the factor $h/2$, and the two endpoint time arguments. |

## 9. Files and commands

`reference/implicit_trapezoidal.py` solves a fixed step. `uv run examples/fast_decay.py` shows ringing and a damped startup. `uv run verify/check_cases.py` tests mixed-scale nonlinear solves, correct and broken ports, midpoint equivalence for an autonomous affine system, nonlinear invariant loss, and a canonical area defect. `uv run checks/make_cases.py` prints the fixture JSON.

## 10. Sources

- Crank and Nicolson introduced the method in the heat-conduction setting. [Crank and Nicolson (1947)](https://doi.org/10.1017/S0305004100023197) · [1996 reprint](https://doi.org/10.1007/BF02127704)
- Giles and Carter analyze replacing initial Crank–Nicolson steps with backward Euler startup. [Giles and Carter (2006)](https://doi.org/10.21314/jcf.2006.152)
