From b10f2af6e614b02c2753cd1dd836ca71e60b9745 Mon Sep 17 00:00:00 2001 From: IguanaAzul Date: Thu, 12 Nov 2020 17:23:29 -0300 Subject: [PATCH] fixed StateSpace for complex inputs --- control/statesp.py | 8 +++--- examples/diagonalize_space_state.py | 42 +++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 4 deletions(-) create mode 100644 examples/diagonalize_space_state.py diff --git a/control/statesp.py b/control/statesp.py index dd0ea6f5e..99b6a8a31 100644 --- a/control/statesp.py +++ b/control/statesp.py @@ -70,7 +70,7 @@ # Define module default parameter values _statesp_defaults = { - 'statesp.use_numpy_matrix': True, + 'statesp.use_numpy_matrix': False, 'statesp.default_dt': None, 'statesp.remove_useless_states': True, } @@ -98,11 +98,11 @@ def _ssmatrix(data, axis=1): # Convert the data into an array or matrix, as configured # If data is passed as a string, use (deprecated?) matrix constructor if config.defaults['statesp.use_numpy_matrix']: - arr = np.matrix(data, dtype=float) + arr = np.matrix(data, dtype=complex) elif isinstance(data, str): - arr = np.array(np.matrix(data, dtype=float)) + arr = np.array(np.matrix(data, dtype=complex)) else: - arr = np.array(data, dtype=float) + arr = np.array(data, dtype=complex) ndim = arr.ndim shape = arr.shape diff --git a/examples/diagonalize_space_state.py b/examples/diagonalize_space_state.py new file mode 100644 index 000000000..c47bd36d8 --- /dev/null +++ b/examples/diagonalize_space_state.py @@ -0,0 +1,42 @@ +from matplotlib import pyplot as plt + +import numpy as np +from scipy.linalg import eig +from control import step_response, StateSpace +# Example of diagonalization of StateSpace representation +# In this example the diagonalized system has complex values on the matrices + +def diagonalize(A, B, C, D): + eigenvectors_matrix = eig(A)[1] + A = np.matmul(np.matmul(np.linalg.inv(eigenvectors_matrix), A), eigenvectors_matrix) + B = np.matmul(np.linalg.inv(eigenvectors_matrix), B) + C = np.matmul(C, eigenvectors_matrix) + print(A) + print(B) + print(C) + print(D) + return A, B, C, D + + +def main(): + A = np.array([[0, 0, 1000], [0.1, 0, 0], [0, 0.5, 0.5]]) + B = np.array([[0], [0], [1]]) + C = np.array([[0, 0, 1]]) + D = np.array([[0]]) + + Aw, Bw, Cw, Dw = diagonalize(A, B, C, D) + + system = StateSpace(A, B, C, D, 1) + system_w = StateSpace(Aw, Bw, Cw, Dw, 1) + + length = 20 + plt.figure(figsize=(14, 9), dpi=100) + response_w = step_response(system_w, T=np.arange(length)) + response = step_response(system, T=np.arange(length)) + plt.plot(response[0], response[1], color="red", label="System") + plt.plot(response_w[0], response_w[1], color="blue", label="System W") + plt.xticks(np.arange(length)) + plt.legend() + plt.savefig("diagonalization.png") + +main() \ No newline at end of file