diff --git a/Python Equivalence/Chapter 3/DCmotor.py b/Python Equivalence/Chapter 3/DCmotor.py new file mode 100644 index 0000000..0948923 --- /dev/null +++ b/Python Equivalence/Chapter 3/DCmotor.py @@ -0,0 +1,30 @@ +import numpy as np +import matplotlib.pyplot as plt +from scipy.signal import lsim + +# Define system matrices +A = np.array([[0, 1, 0], [0, 0, 4.438], [0, -12, -24]]) +b1 = np.array([[0], [0], [20]]) +b2 = np.array([[0], [-7.396], [0]]) +B = np.hstack((b1, b2)) +C = np.array([[1, 0, 0], [0, 1, 0]]) +D = 0 + +# Time vector +t = np.arange(0, 4, 0.01) + +# Define the input signal +u1 = 3 - 6 * square(2 * np.pi * 4 * t) + +# Perform simulation +t, y, x = lsim((A, B, C, D), U=u1, T=t) + +# Plot the results +plt.plot(t, x[:, 0], 'k', label='theta') +plt.plot(t, x[:, 1], 'k-.', label='omega') +plt.plot(t, x[:, 2], 'k:', label='i') +plt.grid(True) +plt.xlabel('Time (sec)') +plt.ylabel('State variable') +plt.legend() +plt.show() diff --git a/Python Equivalence/Chapter 3/DCmotor_transfun.py b/Python Equivalence/Chapter 3/DCmotor_transfun.py new file mode 100644 index 0000000..1459703 --- /dev/null +++ b/Python Equivalence/Chapter 3/DCmotor_transfun.py @@ -0,0 +1,26 @@ +import numpy as np +import control as ctrl + + +# Define system matrices +A = np.array([[0, 1, 0], [0, 0, 4.438], [0, -12, -24]]) +b1 = np.array([[0], [0], [20]]) +b2 = np.array([[0], [-7.396], [0]]) +B = np.hstack((b1, b2)) +C = np.array([[1, 0, 0]]) +D = np.array([[0, 0]]) + +# Create state-space system +DCM = ctrl.ss(A, B, C, D) + +# Convert to transfer function +DCM_tf = ctrl.ss2tf(DCM) + +# Convert to zero-pole-gain +DCM_zpk = ctrl.ss2zpk(DCM) + +# Print the results +print("Transfer Function:") +print(DCM_tf) +print("\nZero-Pole-Gain:") +print(DCM_zpk) diff --git a/Python Equivalent/Chapter 5_6/exp5_3.py b/Python Equivalent/Chapter 5_6/exp5_3.py new file mode 100644 index 0000000..e1885b4 --- /dev/null +++ b/Python Equivalent/Chapter 5_6/exp5_3.py @@ -0,0 +1,31 @@ +import numpy as np +A = np.array([[-3/2, 1/2], [1/2, -3/2]]) +B = np.array([[1/2], [1/2]]) +C = np.array([[1, -1]]) +def ctrb(A, B): + n = A.shape[0] + m = B.shape[1] + C = np.hstack([np.linalg.matrix_power(A, i) @ B for i in range(n)]) + return C +def obsv(A, C): + n = A.shape[0] + m = C.shape[1] + O = np.vstack([C @ np.linalg.matrix_power(A, i) for i in range(n)]) + return O +def null_space(A, tol=1e-15): + u, s, vh = np.linalg.svd(A) + null_mask = (s <= tol) + null_space = np.compress(null_mask, vh, axis=0) + return null_space.T +Cn = ctrb(A, B) +print("Controllability Matrix:") +print(Cn) +print("Rank of Controllability Matrix:", np.linalg.matrix_rank(Cn)) +print("Null space of Controllability Matrix:") +print(null_space(Cn)) +On = obsv(A, C) +print("\nObservability Matrix:") +print(On) +print("Rank of Observability Matrix:", np.linalg.matrix_rank(On)) +print("Null space of Observability Matrix:") +print(null_space(On)) diff --git a/Python Equivalent/Chapter 5_6/exp5_4.py b/Python Equivalent/Chapter 5_6/exp5_4.py new file mode 100644 index 0000000..b2d165e --- /dev/null +++ b/Python Equivalent/Chapter 5_6/exp5_4.py @@ -0,0 +1,33 @@ +import numpy as np +num = [[1, 2], [-1, 1]] +den = [[1, 1], [1, 2], [1, 1], [1, 3]] +def nested_lists_to_arrays(nested_list): + return [np.array(coeff) for coeff in nested_list] +num_np = nested_lists_to_arrays(num) +den_np = nested_lists_to_arrays(den) +def tf(num, den): + num_poly = np.poly1d(num) + den_poly = np.poly1d(den) + return num_poly, den_poly +num_tf, den_tf = tf(num_np[0], den_np[0]) +print("Transfer Function (num/den):") +print(num_tf) +print("---") +print(den_tf) +A = np.array([[-1]]) +B = np.array([[1], [2]]) +C = np.array([[1, 2]]) +D = np.array([[0]]) +def ss2tf(A, B, C, D): + return np.linalg.inv(s*np.eye(A.shape[0]) - A) @ B + D +s = np.array([[0, 1], [-1, 0]]) +my_sys = np.array([[1/(s+1), 2/(s+2)], [-1/(s+1), 1/(s+3)]]) +A_ss = np.array([[0, 1], [-1, -4]]) +B_ss = np.array([[0], [0], [1]]) +C_ss = np.array([[1, 2]]) +D_ss = np.array([[0]]) +print("\nState-Space Matrices:") +print("A_ss:\n", A_ss) +print("B_ss:\n", B_ss) +print("C_ss:\n", C_ss) +print("D_ss:\n", D_ss) diff --git a/Python Equivalent/Chapter 5_6/exp_5_6.py b/Python Equivalent/Chapter 5_6/exp_5_6.py new file mode 100644 index 0000000..097f233 --- /dev/null +++ b/Python Equivalent/Chapter 5_6/exp_5_6.py @@ -0,0 +1,9 @@ +import numpy as np +from scipy.linalg import solve_continuous_lyapunov +A = np.array([[-1, -2], [1, -4]]) +Q = np.eye(2) +P = solve_continuous_lyapunov(A.T, -Q) +det_P = np.linalg.det(P) +print("Matrix P (Lyapunov equation solution):") +print(P) +print("\nDeterminant of P:", det_P) \ No newline at end of file