Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Farbod02 patch 1 #7

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions Python Equivalence/Chapter 3/DCmotor.py
Original file line number Diff line number Diff line change
@@ -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()
26 changes: 26 additions & 0 deletions Python Equivalence/Chapter 3/DCmotor_transfun.py
Original file line number Diff line number Diff line change
@@ -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)
31 changes: 31 additions & 0 deletions Python Equivalent/Chapter 5_6/exp5_3.py
Original file line number Diff line number Diff line change
@@ -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))
33 changes: 33 additions & 0 deletions Python Equivalent/Chapter 5_6/exp5_4.py
Original file line number Diff line number Diff line change
@@ -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)
9 changes: 9 additions & 0 deletions Python Equivalent/Chapter 5_6/exp_5_6.py
Original file line number Diff line number Diff line change
@@ -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)