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 vectoru
is the input vectory
is the output vectorA
is the state matrixB
is the input matrixC
is the output matrixD
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
andmatlab.init
: Calculate the initial value response of a systemimpulse_response
andmatlab.impulse
: Calculate the impulse response of a systeminput_output_response
andmatlab.lsim
: Calculate the output corresponding to a given inputpoles
andmatlab.pole
: Calculate the poles of a systemzeros
andmatlab.zero
: Calculate the zeros of a systemtf
: Convert a state-space model to a transfer function modelss2tf
: Convert a state-space model to a transfer function model