A MATLAB simulation for visualizing rigid body dynamics using quaternion-based attitude parameterization. This project demonstrates torque-free rotation of a rigid body (cube) with configurable inertia properties, featuring real-time 3D visualization and spherical trajectory analysis.
- Quaternion Kinematics: Uses quaternions for singularity-free attitude representation
- RK4 Integration: Fourth-order Runge-Kutta numerical integration for accurate dynamics
- General Inertia Tensor: Supports full 3x3 inertia tensors with off-diagonal products of inertia
- Euler's Equations: Implements torque-free rigid body dynamics
- Animated rotating cube with color-coded faces
- Principal axes (X, Y, Z) displayed in red, green, and blue
- Instantaneous rotation axis (dashed black line)
- Angular momentum vector (magenta dash-dot line, fixed in inertial space)
- Live quaternion and time display
- Motion of principal axes traced on a unit sphere
- Real-time path tracking for all three principal axes
- Synchronized cube visualization within the sphere
- Time slider for playback control
- Play/pause animation controls
- Axis isolation buttons (view individual or all axes)
- Angular momentum direction indicator
- X, Y, and Z components of each principal axis over time
- Reference lines showing angular momentum components
- Three separate figures for comprehensive analysis
simulation.m- Main simulation script with all visualization featuresderivatives.m- Computes state derivatives (quaternion kinematics and Euler's equations)quat2dcm.m- Converts quaternions to direction cosine matrices (rotation matrices)
- MATLAB (tested with recent versions)
- No additional toolboxes required
Simply run the main script:
simulationEdit the parameters section in simulation.m:
% Inertia tensor (3x3 symmetric matrix)
I_tensor = [5, 0, 0; % Ixx, Ixy, Ixz
0, 5, 0; % Iyx, Iyy, Iyz
0, 0, 5]; % Izx, Izy, Izz
% Initial angular velocity [rad/s]
w0 = [0., 0.5, 3.]; % [wx, wy, wz]
% Initial orientation (quaternion, scalar first)
q0 = [1, 0, 0, 0]; % Identity orientation
% Time parameters
tspan = [0 10]; % Simulation duration [seconds]
dt = 0.01; % Time step [seconds]The code includes commented examples demonstrating different dynamics:
-
Intermediate Axis Theorem (unstable rotation):
I_tensor = [1, 0, 0; 0, 5, 0; 0, 0, 3]; w0 = [0.5, 0, 3.];
-
Principal Axis Rotation (stable):
I_tensor = [1, 0, 0; 0, 5, 0; 0, 0, 3]; w0 = [0, 0, 3.];
-
Symmetric Body Precession:
I_tensor = [5, 0, 0; 0, 5, 0; 0, 0, 5]; w0 = [0.5, 0, 3.];
- Time Slider: Scrub through the simulation timeline
- Play/Pause Button: Animate the trajectory automatically
- Axis Buttons:
- "All Axes" - Show all three principal axes
- "X Only" - Isolate X-axis trajectory
- "Y Only" - Isolate Y-axis trajectory
- "Z Only" - Isolate Z-axis trajectory
The orientation evolution is governed by:
dq/dt = 0.5 * q ⊗ [0; ω]
where q is the quaternion and ω is the angular velocity vector.
For torque-free motion with a general inertia tensor:
I * dω/dt + ω × (I * ω) = 0
This is solved for the angular acceleration:
dω/dt = -I⁻¹ * (ω × (I * ω))
- Angular Momentum: Constant in inertial frame (visualized as fixed magenta vector)
- Rotational Energy: Conserved throughout the simulation
- Quaternion Normalization: Maintained at each time step
The simulation generates 6 figures:
- Rotating Cube - 3D animation of the cube with axes
- Live Spherical Representation - Real-time sphere plot during animation
- Interactive Spherical Representation - Sphere plot with time controls
- X Components - Time history of X-components of all principal axes
- Y Components - Time history of Y-components of all principal axes
- Z Components - Time history of Z-components of all principal axes
This simulation beautifully demonstrates several fundamental concepts:
- Polhodes: The curves traced by principal axes on the sphere
- Precession and Nutation: Visible when the body has asymmetric inertia
- Intermediate Axis Theorem: Rotation about the intermediate principal axis is unstable
- Conservation Laws: Angular momentum remains fixed in space
- Initialize state with quaternion and angular velocity
- Integrate equations of motion using RK4
- Normalize quaternion to prevent drift
- Store state history
- Convert quaternions to rotation matrices (DCM)
- Transform cube vertices and axes
- Update graphics objects
- Synchronize multiple figure windows
updateMarkers()- Updates sphere plot based on slider positiontogglePlayback()- Controls animation playbackanimationTimerCallback()- Timer function for smooth animationshowAll(),showXOnly(), etc. - Axis isolation controls
- Adjust
dtfor accuracy vs. speed trade-off - Use smaller time steps (
dt = 0.001) for highly asymmetric inertia tensors - The animation speed can be modified by changing the
pause()duration in the main loop - Close figure windows to stop timers and free memory
Feel free to use and modify this code for educational and research purposes.
Created for visualizing and understanding rigid body dynamics, quaternion kinematics, and rotational motion.