<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Not we characterize how the controller is actually implemented.
Step1:  We want to find the gains in the designed controller that give the LQR gains.
Step2:  Here, we solve for the gains in the designed controller to match the LQR gains.
Step3:  We see that the solution matches the LQR solution at the trim conditions.
<ASSISTANT_TASK:>
Python Code:
P_r, Q_r, R_r, k_P, k_Q, k_R, k_theta, k_phi, k_alpha, k_dh, k_ah, dh_r, V_T_r, k_V_thr, k_rdr_beta, k_ail_beta, k_alpha_thr, alpha_r = \
    sympy.symbols('P_r, Q_r, R_r, k_P, k_Q, k_R, k_theta, k_phi, k_alpha, k_dh, k_ah, dh_r, V_T_r, k_V_thr, k_rdr_beta, k_ail_beta, k_alpha_thr, alpha_r')
phi_r = k_P *(P_r - P)
ail_r = k_phi*(phi_r - phi) + k_ail_beta*beta
theta_r = k_Q *(Q_r - Q)
elv_r = k_theta*(theta_r - theta) - k_alpha*(alpha_r - alpha)
rdr_r = k_R *(R_r - R) + k_rdr_beta*beta
thr_r = -k_dh*(dh_r - V_T*sympy.sin(theta - alpha)) + k_V_thr*(V_T_r - V_T) - k_alpha_thr*(alpha_r - alpha)
u_r = sympy.Matrix([elv_r, thr_r, ail_r, rdr_r])
u_r
u_vect.T, x_vect.T
sub_trim = {x_vect[i]: x0[i] for i in range(len(x_vect))}
sub_trim.update({u_vect[i]: u0[i] for i in range(len(u_vect))})
sub_trim
u_r = sympy.Matrix([elv_r, thr_r, ail_r, rdr_r])
K_syms = -u_r.jacobian(x_vect).subs(sub_trim)
K_syms
Explanation: Not we characterize how the controller is actually implemented.
End of explanation
K_diff = list(pl.where(abs(K) < 1e-1, pl.zeros(K.shape), K) - K_syms)
K_eqs = sympy.Matrix([ K_diff[i] for i in pl.nonzero(K_diff)[0]])
K_eqs
len(K_eqs), len(K_eqs.atoms(sympy.Symbol))
Explanation: We want to find the gains in the designed controller that give the LQR gains.
End of explanation
sol_K = sympy.solve(K_eqs)[0]
sol_K
Explanation: Here, we solve for the gains in the designed controller to match the LQR gains.
End of explanation
K_d = pl.array(K_syms.subs(sol_K)).astype(float)
K_d
K_clean
pl.eig(ss_lin.A - ss_lin.B.dot(K_d))[0]
pl.eig(ss_lin.A - ss_lin.B.dot(K_clean))[0]
pl.eig(ss_lin.A - ss_lin.B.dot(K))[0]
Explanation: We see that the solution matches the LQR solution at the trim conditions.
End of explanation
<END_TASK>