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.


Empty image or helper icon

Prompt

Write a code to create a graph of two trajectories presented by following equations: 
1. xp1(t) = v0*t + x0; yp1(t) = y0
2. xp2(t) = v0*cos(p0)*t + x0; yp2(t) = -10*t^2/2 + v0*t*sin(p0) + y0.
And then add in the code the following equations of the pursuers's accelerations on x and y, integrate them twice and add to the plot, knowing that pursuer's coordinates are x and y, and there are derivatives of them of first and second order used in equations below:

d(dx/dt)/dt = ((dy/dt) * (dxp1/dt) - (dx/dt) * (dyp1/dt) - g)*(xp1 - x)/((xp1 - x)^2 + (yp1 - y)^2) + (dx/dt) * (kv - F)/(m * v)
d(dy/dt)/dt = (g - (dy/dt) * (dxp1/dt) - (dx/dt) * (dyp1/dt))*(yp1 - y)/((xp1 - x)^2 + (yp1 - y)^2) + (dy/dt) * (kv - F)/(m * v)

Answer

MATLAB Function for Trajectory Visualization and Pursuer Dynamics

This document outlines a MATLAB function that:

  1. Creates a graph of two trajectories defined by specified equations.
  2. 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.
  3. 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

  1. Input Validation: The function checks if the provided inputs are valid, ensuring they meet specified conditions (e.g., non-negative, scalar).

  2. Trajectory Calculation: Two trajectories (xp1, yp1 and xp2, yp2) are computed based on the initial conditions and motion equations.

  3. 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.

  4. 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.

Create your Thread using our flexible tools, share it with friends and colleagues.

Your current query will become the main foundation for the thread, which you can expand with other tools presented on our platform. We will help you choose tools so that your thread is structured and logically built.

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.