In [20]:
import numpy as np
import sympy as sp
from sympy import symbols, apart, inverse_laplace_transform, simplify
from sympy import expand, apart, lcm, together, solve, Poly, Matrix
from sympy.abc import s, t
from scipy.signal import TransferFunction, step as sci_step, StateSpace
from scipy.optimize import fsolve
from tbcontrol.symbolic import routh
In [8]:
K_p = symbols("K_p")
K_d = symbols("K_d")
K_i = symbols("K_i")
In [5]:
In [32]:
# q1
K, K_v = symbols("K K_v")
G_s = (2 * K / s) * ((1/s) / (1-K_v/s))
# Define the equation for the poles
equation = s**2 - K_v*s
# Solve the equation for s
poles = solve(equation, s)
poles
Out[32]:
[0, K_v]
In [29]:
OS = 0.2
zeta = fsolve(lambda z: )
Out[29]:
$\displaystyle \frac{2 K}{s^{2} \left(- \frac{K_{v}}{s} + 1\right)}$
In [11]:
# q2
G_s = (s-6)/((s+2)*(s+5)**2)
L_input = 1/s
Y_s = G_s*L_input
Y_s_apart = apart(Y_s)
output_time_domain = inverse_laplace_transform(Y_s_apart, s, t)
In [15]:
output_time_domain
Out[15]:
$\displaystyle - \frac{11 t e^{- 5 t} \theta\left(t\right)}{15} - \frac{3 \theta\left(t\right)}{25} + \frac{4 e^{- 2 t} \theta\left(t\right)}{9} - \frac{73 e^{- 5 t} \theta\left(t\right)}{225}$
In [17]:
# q3
In [ ]:
# q4
In [ ]:
# q5
In [22]:
# q6
A = np.array([[1, 1, 1], [0, 1, 1], [3, -2, 4]])
B = np.array([[1], [0], [1]])
C = np.array([[4, 5, 2]])
D = np.array([[0]])
# Calculate poles and zeros
system = StateSpace(A, B, C, D)
poles = system.poles
zeros = system.zeros
/Users/aarnphm/.pyenv/versions/3.11.2/lib/python3.11/site-packages/scipy/signal/_filter_design.py:1746: BadCoefficients: Badly conditioned filter coefficients (numerator): the results may be meaningless warnings.warn("Badly conditioned filter coefficients (numerator): the "
In [27]:
poles
Out[27]:
array([4.52510225+0.j , 0.73744887+0.8843676j, 0.73744887-0.8843676j])
In [25]:
zeros
Out[25]:
array([0.75+2.10653744j, 0.75-2.10653744j])
In [7]:
def q10_prac():
K_p = symbols("K_p")
G_s = 5/((s+1)**2)
G_PID_s = K_p * (1 + 5/s + 0.2*s)
open_loop = G_s * G_PID_s
char_eq = 1 + open_loop
expanded = char_eq.expand().collect(s)
char_poly = Poly(expanded, s)
coeffs = char_poly.all_coeffs()
return [solve(it, K_p) for i, it in enumerate(coeffs)]
In [ ]: