State-Space Representation and Solutions in Python

State-Space Representation and Solutions in Python

State-Space Representation

State-space representation is a powerful way to model and analyze linear systems, especially for multi-input, multi-output (MIMO) systems. In this representation, we describe the system’s behavior using:

  • State variables: A set of variables that completely describe the system’s internal state.
  • Input variables: External inputs that influence the system’s behavior.
  • Output variables: Measured outputs that reflect the system’s state.

The state-space equations are given by:

\[ \begin{aligned} \dot{x} &= Ax + Bu \\ y &= Cx + Du \end{aligned} \]

where:

  • x is the state vector
  • u is the input vector
  • y is the output vector
  • A is the state matrix
  • B is the input matrix
  • C is the output matrix
  • D is the direct feedthrough matrix

Creating State-Space Models in Python

We can create state-space models in Python using the ss function from the control package. Let’s illustrate this with an example of a mass-spring-damper system:

import control as co
import numpy as np

# System parameters
m = 1  # mass
k = 3  # spring constant
b = 2  # damping coefficient

# State matrix
A = np.array([[0,1],
              [-k/m, -b/m]])

# Input matrix
B = np.array([[0],
              [1/m]])

# Output matrix
C = np.array([1,0])

# Direct feedthrough matrix
D = np.array([0])

# Create the state-space model
sys = co.ss(A, B, C, D)

# Print the state-space model
print(sys)

Obtaining Solutions of State-Space Systems

Once we have a state-space model, we can use Python to obtain the system’s response to different inputs. One common input is the step input. We can simulate the step response using the step_response function:

import control as co
import matplotlib.pyplot as plt
import numpy as np

# Create a state-space model
sys = co.ss(A, B, C, D) # Using A,B,C,D matrices from previous example

# Simulate the step response
T, yout = co.step_response(sys)

# Plot the step response
plt.plot(T, yout)
plt.grid(True)
plt.xlabel("Time (sec)")
plt.ylabel("Output")
plt.show()

The function can also return a time-response object containing the time, input, state, and output vectors associated with the simulation. A plot() method can then plot the response. For example, the previous time plot can be generated also as follows:

sresponse = co.step_response(sys)
sresponse.plot()
plt.show()

Other Useful Functions

The control package provides many other useful functions for working with state-space models, including:

  • initial_response and matlab.init: Calculate the initial value response of a system
  • impulse_response and matlab.impulse: Calculate the impulse response of a system
  • input_output_response and matlab.lsim: Calculate the output corresponding to a given input
  • poles and matlab.pole: Calculate the poles of a system
  • zeros and matlab.zero: Calculate the zeros of a system
  • tf: Convert a state-space model to a transfer function model
  • ss2tf: Convert a state-space model to a transfer function model