how to plot euler angles in a 3d graph for each t from an ode45 solved set of 6 odes containing phi, psi and theta and their derivatives in matlab

Assuming you have solved the set of 6 ODEs containing $\phi$, $\psi$, $\theta$ and their respective derivatives using ode45 in Matlab, you can obtain the orientation of your body frame with respect to the inertial frame by constructing the direction cosine matrix (DCM) at each time step. Here is an example code that you can use:

main.m
% define the initial conditions and time span
x0 = [0 0 0 0 0 0]; % initial values for phi, psi, theta and their derivatives
tspan = [0 10]; % time interval to solve the ODEs

% solve the ODEs
[t, x] = ode45(@odefun, tspan, x0);

% function handle for the ODEs
function dxdt = odefun(t, x)
    % define constants
    k1 = 1;
    k2 = 2;
    
    % define state variables
    phi = x(1);
    psi = x(2);
    theta = x(3);
    phidot = x(4);
    psidot = x(5);
    thetadot = x(6);
    
    % calculate DCM components
    R11 = cos(psi)*cos(theta);
    R12 = cos(psi)*sin(theta)*sin(phi) - sin(psi)*cos(phi);
    R13 = cos(psi)*sin(theta)*cos(phi) + sin(psi)*sin(phi);
    R21 = sin(psi)*cos(theta);
    R22 = sin(psi)*sin(theta)*sin(phi) + cos(psi)*cos(phi);
    R23 = sin(psi)*sin(theta)*cos(phi) - cos(psi)*sin(phi);
    R31 = -sin(theta);
    R32 = cos(theta)*sin(phi);
    R33 = cos(theta)*cos(phi);
    
    % construct the DCM
    R = [R11 R12 R13; R21 R22 R23; R31 R32 R33];
    
    % calculate the angular velocity vector in body frame
    omega = [phidot; psidot; thetadot];
    
    % calculate the angular velocity vector in inertial frame
    omega_inertial = R*omega;
    
    % calculate the Euler angles from the DCM
    theta_euler = asin(-R(3,1));
    phi_euler = atan2(R(3,2)/cos(theta_euler), R(3,3)/cos(theta_euler));
    psi_euler = atan2(R(2,1)/cos(theta_euler), R(1,1)/cos(theta_euler));
    
    % assign the derivatives
    dphidt = k1*psidot*thetadot;
    dpsidt = k2*phidot*thetadot;
    dthetadt = phidot*psidot - k1*sin(theta);
    domegadt = [dphidt; dpsidt; dthetadt];
    
    % assign the state derivatives
    dxdt = [dphidt; dpsidt; dthetadt; domegadt];
end
1693 chars
56 lines

Once you have the Euler angles calculated at each time step, you can use the plot3 function in Matlab to plot them in a 3D graph. Here is an example code that you can use:

main.m
% plot Euler angles in 3D
figure;
plot3(phi_euler, psi_euler, theta_euler);
xlabel('\phi');
ylabel('\psi');
zlabel('\theta');
title('Euler angles vs. time');
158 chars
8 lines

This will give you a 3D graph of the Euler angles $\phi$, $\psi$, and $\theta$ as a function of time.

gistlibby LogSnag