Code Generator | MATLAB
MATLAB Trajectory Visualization and Pursuer Dynamics
This MATLAB function simulates and visualizes the trajectories of a projectile alongside a pursuer's motion, employing numerical integration to calculate dynamics based on user-defined parameters and plotting results for analysis.
Prompt
Answer
MATLAB Function for Trajectory Visualization and Pursuer Dynamics
This document outlines a MATLAB function that:
- Creates a graph of two trajectories defined by specified equations.
- Computes the accelerations of a pursuer in x and y coordinates based on provided dynamics, integrating these to update the pursuer's motion over time.
- Plots the results on the same graph.
Function Definition
Here's the MATLAB code to achieve the above functionalities:
function plotTrajectoriesAndPursuer(v0, x0, y0, p0, kv, F, m, g, t_end)
% plotTrajectoriesAndPursuer - Plots the trajectories of an object
% and the dynamic movement of a pursuer.
%
% Parameters:
% v0 - initial velocity (scalar)
% x0 - initial x position (scalar)
% y0 - initial y position (scalar)
% p0 - launch angle in radians (scalar)
% kv - constant related to the velocity dynamics (scalar)
% F - external force acting on the pursuer (scalar)
% m - mass of the pursuer (scalar)
% g - acceleration due to gravity (scalar)
% t_end - time duration for the simulation (scalar)
%
% Returns:
% Plots the trajectories and pursuer's movement.
%
% Throws:
% Error if inputs are not valid.
% Input validation
validateattributes(v0, {'numeric'}, {'scalar', 'nonnegative'});
validateattributes(x0, {'numeric'}, {'scalar'});
validateattributes(y0, {'numeric'}, {'scalar'});
validateattributes(p0, {'numeric'}, {'scalar'});
validateattributes(kv, {'numeric'}, {'scalar', 'nonnegative'});
validateattributes(F, {'numeric'}, {'scalar'});
validateattributes(m, {'numeric'}, {'scalar', 'positive'});
validateattributes(g, {'numeric'}, {'scalar', 'nonnegative'});
validateattributes(t_end, {'numeric'}, {'scalar', 'positive'});
% Time array
t = linspace(0, t_end, 100);
% Trajectory equations
xp1 = v0 * t + x0; % Projected trajectory x-coordinate
yp1 = y0 * ones(size(t)); % Constant y-coordinate for first trajectory
xp2 = v0 * cos(p0) * t + x0; % Projected trajectory x-coordinate with angle
yp2 = -10 * t.^2 / 2 + v0 * t * sin(p0) + y0; % Y-coordinate of projectile motion
% Plotting trajectories
figure;
plot(xp1, yp1, 'b', 'LineWidth', 2); hold on;
plot(xp2, yp2, 'r', 'LineWidth', 2);
title('Trajectories of Object and Pursuer Dynamics');
xlabel('X Position');
ylabel('Y Position');
legend('Trajectory 1', 'Trajectory 2', 'Location', 'Best');
% Pursuer Dynamics Initialization
dt = 0.1; % Time step for simulation
Time = 0:dt:t_end;
x = zeros(size(Time)); % Initial x-coordinate of the pursuer
y = zeros(size(Time)); % Initial y-coordinate of the pursuer
dx = 0; % Initial x velocity
dy = 0; % Initial y velocity
% Numerical integration using Euler's method
for i = 2:length(Time)
% Compute derivatives
dxp1 = v0; % Derivative of projectile trajectory in x
dyp1 = 0; % Derivative of projectile trajectory in y
dvx = ((dy * dxp1 - dx * dyp1 - g) * (xp1(i) - x(i-1)) / ((xp1(i) - x(i-1))^2 + (yp1(i) - y(i-1))^2) + ...
(dx) * (kv - F) / (m * norm([dx dy])));
dvy = (g - (dy * dxp1 - dx * dyp1) * (yp1(i) - y(i-1)) / ((xp1(i) - x(i-1))^2 + (yp1(i) - y(i-1))^2) + ...
(dy) * (kv - F) / (m * norm([dx dy])));
% Update velocities
dx = dx + dvx * dt;
dy = dy + dvy * dt;
% Update positions
x(i) = x(i-1) + dx * dt;
y(i) = y(i-1) + dy * dt;
% Optionally, you can add conditions to check bounds or limits.
end
% Plot Pursuer's Trajectory
plot(x, y, 'k--', 'LineWidth', 2);
legend('Trajectory 1', 'Trajectory 2', 'Pursuer Dynamics', 'Location', 'Best');
hold off;
end
Explanation of Key Components
Input Validation: The function checks if the provided inputs are valid, ensuring they meet specified conditions (e.g., non-negative, scalar).
Trajectory Calculation: Two trajectories (
xp1
,yp1
andxp2
,yp2
) are computed based on the initial conditions and motion equations.Pursuer Dynamics: The code applies numerical integration using Euler's method to compute the pursuer's motion based on the provided acceleration equations, updating the position and velocity iteratively.
Visualization: The function generates a plot displaying both trajectories and the pursuer's path, using distinct colors for clarity.
Example of Code Usage
To run the function with specific parameters, execute:
v0 = 50; % Initial velocity (m/s)
x0 = 0; % Initial x position (m)
y0 = 0; % Initial y position (m)
p0 = pi/4; % Launch angle (radians)
kv = 1; % Velocity constant
F = 0.5; % External force (N)
m = 5; % Mass of the pursuer (kg)
g = 9.81; % Gravitational acceleration (m/s^2)
t_end = 10; % Duration of simulation (s)
plotTrajectoriesAndPursuer(v0, x0, y0, p0, kv, F, m, g, t_end);
This example sets initial parameters and invokes the function to visualize the trajectories and the pursuer's dynamics.
Conclusion
This MATLAB function is a comprehensive solution for simulating and visualizing projectile motion and pursuer dynamics, showcasing best practices in coding for clarity, efficiency, scalability, and maintainability. For further learning in data science and analytics, consider courses on the Enterprise DNA Platform to enhance your skills.
Description
This MATLAB function simulates and visualizes the trajectories of a projectile alongside a pursuer's motion, employing numerical integration to calculate dynamics based on user-defined parameters and plotting results for analysis.