DLYAP

This function solves the discrete-time Lyapunov equation for a stable linear system and can also be used for the related discrete Sylvester equation.

A X A^T - X + Q = 0

The resulting matrix X is used in stability analysis, covariance propagation, and discrete-time energy calculations. When the optional matrix C is supplied, the computation switches to the discrete Sylvester form supported by the underlying library.

Excel Usage

=DLYAP(A, Q, C)
  • A (list[list], required): Square matrix A.
  • Q (list[list], required): Square symmetric matrix Q.
  • C (list[list], optional, default: null): Optional matrix C for solving the Sylvester equation.

Returns (list[list]): The solution matrix X.

Example 1: Discrete stable system Lyapunov solution

Inputs:

A Q
0.5 0 1 0
0 0.3 0 1

Excel formula:

=DLYAP({0.5,0;0,0.3}, {1,0;0,1})

Expected output:

Result
1.33333 0
0 1.0989
Example 2: Scalar discrete Lyapunov equation

Inputs:

A Q
0.2 1

Excel formula:

=DLYAP({0.2}, {1})

Expected output:

1.04167

Example 3: Coupled stable discrete system

Inputs:

A Q
0.4 0.1 1 0
0 0.3 0 1

Excel formula:

=DLYAP({0.4,0.1;0,0.3}, {1,0;0,1})

Expected output:

Result
1.20713 0.0374625
0.0374625 1.0989
Example 4: Scalar discrete Sylvester equation

Inputs:

A Q C
0.5 0.4 1

Excel formula:

=DLYAP({0.5}, {0.4}, {1})

Expected output:

1.25

Python Code

Show Code
import control as ct
import numpy as np

def dlyap(A, Q, C=None):
    """
    Solve the discrete-time Lyapunov equation.

    See: https://python-control.readthedocs.io/en/latest/generated/control.dlyap.html

    This example function is provided as-is without any representation of accuracy.

    Args:
        A (list[list]): Square matrix A.
        Q (list[list]): Square symmetric matrix Q.
        C (list[list], optional): Optional matrix C for solving the Sylvester equation. Default is None.

    Returns:
        list[list]: The solution matrix X.
    """
    try:
        def to_np(x):
            if x is None:
                return None
            if not isinstance(x, list):
                return np.array([[float(x)]])
            if x and not isinstance(x[0], list):
                x = [x]
            return np.array([[float(v) if v is not None and str(v) != "" else 0.0 for v in row] for row in x])

        a_np = to_np(A)
        q_np = to_np(Q)
        c_np = to_np(C)

        try:
            X = ct.dlyap(a_np, q_np, C=c_np)
        except Exception:
            if c_np is None:
                raise

            left = np.eye(a_np.shape[0] * q_np.shape[0]) - np.kron(q_np, a_np)
            right = c_np.reshape(-1, order="F")
            X = np.linalg.solve(left, right).reshape((a_np.shape[0], q_np.shape[0]), order="F")

        x_array = np.asarray(X, dtype=float)
        if x_array.ndim == 0:
            return [[float(x_array)]]

        return x_array.tolist()
    except Exception as e:
        return f"Error: {str(e)}"

Online Calculator

Square matrix A.
Square symmetric matrix Q.
Optional matrix C for solving the Sylvester equation.