Function to Compute Full Projectile Trajectory

 

 

function [x,y,t]=trajectory(angd,v,numpts)

%

% [x,y,t]=trajectory(angd,v,numpts)

%

% computes the trajectory of a projectile launched

% and an angle angd (in degrees) and initial velovity v.

% The trajectory is displayed in a new figure window.

%

% x                 - output horizontal motion trajectory (feet)

% y                 - output vertical motion trajectory (feet)

% t                  - output time array corresponding to x & y (seconds)

% angd      - input single launch angle (degrees)

% v                 - input initial velocity (feet/sec)

% numpts - input # of times to evaluate in total trajectory

%

% Author: Dr. Russell Hardie

% Date: 1/15/01

 

 

% Get the constants we need

g=32.17;                                                        % acceleration due to gravity (ft/sec^2)

vx=v*cos(deg2rad(angd));              % horizontal initial velocity (we are calling our function!)

vy=v*sin(deg2rad(angd));              % vertical initial velocity

 

% Find the time we hit ground

tg=2*vy/g;

 

% make time array

t=linspace(0,tg,numpts);

 

% vertical path (text P. 3.5-3, p. 128)

y=vy*t-.5*g*t.^2;

 

% horizontal path (distance = velocity  x time)

x=vx*t;

 

% plot results on a new figure

figure

plot(x,y)

xlabel('x (ft)');

ylabel('y (ft)');

title('Projectile Trajectory');

axis equal

 

 

 

·           Take it for a spin…

 

>>       [x,y,t]=trajectory(45,100,100);

 

>>       [x,y,t]=trajectory(65,100,100);

 

·           When does it hit its peak altitude?

 

>>       [maxY,n]=max(y);

>>       t(n)

 

·           How many feet out is it when it his this maximum?

 

>>      x(n)

 

·           If you don’t plan to make use of the arrays x, y and t you can simply call the function with no returned arguments

 

>>       trajectory(45,100,100);

 

·           If you only want the first two

 

>>       [x,y]=trajectory(65,100,100);

 

·           Don’t do this, or t will really be y (the second output argument)

 

>>       [x,t]=trajectory(65,100,100);

 

·           Later we will learn how to make an animation of the trajectory using the getframe and movie( ) commands.  Be sure to save all these functions, as we will use them later.