case study · flagship project

Lyapunov stability optimization

Automatically searching for Lyapunov functions and estimating stability regions in nonlinear dynamical systems.

role
Sole author — research and implementation
origin
Reimplementation of methods from my doctorate
stack
Python · PyTorch
repository
lyapunov-stability-optimization ↗

The problem

Take any system that evolves over time, whether an aircraft actuator, a robot, a power network or a control loop. A small disturbance pushes it away from where it should be. Does it come back, or does it drift, oscillate, and eventually break something?

For linear systems, powerful general tools provide comparatively direct answers. For nonlinear systems, the picture is much harder. In practice, simulation is indispensable, but it only evaluates the scenarios that are actually run. Stability analysis asks a different question: whether a property can be established over an entire region of the state space.

There is a stronger answer, and it is more than a century old. If you can exhibit a single scalar function of the state that behaves like an energy, strictly positive away from equilibrium and always decreasing along the motion of the system, then you have proved that the system converges. That function is a Lyapunov function: a mathematical certificate that can establish stability over a region without relying on an exhaustive set of simulated trajectories.

# continuous-time system, equilibrium at the origin
x' = f(x), f(0) = 0

# find V : R^n -> R over a region D containing the origin
V(0) = 0
V(x) > 0 for all x in D, x != 0
dV/dt = grad V(x) . f(x) < 0 for all x in D, x != 0

# discrete-time version
x[k+1] = f(x[k])
V(f(x)) - V(x) < 0 for all x in D, x != 0

# under the Lyapunov assumptions, a suitable sublevel set
# provides an inner estimate of the region of attraction
Formal statement of the Lyapunov conditions, in continuous and discrete time.

The catch is the word find. Lyapunov theory tells you what a valid certificate looks like; it does not tell you how to obtain one. For anything beyond a small textbook system, finding V is an art, done by hand, by people who are good at it.

Why it is hard

The search space has no useful shape
You are searching over functions, not points. Restrict the family too much and no valid certificate exists inside it; open it up and the search becomes non-convex, high-dimensional and full of flat regions where a gradient tells you nothing.
The constraint is universally quantified
The conditions must hold at every point of a region, not at the finitely many samples you can afford to check. A candidate that satisfies them on a grid can still fail between grid points.
Conservatism can limit practical value
A certificate that only establishes stability in a tiny neighbourhood of the equilibrium may have limited practical value. The challenge is therefore not only to find a valid candidate, but to identify as large a meaningful region as possible.

The approach

Treat the search for a certificate as a global optimization problem. Choose a family of candidate functions with tunable parameters, define a loss that measures how badly a candidate violates the Lyapunov conditions over a region, and let a global optimizer search the parameter space.

candidate families

What V is allowed to look like

  • Quadratic forms: V(x) = xTPx with P positive definite. Few parameters, fast to evaluate, interpretable, and the natural baseline: if a quadratic candidate suffices, you want to know.
  • Neural Lyapunov functions: a small tanh network provides a more expressive candidate family. The parameterization enforces the equilibrium conditions V(0) = 0 and ∇V(0) = 0, while positivity and decrease conditions are checked numerically during search and validation.

search

How the parameters are found

  • Genetic algorithms: population-based, derivative-free, and tolerant of the discontinuous penalties that constraint violations introduce.
  • Particle swarm optimization: another population-based, derivative-free search method, used here for the discrete-time neural reference experiment.
  • Coarse-to-fine validation: candidates are optimised on a 21×21 grid, then checked on increasingly fine 101×101 and 201×201 grids. A separate frozen audit evaluates 100,000 deterministic random points and densely samples the search-box boundary.

Both continuous-time and discrete-time dynamics are handled in the same framework. Only the decrease condition changes: a derivative in one case and a one-step difference in the other. The same candidate families and optimizers can therefore be used for both.

Two 3-D surface plots on the square [-1, 1] squared: the neural Lyapunov candidate V(x) on the left, and its time derivative V-dot(x) on the right, sampled on a 121 by 121 grid.
Neural Lyapunov candidate candidate surface and sampled Lyapunov conditions on the continuous-time benchmark · descriptive surface, not a formal certificate Open the full-size image

What the experiments cover

Three maintained reference experiments, each run over ten deterministic seeds under the same validation protocol.

Continuous-time neural candidate
nonlinear continuous-time benchmark · neural Lyapunov candidate · genetic algorithm · Method 2 · 10 deterministic seeds
Discrete-time neural candidate
nonlinear discrete-time benchmark · neural Lyapunov candidate · particle swarm optimisation · Method 2 · 10 deterministic seeds
Method 1 vs Method 2
difficult nonlinear continuous-time benchmark · quadratic Lyapunov candidates · genetic algorithm · identical reference protocol across 10 seeds · comparison of the reported sampled region

Method 1 scores candidates pointwise; Method 2 optimises the sampled sublevel set directly. Both are compared on the same benchmark, candidate family, search budget, seeds, search box and validation protocol.

Results

All figures and numbers below are outputs of the repository. The reported region is the 4-connected sampled sublevel-set component containing the equilibrium, measured on the final 201×201 grid. Outside that region no conclusion is drawn, and the complement is never labelled unstable. The frozen audit is deliberately stricter: it evaluates the full sampled sublevel set, including any disconnected components, rather than only the connected component reported on the grid.

Two phase-plane panels comparing the reported sampled region of Method 1 and Method 2 on the same nonlinear benchmark. Method 2 covers a visibly larger area. Both panels are annotated: outside, no conclusion.
Method 1 vs Method 2 reported sampled region on the same nonlinear benchmark · representative seed shown, ten-seed statistics below Open the full-size image
Method 1: pointwise objective
mean reported-grid fraction 24.73% · population std 0.36 pp · range 24.05–25.09% · frozen audit 10/10 · sampled-enclosed
Method 2: direct level-set objective
mean reported-grid fraction 47.19% · population std 0.74 pp · range 45.19–47.79% · frozen audit 10/10 · sampled-enclosed
Ratio
Under this exact Python protocol, Method 2 produces a reported-grid region approximately 1.91× larger on average than Method 1. This holds for this benchmark and protocol; it is not a general claim about either objective.
Four phase-plane panels showing the sampled sublevel region under successive grid refinements at 21 by 21, 101 by 101 and 201 by 201, then the reported frozen threshold after the 2 percent holdback.
Coarse-to-fine validation candidate parameters are frozen while the sampled threshold is refined on denser grids, then held back by 2% before the independent audit · this example reaches the box edge and is labelled search-box limited Open the full-size image

On the continuous and discrete neural reference experiments, all 10 seeds pass the entire sampled search box, and the frozen audit passes 10/10 in both cases. These runs are therefore labelled search-box limited: the experiment identifies no boundary inside the tested box and makes no claim outside it.

Phase-plane plot of the discrete-time benchmark in which the sampled region fills the entire tested square box, annotated as search-box limited.
Discrete-time sampled region the sampled region fills the tested box, so no boundary is identified inside it Open the full-size image

This is numerical validation by finite sampling, not a formal certificate over a continuum. The 2% holdback applied to the refined threshold is a transparent numerical guard band, not a discretization-error bound.

Reproducibility

A result that cannot be re-run is an anecdote. The repository is arranged so that someone evaluating it can get from a clean checkout to the figures without asking me anything.

  • A command-line interface and reference scripts that reproduce each maintained experiment.
  • Unit tests covering the dynamics, Lyapunov conditions, optimizers, autodiff and validation.
  • Continuous integration across Python 3.10–3.13.
  • Seeded reference runs with versioned numerical outputs.
  • Generated figures traced to their source JSON and candidate parameters through SHA-256 hashes.

What this demonstrates

This project sits deliberately at the intersection I work in. It is a piece of mathematics: the underlying conditions are precise and falsifiable. It is a piece of machine learning: a neural network parameterizes the object being searched for. It is a piece of optimization: the search is global, non-convex and derivative-free. And it is a piece of engineering: tested, reproducible and readable by someone who has never met me.

The same shape recurs in the applied work: understand the system well enough to say what "correct" means, find the answer with whatever method the problem actually calls for, then make it something other people can rely on.

Limitations & next steps

Sampling is not verification
The conditions are checked on a finite set of sampled points. A formal continuous-domain verification layer, for example using interval methods or suitable formal solvers, could go beyond the current finite-sampling protocol. That is the natural next step.
Dimension
Population-based search scales poorly with the number of parameters. Larger systems need either better-structured candidate families or a hybrid with gradient-based refinement.
Known dynamics
The method assumes a model of f. Extending it to learned or partially known dynamics is where this line of work meets the current interest in certified learned control.

Go deeper

The methodology, the reference scripts and the results are in the repository. It is published so that it can be read and assessed, not licensed for reuse.