In [1]:
%%capture
!uv pip install tbcontrol
In [2]:
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 [3]:
K_p = symbols("K_p")
K_d = symbols("K_d")
K_i = symbols("K_i")
In [ ]:
In [4]:
# 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[4]:
[0, K_v]
In [5]:
OS = 0.2
In [6]:
# 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)
output_time_domain
Out[6]:
$\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 [ ]:
In [ ]:
# q3
In [ ]:
# q4
In [ ]:
# q5
In [ ]:
# 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
zeros, poles
In [ ]:
In [ ]:
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)]