Section 8.8 Function Example: Projectile Motion
Suppose a projectile is shot at an angle \(\theta\) to the horizontal and with initial velocity \(v_o\text{.}\) You wish to find the maximum height \(h_{max}\) the projectile will reach as well as the distance (range) \(d_{max}\) it will fly. You wish to do so on several different planets and so you need to take into consideration the acceleration due to gravity, g. Below is a picture describing the situation:
To solve this problem we separate the problem into x (horizontal) and y (vertical) components:
x-components: \(v_{ox}\) is the component of the initial velocity pointing in the horizontal direction. Neglecting wind resistance we can assume this to be constant over time so that the distance x flown after time t has passed is simply \(v_{ox}t\text{:}\)
\begin{equation*}
v_{ox} = v_o\,\cos(\theta)
\end{equation*}
\begin{equation*}
x = v_{ox}t
\end{equation*}
y-components: \(v_{oy}\) is the component of the initial velocity pointing in the vertical direction. The acceleration due to gravity changes this velocity so that at time t we have the remaining velocity \(v_y = v_{oy}-gt\text{.}\)
\begin{equation*}
v_{oy} = v_o\,\sin(\theta)
\end{equation*}
\begin{equation*}
v_y = v_{oy}-gt
\end{equation*}
The height of the projectile at time t is thus
\begin{equation*}
y = v_{oy}t-g\frac{t^2}{2}
\end{equation*}
The projectile reaches its maximum height when its motion changes from upward to downward, that is, when \(v_y=0\text{.}\) Solving for t yields:
\begin{equation*}
t_{hmax} = \frac{v_{oy}}{g}
\end{equation*}
This yields a maximum height and distance flown of
\begin{equation*}
h_{max} = \frac{v_{oy}^2}{2g} \text{ and } d_{max} = v_{ox}2t_{hmax} = \frac{2v_{ox}v_{oy}}{g}
\end{equation*}
Here is the MATLAB implementation which additionally plots the motion of the projectile:
function[hmax,rmax] = trajectory(v0,theta,g);
% trajectory computes max height and dist. of a projectile
% Inputs:
% v0 - initial speed of projectile (m/s)
% theta - initial angle of projectile (degrees)
% g - acceleration due to gravity (m/s^2)
% Outputs:
% hmax - maximum altitude of projectile (m)
% rmax - maximum range of projectile (m)
v0x = v0*cos(theta*pi/180); % convert to radians
v0y = v0*sin(theta*pi/180); % convert to radians
tmax = v0y/g; % time to reach apex
hmax = v0y^2/(2*g); % maximum altitude assigned
ttot = 2*tmax; % total flight time
rmax = v0x * ttot; % maximum range assigned
tplot = linspace(0,ttot,200); % for plotting
x = v0x * tplot; % ranges for plotting
y = v0y * tplot -.5*g*tplot.^2; % altitudes for plotting
plot(x,y); % trajectory plot
xlabel('Distance (m)');
ylabel('Height (m)');