Answer on Question #52173, Programming, Mat LAB | Mathematica | MathCAD | Maple
Create a function to plot the output of projectile motion with inputs x0, y0, v0x and v0y. These correspond to the (x, y) coordinates of the projectile's start location, as well as the initial velocities in each direction. You may assume zero acceleration in the x-direction, and acceleration in the y-direction is -g where g=9.81.
Use a time series vector (1D array) to denote time, and two vectors to denote the movement in the x-direction and y-direction. Your plot should only show the trajectory until the projectile object reaches the ground.
Solution:
Projectile motion is a form of motion in which an object or particle (called a projectile) is thrown near the earth's surface, and it moves along a curved path under the action of gravity only.
In projectile motion, the horizontal motion and the vertical motion are independent of each other; that is, neither motion affects the other.
The horizontal component of the velocity of the object remains unchanged throughout the motion. The vertical component of the velocity increases linearly, because the acceleration due to gravity is constant (g=9.81 m/s²).
Equations related to trajectory motion are given by
where is the initial velocity.
function projectile(x0,y0,V0x,V0y)
g = -9.8;
% calculate time of flight
% Solve 0.5*g*t^2+V0y*t+y0=0
b=V0y;
a=g/2;
c=y0;
t_flight=(-b-sqrt(b^2-4*a*c))/(2*a);
%time series vector
t=linspace(0,t_flight,100);
x = x0 + V0x.* t;
y = y0+ V0y.* t + (1/2) * g.* t.^2;
plot(x, y, 'b-', 'LineWidth', 3);
axis equal;
xlabel('Distance (m) ');
ylabel('Height (m)');
title('Projectile motion');>> projectile(2,4,6,10);