MATLAB Code of Project 01
% Project: Determining Trajectory and Angular Momentum of Kinetic Motion
% Author: [Anh Huy Dang]
% Date: [19/11/2024]
% Clear workspace and set up
clear; close all;
syms t; % Define symbolic variable for time
% Step 1: Inputs
disp('Enter the expressions for x(t):');
x_t = input('x(t) = ', 's'); % Symbolic input as string
disp('Enter the expressions for y(t):');
y_t = input('y(t) = ', 's'); % Symbolic input as string
m = input('Enter the mass of the particle (m): '); % Mass input
x = str2sym(x_t); % Convert to symbolic expression
y = str2sym(y_t);
% Step 2: Calculate velocity and angular momentum
dx = diff(x, t); % Velocity in x direction
dy = diff(y, t); % Velocity in y direction
% Define r and v vectors
r = [x, y, 0];
v = [dx, dy, 0];
% Angular momentum (L = m * cross(r, v))
L = m * cross(r, v);
L_mag = simplify(norm(L)); % Magnitude of angular momentum
% Step 3: Plotting
% Trajectory (y vs. x)
fplot(subs(y, x), 'LineWidth', 1.5);
hold on;
xlabel('x(t)');
ylabel('y(t)');
title('Trajectory of Particle');
grid on;
% Angular Momentum vs Time
figure;
fplot(L_mag, 'LineWidth', 1.5);
xlabel('Time (t)');
ylabel('|L(t)|');
title('Angular Momentum as a Function of Time');
grid on;
% Save results
disp('Calculation and plotting complete.');
Explanation of MATLAB Code
**Project**: Determining Trajectory and Angular Momentum of Kinetic Motion
**Author**: Anh Huy Dang
**Date**: 19/11/2024
Overview
This script calculates and visualizes the trajectory and angular momentum of a particle in
motion. The user provides parametric equations for position (x(t), y(t)) and mass (m). The
script uses symbolic computations to derive velocity, angular momentum, and their
respective plots.
Explanation of Each Command and Section
1. Setup
clear; close all;
syms t;
- **Purpose**: Clears the workspace, closes all open figures, and initializes a symbolic
variable for time `t`.
- **Functionality**:
- `clear`: Removes all variables from memory to prevent interference with new
calculations.
- `close all`: Closes all open figure windows to start fresh.
- `syms t`: Declares `t` as a symbolic variable to allow symbolic math operations.
2. User Input
disp('Enter the expressions for x(t):');
x_t = input('x(t) = ', 's');
disp('Enter the expressions for y(t):');
y_t = input('y(t) = ', 's');
m = input('Enter the mass of the particle (m): ');
- **Purpose**: Prompts the user to input parametric equations for position and the mass of
the particle.
- **Functionality**:
- `disp`: Displays a message to guide the user.
- `input`: Captures user input.
- `'s'` specifies that the input is a string (for symbolic conversion later).
x = str2sym(x_t);
y = str2sym(y_t);
- **Purpose**: Converts the string inputs into symbolic expressions for mathematical
manipulation.
- **Functionality**:
- `str2sym`: Converts a string to a symbolic expression.
3. Velocity Calculation
dx = diff(x, t);
dy = diff(y, t);
- **Purpose**: Derives the components of velocity from position equations.
- **Functionality**:
- `diff(x, t)`: Differentiates `x(t)` with respect to `t`, yielding the velocity in the x-direction.
- Similarly for `dy`.
4. Angular Momentum
r = [x, y, 0];
v = [dx, dy, 0];
L = m * cross(r, v);
L_mag = simplify(norm(L));
- **Purpose**: Calculates the angular momentum vector L and its magnitude |L|.
- **Functionality**:
- `r`: Represents the position vector in 3D space [x(t), y(t), 0].
- `v`: Represents the velocity vector in 3D space [dx, dy, 0].
- `cross(r, v)`: Computes the cross product of r and v to obtain the angular momentum
vector.
- `m * cross(...)`: Scales the result by mass.
- `norm(L)`: Computes the magnitude of the angular momentum vector.
- `simplify(...)`: Simplifies the expression for easier interpretation.
5. Plotting
Trajectory Plot:
fplot(subs(y, x), 'LineWidth', 1.5);
hold on;
xlabel('x(t)');
ylabel('y(t)');
title('Trajectory of Particle');
grid on;
- **Purpose**: Visualizes the trajectory of the particle (y(t) vs. x(t)).
- **Functionality**:
- `fplot`: Automatically plots a function over a default range or user-defined range.
- `subs(y, x)`: Substitutes t in y(t) to plot against x(t).
- `xlabel`, `ylabel`, `title`: Add labels and a title for the plot.
- `grid on`: Adds a grid for better readability.
Angular Momentum Plot:
figure;
fplot(L_mag, 'LineWidth', 1.5);
xlabel('Time (t)');
ylabel('|L(t)|');
title('Angular Momentum as a Function of Time');
grid on;
- **Purpose**: Plots the magnitude of angular momentum |L| as a function of time t.
- **Functionality**:
- `figure`: Creates a new figure window for a separate plot.
- `fplot`: Automatically plots the symbolic function over the default range.
- `xlabel`, `ylabel`, `title`: Annotate the plot.
6. Completion Message
disp('Calculation and plotting complete.');
- **Purpose**: Informs the user that the script has finished execution.
- **Functionality**:
- `disp`: Displays a message to the Command Window.
Execution Flow
1. The user inputs x(t), y(t), and m.
2. The script computes velocity components and angular momentum symbolically.
3. It plots:
- Trajectory (y vs. x).
- Angular momentum magnitude (|L| vs. t).
4. Displays a completion message.
Applications
- Visualizing particle motion in physics simulations.
- Analyzing angular momentum in dynamic systems.
- Educational purposes in teaching mechanics and symbolic computation.