function part_c()
I1=1;
I3=.5;
w =1;
%initial conditions
Y0 = [0; pi/4; 0; 0; 0; 1]
tspan =[0 10]
%solve IDE
options = odeset('RelTol', 1e-8, 'AbsTol', 1e-8);
[t, Y] = ode45(@(t, Y) eom(t, Y, I1, I3, w), tspan, Y0, options);
%plots
figure;
plot(t, Y(:,1), 'r'); hold on
plot(t, Y(:,2), 'g');
plot(t, Y(:,3), 'b');
end
function dYdt = eom(~, Y, I1, I3, w)
phi = Y(1)
theta = Y(2)
psi = Y(4)
phi_d = Y(4);
theta_d = Y(5);
psi_d = Y(6);
w_phi = w+phi_d;
sin_th=sin(theta);
cos_th=cos(theta);
p_psi = I3* (w_phi * cos_th + psi_d);
%might result in dividing by zero so need to prevent that
if abs(sin_th) < 1e-12
sin_th =sign(sin_th)*1e-12;
if sin_th == 0; sin_th = 1e-12;
end
end
%
% Theta double dot
theta_dd = ( I1 * w_phi^2 * sin_th * cos_th - p_psi * w_phi * sin_th + 3 * w^2 * (I3 - I1) *
sin_th * cos_th * sin(phi)^2 ) / I1;
% Phi double dot
phi_dd = ( -2 * I1 * w_phi * theta_d * sin_th * cos_th + p_psi * theta_d * sin_th + 3 *
w^2 * (I3 - I1) * sin_th^2 * sin(phi) * cos(phi) ) / (I1 * sin_th^2);
% Psi double dot
psi_dd = -phi_dd * cos_th + w_phi * theta_d * sin_th;
% ode45 output
dYdt = [phi_d; theta_d; psi_d; phi_dd; theta_dd; psi_dd];
end