syms u v w p q r phi theta psi Fx Fy Fz L M N t1 t2 t3 h real
x = [u; v; w; p; q; r; phi; theta; psi; x1; y1; z1];
u_vec = [Fx; Fy; Fz; L; M; N; t1; t2; t3];
-g*cos(theta)*cos(psi)+Fx/(m-(0.5*t1 + 0.5*t2 + 0.5*t3))-0.016*u^2-q*w+r*v;
-g*(sin(phi)*sin(theta)*cos(psi)-cos(phi)*sin(psi))+(Fy/(m-(0.5*t1 + 0.5*t2 + 0.5*t3)))-0.16*v^2-r*u+ p*w;
-g*(cos(phi)*sin(theta)*cos(psi)+sin(phi)*sin(psi))+(Fz/(m-(0.5*t1 + 0.5*t2 + 0.5*t3)))-0.16*w^2-p*v + q*u;
p + (q*sin(phi) + r*cos(phi)) * tan(theta);
(q*sin(phi) + r*cos(phi)) / cos(theta);
u*cos(theta)*cos(psi)+u*cos(theta)*sin(psi)-u*sin(theta);
v*(cos(phi)*cos(psi)-cos(phi)*sin(psi)-sin(psi));
w*(-cos(phi)*cos(theta)+cos(phi)*sin(theta)+sin(phi))
A_matrix = jacobian(f, x);
B_matrix = jacobian(f, u_vec);
x_equilibrium = [0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0];
u_equilibrium = [0; 0; 0; 0; 0; 0; 0; 0; 0];
A_eq = subs(A_matrix, x, x_equilibrium);
A_eq = subs(A_eq, u_vec, u_equilibrium);
A_eq = subs(A_eq, [m, Ix, Iy, Iz, g,phi,theta,psi],[m_val, Ix_val, Iy_val, Iz_val, g_val,phi_val,theta_val,psi_val]);
B_eq = subs(B_matrix, [x; u_vec], [x_equilibrium; u_equilibrium]);
B_eq = subs(B_eq, [m, Ix, Iy, Iz, g,phi,theta,psi],[m_val, Ix_val, Iy_val, Iz_val, g_val,phi_val,theta_val,psi_val]);
controllability = ctrb(A, B);
rank_controllability = rank(controllability);
Q = diag([280,14,14,1,6,6,1,1,1,50,115,115]);
R = diag([0.052,0.06,0.06,2,2,2,1,1,1]);
x_init = [0; 0; 0; 0; 0.7; 0.5; 0; 0; 0; 9; 0; 0];
function dx = ode(t, x, A, B, K)
[t, x] = ode45(@(t, x) ode(t, x, A, B, K), t, x_init);
plot(t, x), grid on, xlabel('t')
legend('x_1', 'x_2', 'x_3', 'x_4', 'x_5', 'x_6', 'x_7', 'x_8', 'x_9', 'x_{10}', 'x_{11}', 'x_{12}')