---
title: Lyapunov stability
heading: Proving stability with an energy-like function: Lyapunov's direct method, the Lyapunov equation for linear systems, energy arguments for nonlinear ones, and estimates of the region of attraction
eyebrow: S9 · Lesson 44 · Lyapunov stability
xp: 50
---

::: hook
Poles settle stability for linear systems. For nonlinear ones, linearization (lesson 3) tells you only what happens close to an equilibrium, and nothing about how close is close enough. Lyapunov's idea, from 1892, was to generalize energy: if some positive quantity of the state never increases along every trajectory, the state can't run away, and if it always decreases, the state must settle. Finding such a function proves stability without solving the equations at all, and its level sets give a region from which recovery is guaranteed.
:::

::: play
- The curve is the energy $V = \frac12(x^2 + \dot x^2)$ of a damped oscillator $\ddot x + 2\zeta\dot x + x = 0$ started at x = 1 at rest.
- Its rate of change is $\dot V = -2\zeta\dot x^2$: never positive, and zero only at the instants when the velocity passes through zero, which is why the curve descends in steps.
- That's a Lyapunov function: with ζ > 0 it proves the oscillator settles, without solving for x(t).
:::

```sim plot
view: graph
x: t = 0..15 s
y: V = 0..0.55
slider: z = 0.02..0.9 = 0.15
plot: 0.5*(exp(-2*z*t)*(cos(sqrt(1 - z^2)*t) + z/sqrt(1 - z^2)*sin(sqrt(1 - z^2)*t))^2 + exp(-2*z*t)*sin(sqrt(1 - z^2)*t)^2/(1 - z^2)) | label: V(t)
```

::: predict
## A damped pendulum 1 m long hangs at rest and is struck. With friction, what initial angular speed guarantees, by an energy argument, that it never swings over the top?

- [ ] Any speed: friction always wins
- [x] Anything below about 6.26 rad/s
- [ ] Below 3.13 rad/s
- [ ] Below 9.81 rad/s

::: reveal
The energy per unit $ml^2$ is $V = \frac12\dot\theta^2 + \frac{g}{l}(1 - \cos\theta)$, and friction makes $\dot V \le 0$. Reaching the top needs $V = 2g/l = 19.6$ s⁻², so starting with $\frac12\dot\theta_0^2 < 19.6$, that is $\dot\theta_0 < \sqrt{4g/l} = 6.26$ rad/s, keeps it below forever: a level set of V bounds the region the state can reach.
:::
:::

::: nameit
## Lyapunov's direct method

For $\dot x = f(x)$ with an equilibrium at 0, suppose a function V(x) is
- **positive definite**: V(0) = 0 and V(x) > 0 elsewhere, and
- **non-increasing** along trajectories: $\dot V = \nabla V \cdot f(x) \le 0$.

Then the equilibrium is stable. If $\dot V < 0$ everywhere except 0, it's asymptotically stable. If $\dot V \le 0$ but $\dot V = 0$ only on a set containing no complete trajectory except the equilibrium, LaSalle's principle still gives asymptotic stability, as for the oscillator above, whose $\dot V$ vanishes only at isolated instants.

## Linear systems: the Lyapunov equation

For $\dot x = Ax$, try $V = x^TPx$. Then $\dot V = x^T(A^TP + PA)x$, so choose a positive-definite Q and solve
$$A^TP + PA = -Q$$
A has all its eigenvalues in the left half-plane exactly when the solution P is positive definite. For one state, $\dot x = -ax$, this is $-2aP = -q$, so $P = q/(2a)$. For the companion form $A = \begin{pmatrix} 0 & 1 \\ -k & -c \end{pmatrix}$ with Q = I:
$$p_{12} = \frac{1}{2k}, \qquad p_{22} = \frac{1 + 1/k}{2c}, \qquad p_{11} = \frac{k + 1}{2c} + \frac{c}{2k}$$
The same equation gives the LQR cost (lesson 38) and the steady-state covariance of a system driven by noise.

## Regions of attraction

Where $\dot V < 0$ holds only in part of the state space, the largest level set $V(x) < c$ lying entirely inside that part is a region from which every trajectory converges: an estimate, usually conservative, of the **region of attraction**. For $\dot x = -x + x^3$, $V = x^2/2$ gives $\dot V = -x^2(1 - x^2)$, negative for |x| < 1, so every start with |x| < 1 returns to 0. Here that's exact: the system has other equilibria at ±1.
:::

::: example
## Solve the Lyapunov equation $A^TP + PA = -I$ for $A = \begin{pmatrix} 0 & 1 \\ -2 & -3 \end{pmatrix}$, and check it proves stability.

::: solution
1. **Write out P.**
   $$P = \begin{pmatrix} p_1 & p_2 \\ p_2 & p_3 \end{pmatrix}$$
2. **The equation's entries.**
   $$\begin{aligned} -4p_2 &= -1 \\ 2p_2 - 6p_3 &= -1 \\ p_1 - 3p_2 - 2p_3 &= 0 \end{aligned}$$
3. **Solve.**
   $$\begin{aligned} p_2 &= 0.25 \\ p_3 &= 0.25 \\ p_1 &= 1.25 \end{aligned}$$
4. **Check it's positive definite.** $p_1 > 0$, and
   $$\begin{aligned} \det P &= 0.3125 - 0.0625 \\ &= 0.25 > 0 \end{aligned}$$
5. **Conclusion.** So $V = x^TPx$ proves stability, matching A's eigenvalues at −1 and −2.

**Answer:** $p_1$ = 1.25 and $p_2 = p_3$ = 0.25; P is positive definite, so the origin is stable.
:::
:::

::: example
## A damped pendulum 1 m long hangs at rest. What initial angular speeds are guaranteed never to carry it over the top?

::: solution
1. **Energy function.**
   $$V = \tfrac12\dot\theta^2 + \tfrac gl(1 - \cos\theta)$$
2. **It never grows.** With friction,
   $$\dot V = -\frac{b}{ml^2}\dot\theta^2 \le 0$$
3. **Energy at the top.**
   $$\begin{aligned} V &= 2g/l \\ &= 19.62\ \text{s}^{-2} \end{aligned}$$
4. **Safe starting speeds.** Any start with $\frac12\dot\theta_0^2 < 19.62$, that is
   $$\begin{aligned} \dot\theta_0 &< \sqrt{4g/l} \\ &= 6.26\ \text{rad/s} \end{aligned}$$
   stays in the set V < 19.62 forever and, by LaSalle, settles at the bottom.
5. **Faster starts.** They may or may not go over; the energy argument is silent, since friction may remove enough on the way up.

**Answer:** any initial angular speed below 6.26 rad/s is guaranteed never to carry it over the top.
:::
:::

::: example
## For $\dot x = -2x + 0.5x^3$, estimate the region of attraction of x = 0 with $V = x^2/2$.

::: solution
1. **Derivative of V.**
   $$\begin{aligned} \dot V &= x\dot x \\ &= -2x^2 + 0.5x^4 \\ &= -x^2(2 - 0.5x^2) \end{aligned}$$
2. **Where it's negative.** For $x^2 < 4$, so every start with |x| < 2 returns to zero.
3. **Check against the equilibria.** The other equilibria sit at $x^2 = 4$, x = ±2, so the estimate is exact; beyond them the cubic term wins and the state escapes.

**Answer:** every start with |x| < 2 returns to zero, and that estimate is exact.
:::
:::

::: check
kind: parts
given: k = 1 | 2 | 5; c = 1 | 3 | 6

## Solve $A^TP + PA = -I$ for $A = \begin{pmatrix} 0 & 1 \\ -\given{k} & -\given{c} \end{pmatrix}$.

::: part
answer: 1/(2*k)
tolerance: 0.5%

### What is $p_{12}$?

::: misses
- [any] The (1, 1) entry gives $-2kp_{12} = -1$.
:::
:::

::: part
answer: (1 + 1/k)/(2*c)
tolerance: 0.5%

### What is $p_{22}$?

::: misses
- [any] The (2, 2) entry: $2p_{12} - 2cp_{22} = -1$.
:::
:::

::: part
answer: (k + 1)/(2*c) + c/(2*k)
tolerance: 0.5%

### What is $p_{11}$?

::: misses
- [any] The off-diagonal entry: $p_{11} = kp_{22} + cp_{12}$.
:::
:::
:::

::: check
kind: parts
given: a = 0.5 | 2 | 10; q = 1 | 4

## The scalar system $\dot x = -\given{a}x$ has the Lyapunov function $V = Px^2$ with $\dot V = -\given{q}x^2$.

::: part
answer: q/(2*a)
tolerance: 0.5%

### What is P?

::: misses
- [q/a] $\dot V = 2Px\dot x = -2aPx^2$.
- [any] $P = q/(2a)$.
:::
:::

::: part
answer: 2*a
unit: s⁻¹
tolerance: 0.5%

### At what rate does V decay, in s⁻¹?

::: misses
- [any] $\dot V/V = -2a$.
:::
:::
:::

::: check
kind: parts
given: l = 0.2 | 0.5 | 1 | 2

## A damped pendulum {l} m long hangs at rest and is struck.

::: part
answer: 2*9.81/l
tolerance: 0.5%

### What energy per unit $ml^2$, in s⁻², does it need to reach the top?

::: misses
- [9.81/l] The top is 2l above the bottom.
- [any] $2g/l$.
:::
:::

::: part
answer: sqrt(4*9.81/l)
unit: rad/s
tolerance: 0.5%

### Below what initial angular speed is it guaranteed never to go over?

::: misses
- [any] $\frac12\dot\theta_0^2 < 2g/l$.
:::
:::
:::

::: check
kind: parts
given: a = 1 | 2 | 4; b = 0.25 | 0.5 | 3

## The system $\dot x = -\given{a}x + \given{b}x^3$ is studied with $V = x^2/2$.

::: part
answer: sqrt(a/b)
tolerance: 0.5%

### Within what |x| is $\dot V$ negative?

::: misses
- [a/b] Solve $x^2 < a/b$.
- [any] $\dot V = -x^2(a - bx^2)$.
:::
:::
:::

::: check
kind: parts
given: k = 2 | 5; c = 2 | 4; x = 1 | 3

## $A = \begin{pmatrix} 0 & 1 \\ -\given{k} & -\given{c} \end{pmatrix}$ has the Lyapunov function $V = x^TPx$ with $A^TP + PA = -I$; the state starts at ({x}, 0).

::: part
answer: x^2*((k + 1)/(2*c) + c/(2*k))
tolerance: 0.5%

### What is V at the start?

::: misses
- [any] $p_{11}x_1^2$.
:::
:::

::: part
answer: x^2*((k + 1)/(2*c) + c/(2*k))
tolerance: 0.5%

### What is $\int_0^\infty x^Tx\,dt$, the integrated squared state?

::: misses
- [any] Since $\dot V = -x^Tx$, integrating from 0 to ∞ gives $V(x_0)$.
:::
:::
:::

::: check
kind: choice

## A candidate Lyapunov function has $\dot V \le 0$ everywhere, but $\dot V = 0$ whenever the velocity is zero, as for a damped oscillator's energy. What can be concluded?

- [ ] Nothing: $\dot V$ must be strictly negative
- [x] Stability, and by LaSalle's principle asymptotic stability, because the only trajectory that can stay where the velocity is zero is the equilibrium itself
- [ ] Instability
- [ ] Only that the energy is constant

::: misses
- [1] Strict negativity is sufficient, not necessary.
- [3] A non-increasing positive V rules out runaway.
- [4] It decreases whenever the velocity is nonzero.
:::
:::

::: check
kind: derivation
vars: k = 0.5..20, c = 0.5..20

## The Lyapunov equation $A^TP + PA = -I$ for $A = \begin{pmatrix} 0 & 1 \\ -k & -c \end{pmatrix}$.

::: step
answer: 1/(2*k)

### What is $p_{12}$?
:::

::: step
answer: (1 + 1/k)/(2*c)

### What is $p_{22}$?
:::

::: step
answer: ((k + 1)/(2*c) + c/(2*k))*((1 + 1/k)/(2*c)) - 1/(4*k^2)

### What is det P (positive for every stable A)?
:::

::: right
Right. Positive k and c, exactly the stability conditions for a second-order system, make P positive definite.
:::
:::

::: check
kind: python

## Write `lyap2(A, Q)`: solve $A^TP + PA = -Q$ for 2 × 2 lists A and symmetric Q, returning P as a 2 × 2 list, by solving the three linear equations for $p_{11}, p_{12}, p_{22}$; and `is_pos_def(P)`.

```python starter
def lyap2(A, Q):
    return [[1.0, 0.0], [0.0, 1.0]]

def is_pos_def(P):
    return True
```

```python solution
def lyap2(A, Q):
    (a, b), (c, d) = A
    # unknowns p11, p12, p22
    M = [[2 * a, 2 * c, 0.0],
         [b, a + d, c],
         [0.0, 2 * b, 2 * d]]
    rhs = [-Q[0][0], -Q[0][1], -Q[1][1]]
    # Gaussian elimination with partial pivoting
    n = 3
    for i in range(n):
        piv = max(range(i, n), key=lambda r: abs(M[r][i]))
        M[i], M[piv] = M[piv], M[i]
        rhs[i], rhs[piv] = rhs[piv], rhs[i]
        for r in range(i + 1, n):
            f = M[r][i] / M[i][i]
            for k in range(i, n):
                M[r][k] -= f * M[i][k]
            rhs[r] -= f * rhs[i]
    x = [0.0] * n
    for i in reversed(range(n)):
        x[i] = (rhs[i] - sum(M[i][k] * x[k] for k in range(i + 1, n))) / M[i][i]
    return [[x[0], x[1]], [x[1], x[2]]]

def is_pos_def(P):
    return P[0][0] > 0 and P[0][0] * P[1][1] - P[0][1] * P[1][0] > 0
```

```python tests
P = lyap2([[0, 1], [-2, -3]], [[1, 0], [0, 1]])
assert abs(P[0][0] - 1.25) < 1e-12 and abs(P[0][1] - 0.25) < 1e-12 and abs(P[1][1] - 0.25) < 1e-12, "The worked example."
assert is_pos_def(P), "A stable A gives a positive-definite P."
assert is_pos_def(lyap2([[-1, 5], [-5, -1]], [[1, 0], [0, 1]])), "Lightly damped but stable: still positive definite."
assert not is_pos_def(lyap2([[0, 1], [2, -1]], [[1, 0], [0, 1]])), "An unstable A can't give a positive-definite P."
```

::: right
Right. Solving a linear equation replaces finding eigenvalues, and the same P certifies stability and measures how fast the state decays.
:::
:::

::: explain
## How can a Lyapunov function prove a nonlinear system stable without solving its equations?

::: model
A Lyapunov function is a positive measure of how far the state is from equilibrium, like stored energy, that can be checked to never increase along the system's motion just by computing its rate of change from the differential equation at each point, without solving for the trajectory. If that rate is never positive, the state can never wander farther than the level of the function it started at, which proves stability. If the rate is negative everywhere except the equilibrium, or zero only where no trajectory can stay, the state must keep losing that measure and settle at the equilibrium. Where the rate is negative only in part of the state space, the largest level set inside that part gives a region from which recovery is guaranteed.
:::
:::

::: practice
::: check
given: a = 1 | 5; q = 2 | 8
answer: q/(2*a)
tolerance: 0.5%

## P for $\dot x = -\given{a}x$ with $\dot V = -\given{q}x^2$?

::: misses
- [any] $q/(2a)$.
:::
:::

::: check
given: k = 1 | 2 | 4 | 5 | 8 | 10
answer: 1/(2*k)
tolerance: 0.5%

## $p_{12}$ for the companion matrix with k = {k}, Q = I?

::: misses
- [any] $1/(2k)$.
:::
:::

::: check
given: l = 0.2 | 0.3 | 0.5 | 1 | 1.5 | 2
answer: sqrt(4*9.81/l)
unit: rad/s
tolerance: 0.5%

## Guaranteed no-flip speed for a {l} m pendulum?

::: misses
- [any] $\sqrt{4g/l}$.
:::
:::
:::
