MATLAB Code Project 01
MATLAB Code Project 01
% 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
% 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;
% Save results
disp('Calculation and plotting complete.');
**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
- **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
- **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);
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:
figure;
fplot(L_mag, 'LineWidth', 1.5);
xlabel('Time (t)');
ylabel('|L(t)|');
title('Angular Momentum as a Function of Time');
grid on;
6. Completion Message
- **Purpose**: Informs the user that the script has finished execution.
- **Functionality**:
- `disp`: Displays a message to the Command Window.
Execution Flow
Applications