Skip to content

auto_math_lyapunov_stability_3

github-actions[bot] edited this page Nov 14, 2025 · 1 revision

Snippet from Math-Lyapunov-Stability.md

import numpy as np
from scipy.linalg import solve_continuous_lyapunov

def design_quadratic_lyapunov(A):
    """
    Design quadratic Lyapunov function for linear system.
    
    Args:
        A: System matrix (n x n).
    
    Returns:
        P: Lyapunov matrix for V = x^T P x.
    """
    n = A.shape[0]
    Q = np.eye(n)  # Typically choose identity
    
    # Solve Lyapunov equation
    P = solve_continuous_lyapunov(A.T, -Q)
    
    return P

def lyapunov_quadratic(x, P):
    """Evaluate quadratic Lyapunov function."""
    return x.T @ P @ x

def lyapunov_derivative(x, P, A):
    """Time derivative along trajectories."""
    return x.T @ (A.T @ P + P @ A) @ x  # Should equal -x^T Q x

# Example
A = np.array([[-1, 2], [-3, -2]])
P = design_quadratic_lyapunov(A)
x = np.array([1.0, 0.5])

V_x = lyapunov_quadratic(x, P)
V_dot = lyapunov_derivative(x, P, A)

print(f"V(x) = {V_x:.3f}")
print(f"V-dot(x) = {V_dot:.3f} (should be negative)")

Clone this wiki locally