LYAP

This function solves the continuous-time Lyapunov equation for stable linear systems and also supports the related Sylvester equation form.

A X + X A^T + Q = 0

A X + X Q + C = 0

The solution matrix X appears in stability analysis, covariance calculations, and controllability or observability Gramian computations. Providing the optional matrix C switches the calculation to the Sylvester equation supported by the wrapped library routine.

Excel Usage

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

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

Example 1: Stable system Lyapunov solution

Inputs:

A Q
-1 0 1 0
0 -2 0 1

Excel formula:

=LYAP({-1,0;0,-2}, {1,0;0,1})

Expected output:

Result
0.5 0
0 0.25
Example 2: Scalar continuous Lyapunov equation

Inputs:

A Q
-2 4

Excel formula:

=LYAP({-2}, {4})

Expected output:

1

Example 3: Coupled stable continuous system

Inputs:

A Q
-1 1 1 0
0 -2 0 1

Excel formula:

=LYAP({-1,1;0,-2}, {1,0;0,1})

Expected output:

Result
0.583333 0.0833333
0.0833333 0.25
Example 4: Scalar continuous Sylvester equation

Inputs:

A Q C
-1 -2 3

Excel formula:

=LYAP({-1}, {-2}, {3})

Expected output:

1

Python Code

Show Code
import control as ct
import numpy as np

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

    See: https://python-control.readthedocs.io/en/latest/generated/control.lyap.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 (or Q for Sylvester equation).
        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)

        X = ct.lyap(a_np, q_np, C=c_np)
        return X.tolist()
    except Exception as e:
        return f"Error: {str(e)}"

Online Calculator

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