Code Generator | MATLAB

MATLAB Trajectory and Pursuer Dynamics Plotter

This MATLAB function visualizes the trajectories of two objects based on initial conditions and computes a pursuer's dynamics influenced by gravitational and acceleration factors, offering a comprehensive analysis of motion scenarios.


Empty image or helper icon

Prompt

creates 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 adds the following equations of the pursuer's accelerations on x and y, integrates them twice and adds 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 and Pursuer's Dynamics

Overview

This MATLAB function plots two trajectories based on given equations and incorporates the dynamics of a pursuer influenced by gravitational and acceleration factors.

Functionality

  1. Trajectory Plotting: Plots the trajectories of two objects based on initial velocities and angles.
  2. Pursuer Dynamics: Computes and integrates the pursuer's velocity and position based on specified acceleration equations.

Code Implementation

function plotTrajectories(v0, p0, x0, y0, F, m, kv, g, t_end, dt)
    % plotTrajectories Plots two trajectories and a pursuer's path.
    %
    % PARAMETERS:
    %   v0   - Initial velocity (scalar)
    %   p0   - Launch angle (radians)
    %   x0   - Initial x-coordinate (scalar)
    %   y0   - Initial y-coordinate (scalar)
    %   F    - External force on the pursuer (scalar)
    %   m    - Mass of the pursuer (scalar)
    %   kv   - Velocity proportionality constant (scalar)
    %   g    - Acceleration due to gravity (scalar)
    %   t_end - End time for simulation (scalar)
    %   dt   - Time step for simulation (scalar)
    %
    % RETURNS:
    %   None
    %
    % EXCEPTIONS:
    %   Throws an error if inputs are invalid
    
    % Input validation
    validateattributes(v0, {'numeric'}, {'scalar', 'positive'});
    validateattributes(p0, {'numeric'}, {'scalar'});
    validateattributes(x0, {'numeric'}, {'scalar'});
    validateattributes(y0, {'numeric'}, {'scalar'});
    validateattributes(F, {'numeric'}, {'scalar'});
    validateattributes(m, {'numeric'}, {'scalar', 'positive'});
    validateattributes(kv, {'numeric'}, {'scalar'});
    validateattributes(g, {'numeric'}, {'scalar', 'positive'});
    validateattributes(t_end, {'numeric'}, {'scalar', 'positive'});
    validateattributes(dt, {'numeric'}, {'scalar', 'positive'});

    % Define time vector
    t = 0:dt:t_end;

    % Trajectories: First Object
    xp1 = v0 .* t + x0; % x-coordinate trajectory
    yp1 = repmat(y0, size(t)); % y-coordinate trajectory (constant)

    % Trajectories: Second Object
    xp2 = v0 * cos(p0) .* t + x0; % x-coordinate trajectory
    yp2 = -(10 * t.^2) / 2 + v0 * sin(p0) .* t + y0; % y-coordinate trajectory

    % Plot the trajectories
    figure; hold on;
    plot(xp1, yp1, 'r', 'DisplayName', 'Trajectory 1');
    plot(xp2, yp2, 'b', 'DisplayName', 'Trajectory 2');
    
    % Pursuer's dynamics initialization
    x = x0; % Initial x-coordinate of the pursuer
    y = y0; % Initial y-coordinate of the pursuer
    dxdt = 0; % Initial velocity in x-direction
    dydt = 0; % Initial velocity in y-direction
    
    % Arrays to store pursuer positions
    xs = zeros(size(t));
    ys = zeros(size(t));
    
    for i = 1:length(t)
        % Compute distance to xp1 and yp1
        dist_x = xp1(i) - x;
        dist_y = yp1(i) - y;
        dist_sq = dist_x^2 + dist_y^2;
        
        % Update accelerations based on dynamic equations
        ddxdt = ((dydt * (v0 * cos(p0))) - (dxdt * (0))) - g; 
        ddxdt = ddxdt * (dist_x / dist_sq) + (dxdt * (kv - F) / (m * sqrt(dxdt^2 + dydt^2)));

        ddy = (g - (dydt * (v0 * cos(p0))) - (dxdt * (0))) * (dist_y / dist_sq);
        ddy = ddy + (dydt * (kv - F) / (m * sqrt(dxdt^2 + dydt^2)));

        % Integrate to get new velocities
        dxdt = dxdt + ddxdt * dt;
        dydt = dydt + ddy * dt;

        % Integrate to get new positions
        x = x + dxdt * dt;
        y = y + dydt * dt;

        % Store positions
        xs(i) = x;
        ys(i) = y;
    end

    % Plot the pursuer's trajectory
    plot(xs, ys, 'g', 'DisplayName', 'Pursuer Trajectory');
    xlabel('X Position');
    ylabel('Y Position');
    title('Object Trajectories and Pursuer Path');
    legend;
    grid on;
    hold off;
end

Key Steps Explained

  • Input Validation: Ensures the parameters are of the right type and within expected bounds.
  • Trajectory Calculations: Computes the positions for the projectile trajectories based on initial conditions and time.
  • Pursuer Dynamics: Calculates accelerations based on the equations provided, integrating them iteratively to get new velocities and positions.
  • Plotting: Visualizes both the trajectories and the pursuer's path in a single plot with appropriate labels and legends.

Code Usage Example

To utilize the function, invoke it with the desired parameters as shown below:

% Example parameters
v0 = 20;     % Initial velocity
p0 = pi/4;   % 45 degrees
x0 = 0;      % Initial x-coordinate
y0 = 0;      % Initial y-coordinate
F = 5;       % Force
m = 1;       % Mass
kv = 0.1;    % Velocity constant
g = 9.81;    % Acceleration due to gravity
t_end = 10;  % End time
dt = 0.01;   % Time step

% Call the function
plotTrajectories(v0, p0, x0, y0, F, m, kv, g, t_end, dt);

Conclusion

The plotTrajectories function intelligently combines kinematics and physics to visualize the motion of two objects and a pursuee’s trajectory under defined forces. It serves as a practical tool in modeling physics-based motion scenarios in MATLAB.

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 visualizes the trajectories of two objects based on initial conditions and computes a pursuer's dynamics influenced by gravitational and acceleration factors, offering a comprehensive analysis of motion scenarios.