6import matplotlib.pyplot
as plt
7from scipy.linalg
import solve_continuous_are
22A = np.array([[-R / L, -Ke / L, 0], [Kt / J, -B / J, 0], [0, 1, 0]])
23B = np.array([[1 / L], [0], [0]])
24C = np.array([[0, 0, 1]])
29Q = np.diag([10, 1, 100])
30R_mat = np.array([[0.01]])
32P = solve_continuous_are(A, B, Q, R_mat)
33K = np.linalg.inv(R_mat) @ B.T @ P
34print(
"LQR Gain K =", K)
42t = np.linspace(0, T, N)
47x_hist, u_hist = [], []
51 u = -K @ x + K[0, 2] * r
55 x_hist.append(x.flatten())
56 u_hist.append(u.item())
58x_hist = np.array(x_hist)
59u_hist = np.array(u_hist)
64plt.figure(figsize=(10, 6))
67plt.plot(t, x_hist[:, 2], label=
"θ (position)")
68plt.axhline(r, color=
"r", linestyle=
"--", label=
"reference")
69plt.ylabel(
"Position [rad]")
73plt.plot(t, x_hist[:, 1])
74plt.ylabel(
"Angular Velocity [rad/s]")
78plt.ylabel(
"Control Voltage [V]")