STATE_SPACE
Creates a linear input/output system in state-space form. A continuous-time state-space model is defined by the following differential equations:
d\mathbf{x}/dt = \mathbf{A}\mathbf{x} + \mathbf{B}\mathbf{u} \mathbf{y} = \mathbf{C}\mathbf{x} + \mathbf{D}\mathbf{u}
This function accepts matrices \mathbf{A}, \mathbf{B}, \mathbf{C}, and \mathbf{D}. It returns a representation of the system dynamics that can be used for simulation and control design within the python-control library package.
Excel Usage
=STATE_SPACE(A, B, C, D)
A(list[list], required): System matrix (nxn) representing state dynamics.B(list[list], required): Control matrix (nxm) representing input effects on states.C(list[list], required): Output matrix (pxn) representing state effects on outputs.D(list[list], required): Feedforward/feedthrough matrix (pxm). For strictly proper systems, this is a zero matrix.
Returns (str): String representation of the StateSpace object (since we cannot return an object to excel directly).
Example 1: Simple SISO system
Inputs:
| A | B | C | D | ||
|---|---|---|---|---|---|
| -1 | -2 | 5 | 6 | 8 | 9 |
| 3 | -4 | 7 |
Excel formula:
=STATE_SPACE({-1,-2;3,-4}, {5;7}, {6,8}, {9})
Expected output:
"<StateSpace>: sys[0]\nInputs (1): ['u[0]']\nOutputs (1): ['y[0]']\nStates (2): ['x[0]', 'x[1]']\n\nA = [[-1. -2.]\n [ 3. -4.]]\n\nB = [[5.]\n [7.]]\n\nC = [[6. 8.]]\n\nD = [[9.]]"
Example 2: Simple Integrator (1/s)
Inputs:
| A | B | C | D |
|---|---|---|---|
| 0 | 1 | 1 | 0 |
Excel formula:
=STATE_SPACE({0}, {1}, {1}, {0})
Expected output:
"<StateSpace>: sys[1]\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 3: 2x2 MIMO System
Inputs:
| A | B | C | D | ||||
|---|---|---|---|---|---|---|---|
| -1 | 0 | 1 | 0 | 1 | 1 | 0 | 0 |
| 0 | -2 | 0 | 1 | 1 | -1 | 0 | 0 |
Excel formula:
=STATE_SPACE({-1,0;0,-2}, {1,0;0,1}, {1,1;1,-1}, {0,0;0,0})
Expected output:
"<StateSpace>: sys[2]\nInputs (2): ['u[0]', 'u[1]']\nOutputs (2): ['y[0]', 'y[1]']\nStates (2): ['x[0]', 'x[1]']\n\nA = [[-1. 0.]\n [ 0. -2.]]\n\nB = [[1. 0.]\n [0. 1.]]\n\nC = [[ 1. 1.]\n [ 1. -1.]]\n\nD = [[0. 0.]\n [0. 0.]]"
Example 4: Scalar continuous-time drift
Inputs:
| A | B | C | D |
|---|---|---|---|
| -5 | 2 | 1 | 0 |
Excel formula:
=STATE_SPACE({-5}, {2}, {1}, {0})
Expected output:
"<StateSpace>: sys[3]\nInputs (1): ['u[0]']\nOutputs (1): ['y[0]']\nStates (1): ['x[0]']\n\nA = [[-5.]]\n\nB = [[2.]]\n\nC = [[1.]]\n\nD = [[0.]]"
Python Code
Show Code
import control as ct
import numpy as np
def state_space(A, B, C, D):
"""
Create a state-space system model from system, control, output, and feedforward matrices.
See: https://python-control.readthedocs.io/en/latest/generated/control.ss.html
This example function is provided as-is without any representation of accuracy.
Args:
A (list[list]): System matrix (nxn) representing state dynamics.
B (list[list]): Control matrix (nxm) representing input effects on states.
C (list[list]): Output matrix (pxn) representing state effects on outputs.
D (list[list]): Feedforward/feedthrough matrix (pxm). For strictly proper systems, this is a zero matrix.
Returns:
str: String representation of the StateSpace object (since we cannot return an object to excel directly).
"""
try:
def to2d(x):
return [[x]] if not isinstance(x, list) else x
A = to2d(A)
B = to2d(B)
C = to2d(C)
D = to2d(D)
# Convert input lists to numpy arrays
A_arr = np.array(A, dtype=float)
B_arr = np.array(B, dtype=float)
C_arr = np.array(C, dtype=float)
D_arr = np.array(D, dtype=float)
# Basic dimension checks
if A_arr.ndim != 2 or A_arr.shape[0] != A_arr.shape[1]:
return "Error: A must be a square 2D matrix"
n = A_arr.shape[0]
if B_arr.shape[0] != n:
return "Error: B must have the same number of rows as A"
if C_arr.shape[1] != n:
return "Error: C must have the same number of columns as A"
m = B_arr.shape[1] if B_arr.ndim == 2 else 1
p = C_arr.shape[0] if C_arr.ndim == 2 else 1
expected_d_shape = (p, m)
if D_arr.shape != expected_d_shape:
return f"Error: D must have shape {expected_d_shape}"
sys = ct.ss(A_arr, B_arr, C_arr, D_arr)
return str(sys)
except Exception as e:
return f"Error: {str(e)}"