fhat. Workspace

Implicit trapezoidal rule (Crank–Nicolson in time)

Average endpoint slopes for second-order implicit time steps.

errorO(h2)costnonlinear solve / stepreversible · implicit · A-stable

Crank & Nicolson 1947

Full skillv0.1.0

Use with your agent

Use the fhat method "implicit-trapezoidal" (https://fhat.dev/methods/implicit-trapezoidal/SKILL.md) for this problem. Check first whether it applies.

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)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)(t,y) to (t+h,Y)(t+h,Y) by solving

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

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

implicit-midpoint instead uses the single slope f(t+h/2,(y+Y)/2)f(t+h/2,(y+Y)/2). They give identical steps for autonomous linear or affine systems f(y)=Ay+bf(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λz=h\lambda, the factor (1+z/2)/(1−z/2)(1+z/2)/(1-z/2) tends to −1-1 as zz tends to −∞-\infty. Steps with z<−2z<-2 alternate in sign. At z=−1000z=-1000, the mode begins 1,−0.996008,0.992032,…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 hh. 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-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/2h/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

Evidence

Output of the verification scripts, run on the reference implementation of version 0.1.0 when this page was built.

Passed verify/check_cases.py: Tests regeneration, second order, fast-mode ringing and its startup fix, affine equivalence, nonlinear invariant and symplectic defects, mixed-scale endpoint solves, and three broken ports.
PASS nonautonomous: endpoint error 0.00e+00
PASS affine: endpoint error 0.00e+00
PASS rotation: endpoint error 0.00e+00
PASS rotation: endpoint error 0.00e+00
PASS fast: endpoint error 0.00e+00
PASS global orders 2.005, 2.001, 2.000
PASS h*lambda=-1000: plain y1=-0.996008, y4=0.984127; y4 with backward Euler startup=-3.937e-06; exact y4=0
PASS mixed-scale active error in normalized units: 1.11e-16
PASS affine midpoint equality gap 1.16e-16
PASS nonlinear squared-norm error trapezoid/midpoint 2.810e-02/0.000e+00
PASS trapezoid canonical area defect 2.247e-02
PASS midpoint: rejected by 3 fixture(s)
PASS backward_euler: rejected by 5 fixture(s)
PASS wrong_time: rejected by 1 fixture(s)

More methods are coming.