TF2SS

Converts a continuous-time or discrete-time polynomial transfer function into an equivalent state-space representation.

A state-space model provides a time-domain representation of the system dynamics using a set of first-order differential (or difference) equations, defined by the matrices \mathbf{A}, \mathbf{B}, \mathbf{C}, and \mathbf{D}.

Because state-space realizations are not unique (the same transfer function can be represented by infinitely many state-space models depending on the choice of state variables), this function returns a generic realization created by the slycot or scipy backend methods.

Excel Usage

=TF2SS(num, den)
  • num (list[list], required): Polynomial coefficients of the numerator, in descending powers of s.
  • den (list[list], required): Polynomial coefficients of the denominator, in descending powers of s.

Returns (str): String representation of the resulting state-space model.

Example 1: 1/(s+1) -> State-Space

Inputs:

num den
1 1 1

Excel formula:

=TF2SS({1}, {1,1})

Expected output:

"<StateSpace>: sys[2]\nInputs (1): ['u[0]']\nOutputs (1): ['y[0]']\nStates (1): ['x[0]']\n\nA = [[-1.]]\n\nB = [[1.]]\n\nC = [[1.]]\n\nD = [[0.]]"

Example 2: (s+2)/(s^2+3s+2) -> State-Space

Inputs:

num den
1 2 1 3 2

Excel formula:

=TF2SS({1,2}, {1,3,2})

Expected output:

"<StateSpace>: sys[6]\nInputs (1): ['u[0]']\nOutputs (1): ['y[0]']\nStates (2): ['x[0]', 'x[1]']\n\nA = [[-3. -2.]\n [ 1. 0.]]\n\nB = [[1.]\n [0.]]\n\nC = [[1. 2.]]\n\nD = [[0.]]"

Example 3: 1/s -> State space

Inputs:

num den
1 1 0

Excel formula:

=TF2SS({1}, {1,0})

Expected output:

"<StateSpace>: sys[10]\nInputs (1): ['u[0]']\nOutputs (1): ['y[0]']\nStates (1): ['x[0]']\n\nA = [[-0.]]\n\nB = [[1.]]\n\nC = [[1.]]\n\nD = [[0.]]"

Example 4: Third order system

Inputs:

num den
2 1 1 4 5 2

Excel formula:

=TF2SS({2,1}, {1,4,5,2})

Expected output:

"<StateSpace>: sys[14]\nInputs (1): ['u[0]']\nOutputs (1): ['y[0]']\nStates (3): ['x[0]', 'x[1]', 'x[2]']\n\nA = [[-4. -5. -2.]\n [ 1. 0. 0.]\n [ 0. 1. 0.]]\n\nB = [[1.]\n [0.]\n [0.]]\n\nC = [[0. 2. 1.]]\n\nD = [[0.]]"

Python Code

Show Code
import control as ct
import numpy as np

def tf2ss(num, den):
    """
    Convert a transfer function object back into a state-space system object.

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

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

    Args:
        num (list[list]): Polynomial coefficients of the numerator, in descending powers of s.
        den (list[list]): Polynomial coefficients of the denominator, in descending powers of s.

    Returns:
        str: String representation of the resulting state-space model.
    """
    try:
        def flatten_to_1d(x):
            if not isinstance(x, list):
                return [float(x)] if str(x) != "" else []
            flat = []
            for row in x:
                if isinstance(row, list):
                    for val in row:
                        if str(val) != "":
                            flat.append(float(val))
                elif str(row) != "":
                    flat.append(float(row))
            return flat

        num_arr = flatten_to_1d(num)
        den_arr = flatten_to_1d(den)

        if not num_arr or not den_arr:
            return "Error: Numerator and denominator coefficients must be provided"

        if all(d == 0 for d in den_arr):
            return "Error: Denominator cannot be zero"

        try:
            sys_ss = ct.tf2ss(num_arr, den_arr, method='scipy')
        except TypeError:
            sys_ss = ct.tf2ss(num_arr, den_arr)

        # For Excel output, we must return a rectangular array. Let's return the string representations.
        return str(sys_ss)
    except Exception as e:
        return f"Error: {str(e)}"

Online Calculator

Polynomial coefficients of the numerator, in descending powers of s.
Polynomial coefficients of the denominator, in descending powers of s.