Algorithm Search⌘K

Algorithmadvanced

Sontag's universal formula

Also called Sontag's formula.

Gives an explicit stabilizing feedback from a control Lyapunov function, smooth away from the origin.

Time
O(n m) worst
Space
O(m) extra
Where
m = the number of inputs; n = the number of states
Properties
deterministicexactonlineiterative
Origin
Eduardo D. Sontag, 1989

Sontag's universal formula turns a control Lyapunov function VV of a control-affine system x˙=f(x)+g(x)u\dot x = f(x) + g(x) u into a feedback u=k(x)u = k(x) by one explicit expression in the two Lie derivatives LfVL_f V and LgVL_g V. The feedback makes V˙=−(LfV)2+∣LgV∣4\dot V = -\sqrt{(L_f V)^2 + |L_g V|^4}, which is negative at every nonzero state, and it is smooth everywhere except perhaps at the origin. It is the constructive half of Artstein's theorem: Artstein proved that such a feedback exists, and the formula writes one down. The feedback is continuous at the origin exactly when VV has the small control property.

In plain words

Suppose you already have an energy-like measure of how far the system is from rest, and you know that at every state some input can make that measure go down. You still need a rule that picks one input at each state. This rule is a single closed formula. At each state it looks at two numbers: how fast the measure changes when you do nothing, and how strongly the input can push the measure. If doing nothing already lowers the measure, the rule applies only a small push. If doing nothing raises it, the rule pushes hard against it. The rule never divides by zero in a bad way, it changes smoothly from state to state, and it always makes the measure fall. You do not tune anything. All the design work is in the choice of the measure.

The problem

Input. Smooth maps f:Rn→Rnf : \mathbb{R}^n \to \mathbb{R}^n and g:Rn→Rn×mg : \mathbb{R}^n \to \mathbb{R}^{n \times m} with f(0)=0f(0) = 0, and a smooth, positive definite and proper (radially unbounded) function V:Rn→RV : \mathbb{R}^n \to \mathbb{R} that is a control Lyapunov function: at every x≠0x \neq 0, LgV(x)=0L_g V(x) = 0 implies LfV(x)<0L_f V(x) < 0. Smoothness makes f+gkf + g k locally Lipschitz wherever kk is smooth, so solutions exist and are unique there (see the Lipschitz condition and the Picard-Lindelöf theorem). Here LfV=∇V⋅fL_f V = \nabla V \cdot f is a number and LgV=∇V gL_g V = \nabla V\, g is a row of mm numbers.

Output. A feedback k:Rn→Rmk : \mathbb{R}^n \to \mathbb{R}^m with k(0)=0k(0) = 0 such that

  • kk is smooth on Rn∖{0}\mathbb{R}^n \setminus \{0\};
  • along x˙=f(x)+g(x)k(x)\dot x = f(x) + g(x) k(x), V˙(x)<0\dot V(x) < 0 for every x≠0x \neq 0;
  • if VV also has the small control property, kk is continuous at 00 and the origin of the closed loop is globally asymptotically stable.

The small control property says: for every ε>0\varepsilon > 0 there is a δ>0\delta > 0 such that each xx with 0<∣x∣<δ0 < |x| < \delta has some input uu with ∣u∣<ε|u| < \varepsilon and LfV(x)+LgV(x)u<0L_f V(x) + L_g V(x) u < 0. Norms here are Euclidean.

The algorithm

  1. SONTAG(x) ▷ f, g and ∇V are given
  2. 1a ← ∇V(x) · f(x) ▷ L_f V(x), a number
  3. 2b ← ∇V(x) g(x) ▷ L_g V(x), a row of m numbers
  4. 3β ← b b^T ▷ |b|^2
  5. 4if β = 0 then return u = 0
  6. 5s ← sqrt(a^2 + β^2); if a > 0 then φ ← (a + s)/β else φ ← β/(s - a)
  7. 6return u = -φ b^T

Lines L1 and L2 evaluate the two Lie derivatives at the current state. If the input has no effect on VV at this state (line L4), the formula returns zero; for a control Lyapunov function this happens only where the drift already lowers VV. Otherwise line L5 computes the scalar gain

φ(a,β)=a+a2+β2β,\varphi(a, \beta) = \frac{a + \sqrt{a^2 + \beta^2}}{\beta},

and line L6 pushes along −bT-b^T, the direction in which the input lowers VV fastest. The two branches at line L5 are equal, because (a+s)(s−a)=s2−a2=β2(a + s)(s - a) = s^2 - a^2 = \beta^2. The second branch avoids the cancellation in a+sa + s when aa is negative.

The gain φ\varphi is the positive root of βp2−2ap−β=0\beta p^2 - 2 a p - \beta = 0. With u=−φbTu = -\varphi b^T,

V˙=a+bu=a−φβ=−a2+β2.\dot V = a + b u = a - \varphi \beta = -\sqrt{a^2 + \beta^2}.

So the closed loop lowers VV at least at the rate ∣a∣|a| and at least at the rate β\beta. When the drift helps (a<0a < 0 and β\beta small) the gain is φ≈β/(2∣a∣)\varphi \approx \beta / (2 |a|), which is small. When the drift hurts (a>0a > 0) the gain is about 2a/β2 a / \beta, enough to cancel the drift twice over.

Example

The check value. Take x˙=x+u\dot x = x + u and V=x2/2V = x^2 / 2. Then a=x2a = x^2, b=xb = x and β=x2\beta = x^2. Line L5 gives φ=(x2+2x2)/x2=1+2\varphi = (x^2 + \sqrt{2} x^2)/x^2 = 1 + \sqrt 2, so u=−(1+2)xu = -(1 + \sqrt 2) x and the closed loop is x˙=−2 x\dot x = -\sqrt 2\, x. For x˙=cx+u\dot x = c x + u the same steps give u=−(c+c2+1)xu = -(c + \sqrt{c^2 + 1}) x, which is the LQR gain of the scalar Riccati equation 2cP−P2+1=02 c P - P^2 + 1 = 0. For c=1c = 1 this is P=1+2P = 1 + \sqrt 2.

A nonlinear system. Take

x˙1=−x1+x12x2,x˙2=u,V=12(x12+x22).\dot x_1 = -x_1 + x_1^2 x_2, \qquad \dot x_2 = u, \qquad V = \tfrac12 (x_1^2 + x_2^2).

Then a=−x12+x13x2=x12(x1x2−1)a = -x_1^2 + x_1^3 x_2 = x_1^2 (x_1 x_2 - 1) and b=x2b = x_2. Where b=0b = 0, a=−x12<0a = -x_1^2 < 0 for x1≠0x_1 \neq 0, so VV is a control Lyapunov function on the whole plane. The drift raises VV only where x1x2>1x_1 x_2 > 1. Start at x(0)=(2,1)x(0) = (2, 1), where a=4a = 4, b=1b = 1 and line L5 gives φ=4+17\varphi = 4 + \sqrt{17}, so u(0)=−8.12311u(0) = -8.12311 and V˙(0)=−17=−4.12311\dot V(0) = -\sqrt{17} = -4.12311. A 4th order Runge-Kutta simulation with step 0.010.01, with the feedback evaluated inside the right side, gives:

tt x1x_1 x2x_2 uu VV
0 2 1 -8.12311 2.5
0.5 2.03195 0.42643 -0.06851 2.15533
1 1.84232 0.40220 -0.03669 1.77796
2 1.23857 0.37134 -0.03069 0.83597
4 0.26436 0.25125 -0.10165 0.06651
6 0.03733 0.07237 -0.05567 0.00332
8 0.00506 0.01374 -0.01200 0.000107
10 0.00068 0.00229 -0.00209 0.0000028

In the first half second the input is large, because the drift pushes VV up and the formula pushes x2x_2 down until x1x2<1x_1 x_2 < 1. After that the drift lowers VV by itself, and the input stays small. VV decreases at every one of the 1000 steps. Near the origin x1x2<1x_1 x_2 < 1, so a≤0a \le 0 and ∣u∣=φ∣b∣≤∣b∣=∣x2∣|u| = \varphi |b| \le |b| = |x_2|; this is why the feedback is continuous at 00 here.

Correctness

The full proof is in proofs/correctness.md. Its main steps:

  • The derivative. With u=k(x)u = k(x), V˙=−a2+β2\dot V = -\sqrt{a^2 + \beta^2} when β>0\beta > 0 and V˙=a\dot V = a when β=0\beta = 0. For a control Lyapunov function both are negative at x≠0x \neq 0.
  • Smooth away from the origin. On the open set S={β>0 or a<0}S = \{\beta > 0 \text{ or } a < 0\}, φ\varphi is the root of βp2−2ap−β=0\beta p^2 - 2 a p - \beta = 0 at which the derivative in pp is 2a2+β2>02 \sqrt{a^2 + \beta^2} > 0 (or −2a>0-2a > 0 on β=0\beta = 0). The implicit function theorem makes φ\varphi real-analytic on SS. A control Lyapunov function keeps (a(x),β(x))(a(x), \beta(x)) in SS for x≠0x \neq 0, so kk is smooth there.
  • Continuity at the origin. If a≤0a \le 0, φ≤1\varphi \le 1 and ∣k∣≤∣b∣|k| \le |b|. If a>0a > 0, the small control property gives a<ε∣b∣a < \varepsilon |b|, and then ∣k∣<2ε+∣b∣|k| < 2 \varepsilon + |b|. Since b(0)=0b(0) = 0, k(x)→0k(x) \to 0. Conversely, if kk is continuous at 00, the input k(x)k(x) itself shows the small control property.
  • Global asymptotic stability. With the small control property, the closed loop is continuous, solutions are unique, and VV is a Lyapunov function in the sense of Lyapunov's direct method.

The tests check the check value and the scalar LQR gain, the identity a+bu=−a2+β2a + b u = -\sqrt{a^2 + \beta^2} and the quadratic for φ\varphi on 2000 random inputs with up to 3 entries, the table above within 10−510^{-5}, that VV decreases at every step, the bound ∣k∣≤∣x2∣|k| \le |x_2| near the origin, the unbounded feedback of the pitfall below, and a closed loop for the double integrator with the LQR matrix P=[3113]P = \begin{bmatrix} \sqrt 3 & 1 \\ 1 & \sqrt 3 \end{bmatrix} as V=xTPxV = x^T P x.

Proof: Sontag's formula makes the derivative of the control Lyapunov function negative, is smooth away from the origin, and is continuous at the origin exactly under the small control propertyfull

The system is x˙=f(x)+g(x)u\dot x = f(x) + g(x) u with x∈Rnx \in \mathbb{R}^n, u∈Rmu \in \mathbb{R}^m, ff and gg smooth (C∞C^\infty) and f(0)=0f(0) = 0. V:Rn→RV : \mathbb{R}^n \to \mathbb{R} is smooth, positive definite and proper (radially unbounded). For a state xx write

a(x)=∇V(x)⋅f(x),b(x)=∇V(x) g(x)∈R1×m,β(x)=∣b(x)∣2,a(x) = \nabla V(x) \cdot f(x), \qquad b(x) = \nabla V(x)\, g(x) \in \mathbb{R}^{1 \times m}, \qquad \beta(x) = |b(x)|^2,

where ∣⋅∣|\cdot| is the Euclidean norm. VV is a control Lyapunov function if β(x)=0\beta(x) = 0 implies a(x)<0a(x) < 0 for every x≠0x \neq 0. VV has the small control property if for every ε>0\varepsilon > 0 there is δ>0\delta > 0 such that every xx with 0<∣x∣<δ0 < |x| < \delta has some uu with ∣u∣<ε|u| < \varepsilon and a(x)+b(x)u<0a(x) + b(x) u < 0.

The algorithm (lines L1 to L6) returns k(x)=0k(x) = 0 if β(x)=0\beta(x) = 0 and k(x)=−φ(a(x),β(x)) b(x)Tk(x) = -\varphi(a(x), \beta(x))\, b(x)^T otherwise, where

φ(a,β)=a+a2+β2β(β>0),φ(a,0)=0.\varphi(a, \beta) = \frac{a + \sqrt{a^2 + \beta^2}}{\beta} \quad (\beta > 0), \qquad \varphi(a, 0) = 0.

Lemma 1: the two forms at line L5 agree

Let β>0\beta > 0 and s=a2+β2s = \sqrt{a^2 + \beta^2}. Then s>∣a∣≥as > |a| \ge a, so s−a>0s - a > 0, and (a+s)(s−a)=s2−a2=β2(a + s)(s - a) = s^2 - a^2 = \beta^2. Divide by β(s−a)\beta (s - a): (a+s)/β=β/(s−a)(a + s)/\beta = \beta / (s - a). So both branches of line L5 compute φ(a,β)\varphi(a, \beta). The second form adds two nonnegative numbers when a≤0a \le 0, so it has no cancellation. □\square

Lemma 2: the closed loop derivative

For every xx, with u=k(x)u = k(x),

V˙=a+b k(x)={−a2+β2if β>0,aif β=0.\dot V = a + b\, k(x) = \begin{cases} -\sqrt{a^2 + \beta^2} & \text{if } \beta > 0, \\ a & \text{if } \beta = 0. \end{cases}

Proof. If β=0\beta = 0, k=0k = 0 and V˙=a\dot V = a. If β>0\beta > 0, b k=−φ bbT=−φβ=−(a+a2+β2)b\, k = -\varphi\, b b^T = -\varphi \beta = -(a + \sqrt{a^2 + \beta^2}), so a+bk=−a2+β2a + b k = -\sqrt{a^2 + \beta^2}. □\square

Consequence. If VV is a control Lyapunov function, then V˙(x)<0\dot V(x) < 0 for every x≠0x \neq 0. If β(x)>0\beta(x) > 0, V˙=−a2+β2≤−β<0\dot V = -\sqrt{a^2 + \beta^2} \le -\beta < 0. If β(x)=0\beta(x) = 0 and x≠0x \neq 0, the definition gives V˙=a(x)<0\dot V = a(x) < 0.

Lemma 3: phi is real-analytic on the set S

Let S={(a,β)∈R2:β>0 or a<0}S = \{(a, \beta) \in \mathbb{R}^2 : \beta > 0 \text{ or } a < 0\}. SS is open, and φ\varphi is real-analytic on SS.

Proof. Put F(a,β,p)=βp2−2ap−βF(a, \beta, p) = \beta p^2 - 2 a p - \beta. For β>0\beta > 0 the roots of F(a,β,⋅)F(a, \beta, \cdot) are (a±a2+β2)/β(a \pm \sqrt{a^2 + \beta^2})/\beta, and φ\varphi is the larger one, so F(a,β,φ(a,β))=0F(a, \beta, \varphi(a, \beta)) = 0. For β=0\beta = 0 and a<0a < 0, F(a,0,p)=−2apF(a, 0, p) = -2 a p, which is zero at p=0=φ(a,0)p = 0 = \varphi(a, 0). So F(a,β,φ(a,β))=0F(a, \beta, \varphi(a, \beta)) = 0 on all of SS. The partial derivative is ∂F/∂p=2βp−2a\partial F / \partial p = 2 \beta p - 2 a. At p=φp = \varphi with β>0\beta > 0 it equals 2a2+β2>02 \sqrt{a^2 + \beta^2} > 0. At p=0p = 0 with β=0\beta = 0, a<0a < 0 it equals −2a>0-2 a > 0. By the real-analytic implicit function theorem, near each point of SS the equation F=0F = 0 has a unique real-analytic solution p(a,β)p(a, \beta) close to φ\varphi. It remains to show that this solution is φ\varphi itself. On β>0\beta > 0, φ\varphi is the larger root, which is analytic there by the formula. Near a point (a0,0)(a_0, 0) with a0<0a_0 < 0, Lemma 1 gives φ=β/(a2+β2−a)\varphi = \beta / (\sqrt{a^2 + \beta^2} - a) for β>0\beta > 0, and this expression is also 00 at β=0\beta = 0. The expression is a quotient of analytic functions with a denominator near −2a0>0-2 a_0 > 0, so it is analytic near (a0,0)(a_0, 0), and it equals φ\varphi there. So φ\varphi is real-analytic on SS. □\square

Theorem 1: smooth away from the origin

If VV is a control Lyapunov function, then kk is smooth on Rn∖{0}\mathbb{R}^n \setminus \{0\}.

Proof. Let x≠0x \neq 0. If β(x)>0\beta(x) > 0, then (a(x),β(x))∈S(a(x), \beta(x)) \in S. If β(x)=0\beta(x) = 0, the definition gives a(x)<0a(x) < 0, so again (a(x),β(x))∈S(a(x), \beta(x)) \in S. The maps x↦a(x)x \mapsto a(x), x↦β(x)x \mapsto \beta(x) and x↦b(x)x \mapsto b(x) are smooth, and SS is open. On a neighborhood of xx, k(x)=−φ(a(x),β(x)) b(x)Tk(x) = -\varphi(a(x), \beta(x))\, b(x)^T is a composition and product of smooth maps, by Lemma 3. Note that on β=0\beta = 0 the formula −φbT-\varphi b^T gives 00, which is the value that line L4 returns, so one smooth expression covers both branches. □\square

Theorem 2: continuity at the origin

Set k(0)=0k(0) = 0. Then kk is continuous at 00 if and only if VV has the small control property.

Proof. First, b(0)=0b(0) = 0. VV has a minimum at 00, so ∇V(0)=0\nabla V(0) = 0.

(⇐\Leftarrow) Let ε>0\varepsilon > 0. Take δ\delta from the small control property, and take δ′>0\delta' > 0 such that ∣b(x)∣<ε|b(x)| < \varepsilon when ∣x∣<δ′|x| < \delta'; δ′\delta' exists because bb is continuous and b(0)=0b(0) = 0. Let 0<∣x∣<min⁡(δ,δ′)0 < |x| < \min(\delta, \delta'). If β(x)=0\beta(x) = 0, k(x)=0k(x) = 0. Otherwise ∣k(x)∣=φ ∣b∣|k(x)| = \varphi\, |b|. There are two cases.

  • a≤0a \le 0. By Lemma 1, φ=β/(s−a)\varphi = \beta / (s - a) with s=a2+β2≥βs = \sqrt{a^2 + \beta^2} \ge \beta and −a≥0-a \ge 0, so φ≤1\varphi \le 1 and ∣k(x)∣≤∣b(x)∣<ε|k(x)| \le |b(x)| < \varepsilon.
  • a>0a > 0. The small control property gives a uu with ∣u∣<ε|u| < \varepsilon and a+bu<0a + b u < 0. By the Cauchy-Schwarz inequality, a<−bu≤∣b∣ εa < -b u \le |b|\, \varepsilon. Also s≤a+βs \le a + \beta. So φ≤(2a+β)/β\varphi \le (2 a + \beta)/\beta, and ∣k(x)∣≤(2a+β)/∣b∣=2a/∣b∣+∣b∣<2ε+ε|k(x)| \le (2 a + \beta) / |b| = 2 a / |b| + |b| < 2 \varepsilon + \varepsilon.

In both cases ∣k(x)∣<3ε|k(x)| < 3 \varepsilon. As ε\varepsilon is arbitrary, k(x)→0=k(0)k(x) \to 0 = k(0).

(⇒\Rightarrow) Let kk be continuous at 00, and let ε>0\varepsilon > 0. Take δ\delta with ∣k(x)∣<ε|k(x)| < \varepsilon for ∣x∣<δ|x| < \delta. For 0<∣x∣<δ0 < |x| < \delta, the input u=k(x)u = k(x) has ∣u∣<ε|u| < \varepsilon and, by Lemma 2 and its consequence, a+bu<0a + b u < 0. This is the small control property. □\square

Theorem 3: global asymptotic stability

If VV is a control Lyapunov function with the small control property, the closed loop x˙=F(x)=f(x)+g(x)k(x)\dot x = F(x) = f(x) + g(x) k(x) has the origin as a globally asymptotically stable equilibrium.

Proof. FF is continuous on Rn\mathbb{R}^n by Theorems 1 and 2, and F(0)=f(0)=0F(0) = f(0) = 0. On Rn∖{0}\mathbb{R}^n \setminus \{0\}, FF is smooth, so it is locally Lipschitz there, and solutions are unique while they stay away from 00 (Picard-Lindelöf). A solution that is at 00 at some time t0t_0 stays at 00 for t≥t0t \ge t_0: along any solution, ddtV(x(t))=∇V⋅F≤0\frac{d}{dt} V(x(t)) = \nabla V \cdot F \le 0, because it is negative for x≠0x \neq 0 and ∇V(0)=0\nabla V(0) = 0. So V(x(t))≤V(x(t0))=0V(x(t)) \le V(x(t_0)) = 0 for t≥t0t \ge t_0, and VV is positive definite. So every initial state has a unique forward solution. VV is proper, so the sublevel set {V≤V(x0)}\{V \le V(x_0)\} is compact and holds the whole solution, which therefore exists for all t≥0t \ge 0. With VV positive definite and proper and V˙<0\dot V < 0 for x≠0x \neq 0, Lyapunov's direct method gives global asymptotic stability; its proof uses only existence, uniqueness and the decrease of VV along solutions. □\square

The implementation

sontag_feedback computes aa at line L1 and the mm entries of bb at line L2 from the given ff, gg and ∇V\nabla V. sontag_gain computes β\beta at line L3, returns 00 at line L4 when β=0\beta = 0, computes φ\varphi at line L5 with the form of Lemma 1 that has no cancellation, and returns −φbT-\varphi b^T at line L6. Each line is a direct transcription of the definition of kk, so the code returns k(x)k(x) up to the rounding of floating point arithmetic. ■\blacksquare

Complexity

Count real operations and one square root at unit cost, as in the real RAM, with nn states and mm inputs.

  • Line L1. A dot product of length nn: O(n)O(n).
  • Line L2. A row vector times an n×mn \times m matrix: O(nm)O(n m).
  • Lines L3 to L6. O(m)O(m) operations and one square root.

So one evaluation costs O(nm)O(n m) operations plus one evaluation each of f(x)f(x), g(x)g(x) and ∇V(x)\nabla V(x), which the bound does not count. The extra space is O(m)O(m) for bb and uu. A closed-loop simulation costs the number of steps times this, times 4 for the four stages of RK4.

Properties

  • Explicit. No optimization, no iteration and no tuning parameter. Every design choice is in VV.
  • Smooth away from the origin. The formula is real-analytic in (a,β)(a, \beta) on the set where β>0\beta > 0 or a<0a < 0, so kk is as smooth as ff, gg and ∇V\nabla V there.
  • Continuous at the origin if and only if VV has the small control property.
  • Depends on the scaling of V. Replacing VV by cVc V with c>0c > 0 changes (a,β)(a, \beta) to (ca,c2β)(c a, c^2 \beta) and changes the feedback. The decrease V˙<0\dot V < 0 holds for every scaling.
  • Related to optimal control. In the scalar example the formula gives the LQR gain, and Sontag (1998, Exercise 5.9.11) relates the formula with an LQ problem in general. Freeman and Kokotović (1996) show that the pointwise min-norm laws, close relatives of the formula, are optimal for meaningful costs.

Variants and improvements

  • A general gain function. Sontag's paper allows φ=(a+a2+β q(β))/β\varphi = (a + \sqrt{a^2 + \beta\, q(\beta)})/\beta with a real-analytic qq that is positive for β>0\beta > 0 and q(0)=0q(0) = 0. The page uses q(β)=βq(\beta) = \beta. Other choices trade input size against the decay rate of VV.
  • Bounded inputs. Lin and Sontag (1991) give a version for ∣u∣≤1|u| \le 1, with an extra scaling that keeps the input in the unit ball. It needs VV to have a decrease with bounded input, which is a stronger assumption.
  • Pointwise min-norm control. Freeman and Kokotović (1996) choose at each state the input of least norm that makes V˙≤−σ(x)\dot V \le -\sigma(x) for a given positive function σ\sigma. This is a small quadratic program with one constraint, with the same closed form as the projection in the CBF safety filter.
  • Where the control Lyapunov function comes from. For a system in strict feedback form, backstepping builds VV and a feedback together. For a polynomial system, sum-of-squares programming can search for VV. For a linear system, V=xTPxV = x^T P x with PP from the Riccati equation.

When to use it

Use it when you have a control Lyapunov function and want a feedback with no further design, for example after backstepping or when VV comes from a physical energy. It is also a safe fallback inside a supervisor: whatever the state, it lowers VV. It is not the best choice when the input is limited, because the gain grows where a>0a > 0 and β\beta is small; use the bounded version or a quadratic program with input limits there. When the performance matters more than the simplicity, an optimal control design or model predictive control with VV as a terminal cost uses the same VV better.

Pitfalls

  • Cancellation at line L5. The direct form (a+s)/β(a + s)/\beta loses all digits when a<0a < 0 and β≪∣a∣\beta \ll |a|. In IEEE double precision, a=−1a = -1 and β=10−8\beta = 10^{-8} give s=1s = 1 exactly, and the direct form returns φ=0\varphi = 0; the true value is β/(1+1+β2)≈5⋅10−9\beta/(1 + \sqrt{1 + \beta^2}) \approx 5 \cdot 10^{-9}. Use the second form of line L5 when a≤0a \le 0.
  • No small control property. The formula is then discontinuous at the origin. For x˙=x+x2u\dot x = x + x^2 u with V=x2/2V = x^2 / 2: a=x2a = x^2 and b=x3b = x^3, and lines L5 and L6 give u=−(1+1+x8)/xu = -(1 + \sqrt{1 + x^8})/x, so ∣u∣≥2/∣x∣|u| \ge 2 / |x|, which is unbounded near 00. The closed loop x˙=−x1+x8\dot x = -x \sqrt{1 + x^8} is fine on its own, but the input is not. In this system no continuous feedback can do better: if kk is continuous, x k(x)→0x\, k(x) \to 0, so x˙=x(1+xk(x))\dot x = x (1 + x k(x)) has the sign of xx near 00.
  • V that is not a control Lyapunov function. Line L4 returns 00 whenever b=0b = 0, whatever aa is. For the double integrator x˙1=x2\dot x_1 = x_2, x˙2=u\dot x_2 = u with V=x12+x22V = x_1^2 + x_2^2, b=2x2b = 2 x_2 and a=2x1x2a = 2 x_1 x_2, so on the line x2=0x_2 = 0 both vanish. Line L4 returns u=0u = 0 there, and every point (x1,0)(x_1, 0) is an equilibrium of the closed loop. Check the control Lyapunov function condition before you trust the formula.
  • Sampled implementation. The proof assumes that line L6 is evaluated continuously. In the nonlinear example with the input held constant over intervals of 0.30.3, VV increases on 7 of the 1000 simulation steps, and with a hold of 0.50.5 the first input −8.12-8.12 drives x2x_2 to −3.06-3.06 and VV rises from 2.52.5 to 4.954.95. The state still went to the origin in these runs, but the decrease of VV is no longer guaranteed.

Implementations

Each implementation follows the pseudocode above. A comment @line links a line of code to a line of the pseudocode. Point at a line of the pseudocode to see the matching code. The build runs the native tests of each language.

ctlsim.pyI0RawGitHub
# SPDX-License-Identifier: MIT
# Shared helper of the control pages of https://algorithm.tamnd.com/. The authors are listed in prov/.

"""Fixed step simulation for the control pages.

Conventions:
- A continuous-time system is a function f(t, x, u) that returns x' as a
  flat list. x is a flat list. u is whatever the control returns (a flat
  list, a scalar, or None when there is no control).
- A discrete-time system is a function F(k, x, u) that returns x_(k+1).
- The integrator is the classical 4th order Runge-Kutta method with a fixed
  step dt. The input is held constant over each step.
- Time t_k = t0 + k dt is computed by multiplication, not by summation.
- The number of steps round((t1 - t0) / dt) and the hold ratio
  round(hold / dt) must match the real ratios within 1e-9 (relative), or
  ValueError is raised.

This file holds no controller or observer of a control page.
"""

__all__ = ["rk4_step", "simulate", "simulate_discrete", "lti"]


def _axpy(a, x, y):
    return [yi + a * xi for xi, yi in zip(x, y)]


def rk4_step(f, t, x, u, dt):
    """Return x(t + dt) after one RK4 step of x' = f(t, x, u), u held constant."""
    k1 = f(t, x, u)
    k2 = f(t + dt / 2, _axpy(dt / 2, k1, x), u)
    k3 = f(t + dt / 2, _axpy(dt / 2, k2, x), u)
    k4 = f(t + dt, _axpy(dt, k3, x), u)
    return [xi + dt / 6 * (a + 2 * b + 2 * c + d) for xi, a, b, c, d in zip(x, k1, k2, k3, k4)]


def _ratio(num, den, what):
    r = round(num / den)
    if abs(r * den - num) > 1e-9 * max(abs(num), abs(den)):
        raise ValueError(what + " is not an integer multiple of dt")
    return r


def simulate(f, x0, t0, t1, dt, control=None, hold=None):
    """Simulate x' = f(t, x, u) from x(t0) = x0 to t1 with RK4 and step dt.

    control(t, x) gives the input. It is called at each sampling instant
    t0 + j hold, and its value is held (zero-order hold) until the next
    instant. hold must be an integer multiple of dt; hold = None means
    hold = dt. With control = None the input is None.

    Returns (ts, xs, us): ts has N + 1 times, xs the N + 1 states, and us the
    N inputs, where us[k] acts on [ts[k], ts[k + 1]].
    """
    if dt <= 0:
        raise ValueError("dt must be positive")
    n_steps = _ratio(t1 - t0, dt, "t1 - t0")
    if n_steps < 0:
        raise ValueError("t1 is before t0")
    r = 1 if hold is None else _ratio(hold, dt, "hold")
    if r < 1:
        raise ValueError("hold must be at least dt")
    x = list(x0)
    ts = [t0]
    xs = [list(x)]
    us = []
    u = None
    for k in range(n_steps):
        t = t0 + k * dt
        if control is not None and k % r == 0:
            u = control(t, list(x))
        x = rk4_step(f, t, x, u, dt)
        ts.append(t0 + (k + 1) * dt)
        xs.append(x)
        us.append(u)
    return ts, xs, us


def simulate_discrete(F, x0, N, control=None):
    """Run x_(k+1) = F(k, x_k, u_k) for k = 0, ..., N - 1.

    u_k = control(k, x_k), or None if control is None. Returns (xs, us) with
    N + 1 states and N inputs. Exact if F and control are exact.
    """
    x = list(x0)
    xs = [list(x)]
    us = []
    for k in range(N):
        u = control(k, list(x)) if control is not None else None
        x = list(F(k, x, u))
        xs.append(x)
        us.append(u)
    return xs, us


def lti(A, B=None):
    """Return f(t, x, u) = A x + B u for the system x' = A x + B u.

    u may be a flat list, a scalar (one input) or None (zero input). B may
    be None for a system with no input.
    """
    def f(t, x, u):
        dx = [sum(a * xi for a, xi in zip(row, x)) for row in A]
        if B is None or u is None:
            return dx
        uu = u if isinstance(u, (list, tuple)) else [u]
        return [d + sum(b * ui for b, ui in zip(row, uu)) for d, row in zip(dx, B)]
    return f


def _self_test():
    import math
    f = lti([[-1.0]])
    _, xs, _ = simulate(f, [1.0], 0.0, 1.0, 0.01)
    assert abs(xs[-1][0] - math.exp(-1.0)) < 1e-9
    return True


if __name__ == "__main__":
    _self_test()
sontag_universal_formula.py 87 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/sontag-universal-formula/. The authors are listed in prov/B75HKW.jsonl.

"""Sontag's universal formula for a control-affine system

    x' = f(x) + g(x) u,    x in R^n,  u in R^m,

with a known control Lyapunov function V. At a state x let

    a = L_f V(x) = grad V(x) . f(x)            (a number)
    b = L_g V(x) = grad V(x) g(x)              (a row of m numbers)
    beta = |b|^2.

The feedback is u = 0 if beta = 0, and otherwise

    u = -phi b^T,    phi = (a + sqrt(a^2 + beta^2)) / beta.

Along the closed loop, V' = a - phi beta = -sqrt(a^2 + beta^2), which is
negative wherever a < 0 or b != 0, that is, at every x != 0 when V is a
control Lyapunov function.

When a <= 0 the numerator a + sqrt(a^2 + beta^2) cancels. The code then uses
the equal form phi = beta / (sqrt(a^2 + beta^2) - a), which has no
cancellation.

Precondition: f, g and grad V are given as functions that return lists. The
caller must make sure that V is a control Lyapunov function; the code does
not check it.

Cost: O(n m) arithmetic operations and one square root per state, plus one
evaluation each of f, g and grad V.
"""

import math

__all__ = ["sontag_gain", "sontag_feedback", "closed_loop"]


def sontag_gain(a, b):
    """Return the input u (a list of m numbers) from a = L_f V and b = L_g V.

    b is a list of m numbers. The result is u = -phi b with
    phi = (a + sqrt(a^2 + |b|^4)) / |b|^2, and u = 0 when b = 0.
    """
    beta = sum(bj * bj for bj in b)  # @line L3
    if beta == 0:  # @line L4
        return [0.0 for _ in b]  # @line L4
    s = math.sqrt(a * a + beta * beta)  # @line L5
    if a > 0:  # @line L5
        phi = (a + s) / beta  # @line L5
    else:
        phi = beta / (s - a)  # @line L5
    return [-phi * bj for bj in b]  # @line L6


def sontag_feedback(f, g, grad_V):
    """Return the feedback k(x) of Sontag's formula.

    f(x) returns the drift as a list of n numbers, g(x) returns the input
    matrix as n rows of m numbers, and grad_V(x) returns the gradient of V as
    a list of n numbers. k(x) returns a list of m numbers.
    """
    def k(x):
        p = grad_V(x)
        fx = f(x)
        gx = g(x)
        m = len(gx[0])
        a = sum(pi * fi for pi, fi in zip(p, fx))  # @line L1
        b = [sum(p[i] * gx[i][j] for i in range(len(p))) for j in range(m)]  # @line L2
        return sontag_gain(a, b)
    return k


def closed_loop(f, g, grad_V):
    """Return F(t, x, u) = f(x) + g(x) k(x) for the simulation helper.

    The feedback is evaluated inside the right side, so an RK4 step sees
    the continuous closed loop. The argument u is ignored.
    """
    k = sontag_feedback(f, g, grad_V)

    def F(t, x, u=None):
        uu = k(x)
        fx = f(x)
        gx = g(x)
        return [fx[i] + sum(gx[i][j] * uu[j] for j in range(len(uu))) for i in range(len(x))]
    return F
test_sontag_universal_formula.py 160 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/sontag-universal-formula/. The authors are listed in prov/B75HKW.jsonl.

import math
import random

import ctlsim
from sontag_universal_formula import closed_loop, sontag_feedback, sontag_gain

# Simulations: classical RK4 (ctlsim), step dt = 0.01, feedback evaluated
# inside the right side. Tolerances are stated at each check.
DT = 0.01


def steps(T):
    n = round(T / DT)
    assert abs(n * DT - T) < 1e-12
    return n


def scalar_system(c, d):
    return (lambda x: [c * x[0]]), (lambda x: [[d]]), (lambda x: [x[0]])


def page_system():
    def f(x):
        return [-x[0] + x[0] * x[0] * x[1], 0.0]

    def g(x):
        return [[0.0], [1.0]]

    def grad_V(x):
        return [x[0], x[1]]

    return f, g, grad_V


def test_check_value_scalar():
    # x' = x + u, V = x^2 / 2: a = x^2, b = x, u = -(1 + sqrt 2) x.
    f, g, gv = scalar_system(1.0, 1.0)
    k = sontag_feedback(f, g, gv)
    for x in [-3.0, -0.5, 0.25, 1.0, 7.0]:
        assert abs(k([x])[0] + (1 + math.sqrt(2)) * x) < 1e-12 * max(1.0, abs(x))
    assert k([0.0]) == [0.0]
    # Closed loop x' = -sqrt(2) x, so x(2) = exp(-2 sqrt 2).
    _, xs, _ = ctlsim.simulate(closed_loop(f, g, gv), [1.0], 0.0, 2.0, DT)
    assert len(xs) == steps(2.0) + 1
    assert abs(xs[-1][0] - math.exp(-2 * math.sqrt(2))) < 1e-9


def test_scalar_gain_equals_lq_gain():
    # x' = c x + u with V = x^2 / 2 gives u = -(c + sqrt(c^2 + 1)) x, the
    # gain of the scalar CARE with q = r = 1 (P = c + sqrt(c^2 + 1)).
    rng = random.Random(11)
    for _ in range(50):
        c = rng.uniform(-5, 5)
        x = rng.uniform(-4, 4)
        if abs(x) < 1e-3:
            continue
        f, g, gv = scalar_system(c, 1.0)
        u = sontag_feedback(f, g, gv)([x])[0]
        P = c + math.sqrt(c * c + 1)
        assert abs(P * P - 2 * c * P - 1) < 1e-9 * (1 + P * P)
        assert abs(u + P * x) < 1e-9 * (1 + abs(P * x))


def test_derivative_identity_and_quadratic():
    # a + b.u = -sqrt(a^2 + |b|^4) and beta phi^2 - 2 a phi - beta = 0.
    rng = random.Random(7)
    for _ in range(2000):
        m = rng.randint(1, 3)
        a = rng.choice([-1, 1]) * 10 ** rng.uniform(-6, 3)
        b = [rng.choice([-1, 1]) * 10 ** rng.uniform(-3, 2) for _ in range(m)]
        u = sontag_gain(a, b)
        beta = sum(bj * bj for bj in b)
        s = math.sqrt(a * a + beta * beta)
        vdot = a + sum(bj * uj for bj, uj in zip(b, u))
        assert abs(vdot + s) <= 1e-12 * (abs(a) + s) * 10
        phi = -u[0] / b[0]
        assert phi > 0
        assert abs(beta * phi * phi - 2 * a * phi - beta) <= 1e-10 * (beta * phi * phi + abs(a) * phi + beta)


def test_zero_b_gives_zero_input():
    assert sontag_gain(-2.0, [0.0, 0.0]) == [0.0, 0.0]
    assert sontag_gain(0.0, [0.0]) == [0.0]


def test_stable_form_when_a_negative():
    # The naive (a + sqrt(a^2 + beta^2)) / beta returns 0 here in floating
    # point. The true value is beta / (1 + sqrt(1 + beta^2)), about 5e-9.
    a, beta = -1.0, 1e-8
    naive = (a + math.sqrt(a * a + beta * beta)) / beta
    assert naive == 0.0
    u = sontag_gain(a, [math.sqrt(beta)])
    phi = -u[0] / math.sqrt(beta)
    exact = beta / (1 + math.sqrt(1 + beta * beta))
    assert abs(phi - exact) < 1e-12 * exact


def test_page_example_trace():
    f, g, gv = page_system()
    k = sontag_feedback(f, g, gv)
    assert abs(k([2.0, 1.0])[0] + (4 + math.sqrt(17))) < 1e-12
    _, xs, _ = ctlsim.simulate(closed_loop(f, g, gv), [2.0, 1.0], 0.0, 10.0, DT)
    V = [(x[0] ** 2 + x[1] ** 2) / 2 for x in xs]
    assert all(V[i + 1] < V[i] for i in range(len(V) - 1))
    table = {1.0: (1.84232, 0.40220), 2.0: (1.23857, 0.37134), 4.0: (0.26436, 0.25125), 6.0: (0.03733, 0.07237)}
    for T, (x1, x2) in table.items():
        x = xs[steps(T)]
        assert abs(x[0] - x1) < 1e-5 and abs(x[1] - x2) < 1e-5
    assert math.hypot(*xs[-1]) < 3e-3


def test_small_control_property_on_page_example():
    # For |x| <= 0.5, x1 x2 < 1, so a <= 0 and |k(x)| <= |b| = |x2| <= |x|.
    f, g, gv = page_system()
    k = sontag_feedback(f, g, gv)
    rng = random.Random(3)
    for _ in range(2000):
        r = 0.5 * rng.random()
        t = rng.uniform(0, 2 * math.pi)
        x = [r * math.cos(t), r * math.sin(t)]
        assert abs(k(x)[0]) <= abs(x[1]) + 1e-15


def test_no_small_control_property_gives_unbounded_feedback():
    # x' = x + x^2 u, V = x^2 / 2: u = -(1 + sqrt(1 + x^8)) / x.
    k = sontag_feedback(lambda x: [x[0]], lambda x: [[x[0] ** 2]], lambda x: [x[0]])
    for x in [1.0, 0.1, 0.01, 1e-3]:
        u = k([x])[0]
        assert abs(u + (1 + math.sqrt(1 + x ** 8)) / x) < 1e-9 * abs(u)
        assert abs(u) >= 2 / x * (1 - 1e-12)


def test_double_integrator_with_lq_lyapunov_function():
    # V = x^T P x with the LQR solution P = [[sqrt 3, 1], [1, sqrt 3]].
    r3 = math.sqrt(3)
    P = [[r3, 1.0], [1.0, r3]]

    def V(x):
        return sum(x[i] * P[i][j] * x[j] for i in range(2) for j in range(2))

    f = lambda x: [x[1], 0.0]
    g = lambda x: [[0.0], [1.0]]
    gv = lambda x: [2 * (P[0][0] * x[0] + P[0][1] * x[1]), 2 * (P[1][0] * x[0] + P[1][1] * x[1])]
    _, xs, _ = ctlsim.simulate(closed_loop(f, g, gv), [1.0, 0.0], 0.0, 15.0, DT)
    vs = [V(x) for x in xs]
    assert all(vs[i + 1] < vs[i] for i in range(len(vs) - 1))
    assert math.hypot(*xs[-1]) < 1e-3


def test_two_inputs():
    # x' = u with u in R^2 and V = |x|^2 / 2: a = 0, b = x, u = -x.
    k = sontag_feedback(lambda x: [0.0, 0.0], lambda x: [[1.0, 0.0], [0.0, 1.0]], lambda x: list(x))
    rng = random.Random(5)
    for _ in range(100):
        x = [rng.uniform(-2, 2), rng.uniform(-2, 2)]
        u = k(x)
        assert abs(u[0] + x[0]) < 1e-12 and abs(u[1] + x[1]) < 1e-12

History

Artstein (1983) proved that a control-affine system with a smooth control Lyapunov function has a feedback that is smooth away from the origin, by an argument with relaxed controls that does not give a formula. Sontag (1983) characterized asymptotic controllability by a Lyapunov-like function. In 1989 Sontag gave the explicit formula, called it "universal" because it depends only on LfVL_f V and LgVL_g V, and proved that it is real-analytic away from the origin and continuous at it under the small control property. Lin and Sontag (1991) extended it to bounded inputs. Freeman and Kokotović (1996) showed that pointwise min-norm laws built from a control Lyapunov function are optimal for meaningful costs, which made this family of formulas part of the inverse optimal design method.

References

  1. Eduardo D. Sontag, A 'universal' construction of Artstein's theorem on nonlinear stabilization, Systems and Control Letters 13(2), 117-123, 1989, the formula for control-affine systems and the proof that it is analytic where L_g V is not zero or L_f V is negative
  2. Eduardo D. Sontag, Mathematical Control Theory, second edition, Springer, 1998, Section 5.9 (Introduction to Nonlinear Stabilization), Proposition 5.9.10 and Exercise 5.9.11; Section 5.7 for control Lyapunov functions
  3. Miroslav Krstić, Ioannis Kanellakopoulos and Petar Kokotović, Nonlinear and Adaptive Control Design, Wiley, 1995, Section 2.1.2, Control Lyapunov functions
  4. Randy A. Freeman and Petar V. Kokotović, Inverse optimality in robust stabilization, SIAM Journal on Control and Optimization 34(4), 1365-1391, 1996, robust control Lyapunov functions and the pointwise min-norm control laws
  5. A universal formula for stabilization with bounded controls, the version of the formula with a bound on the norm of the input
  6. Zvi Artstein, Stabilization with relaxed controls, Nonlinear Analysis: Theory, Methods and Applications 7(11), 1163-1173, 1983, the existence result that the formula makes explicit

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH1 sketch
  • a full proof of time.worst
  • a full proof of space.extra
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
Code (python)I0 not checked yet
  • the tests did not run in this build
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW2 basic
  • a scene preset for the example