plot trajectory based on x and y velocity in matlab

To plot trajectory based on x and y velocity in matlab, we need to first define the variables for x and y velocity, initial velocity and the angle of projection. We can use the following code to generate the trajectory plot:

main.m
% Define variables
u = 10; % Initial Velocity = 10 m/s
theta = 45; % Angle of projection = 45 degrees

% Calculate x and y velocities
ux = u*cosd(theta); % x component of velocity
uy = u*sind(theta); % y component of velocity

% Define time interval and time vector
dt = 0.01; % time interval = 0.01 s
t = 0:dt:2; % time vector from 0 to 2 seconds with interval of 0.01 s

% Calculate x and y positions
x = ux*t; % x position array
y = uy*t - 0.5*9.81*t.^2; % y position array, where g = 9.81 m/s^2

% Plot trajectory
plot(x,y,'LineWidth',2);
xlabel('X distance (m)');
ylabel('Y distance (m)');
title('Projectile Trajectory');
grid on;
636 chars
23 lines

In this code, we have defined the initial velocity of the projectile as u and the angle of projection as theta. We have then calculated the x and y components of velocity (ux and uy) and defined the time interval and time vector using dt and t. Using these values, we have then calculated the x and y positions of the projectile (x and y) using the projectile motion equations.

Finally, we have plotted the trajectory using the plot() function, with x and y as the input arrays. The rest of the code defines the axes labels and the plot title, and turns on the gridlines for better visualization.

gistlibby LogSnag