## Homework Solution on Programming with MATLAB

• 17th Jun, 2022
• 15:28 PM
```% Modified for problem-2 HW-4 (Problem Chapter 3, Version 09)
%   Parabolic flight of a projectile
%

% cleanup memory
clear all;

% clear screen
clc;

% gravitational acceleration
g = 9.81;   % m/s^2
% Give input pitch velocity in mph
Vo    = input (' Enter pitch velocity in mph: ');    % m/s
Vo    = Vo*0.44704; %convert mph to m/s

fprintf ('\n \t theta, deg \t maximum range, m  ');
fprintf ('\n\t ______________________________________________');
% let's try a range of theta
for theta = 10: 10: 80

% the projectile leaves from the ground
x0 = 0;
y0 = 4; %in ft
y0 = y0/3.28; % convert to m;
% velocity components
% Vox  = Vo cos(theta)
Vox = Vo * cosd(theta); % m/s
% Voy = Vo sin(theta)
Voy = Vo * sind(theta); % m/s
% print headers

t1 = 0;
t2 = Voy/((1/2)*g);

t_flight = max(t1, t2);

% use linspace this time
t = linspace (0, t_flight, 21)';

% x(t) = x0 + Vox  t
x = x0 + Vox.*t;

% % y(t) = y0 + Voy t - (1/2) g t^2
% y = y0 + Voy.*t - (1/2)*g*t.^2;

y = y0 + Voy.*t - (1/2).*g*t.^2;

%The distance r to the projectile at time t can be calculated by
% r(t) = sqrt(x(t)^2 +y(t)^2)

r = sqrt(x.^2 + y.^2);

Results = [t, x, y, r];

% find maximum and corresponding x and t
[hmax, imax] = max(y);

[rmax, imax] = max(r);

% fprintf (' \n\t The maximum distance of %7.2f m ', rmax);
% fprintf ('\n \t\t is reached after %3i s ', t(imax));

% fprintf ('\n\t ______________________________________________ \n');
fprintf ('\n \t %7.2f \t\t %7.2f ', theta,rmax');
% plots

% make a plot of height (t,y)
subplot (2,2,1), plot (t,y);
title('Fig.1: y variation with t')
xlabel('t, s')
ylabel('y, m')
hold on

% make a plot (x,y) of the trajectory
subplot (2,2,2), plot (x,y);
title('Fig.2: y variation with x')
xlabel('x, m')
ylabel('y, m')
hold on

% make a plot of reach (t,r)
subplot (2,2,3), plot (t, r);
title('Fig.3: r variation with t')
xlabel('t, s')
ylabel('r, m')
hold on

pause (0.5)

end

fprintf ('\n\t ______________________________________________ \n');
hold off```