Question: check the code: function project _ 5 ( ) % PROJECT _ 5 project _ 5 ( ) models the path of a projectile launched

check the code:
function project_5()
% PROJECT_5 project_5() models the path of a projectile launched
% from a raised platform.
%
% Name: Adham Elmezayen
% Date: 3 October 2024
% Class: CMPSC 200
% Print the Splash Screen
fprintf('-------------------------------------------------
');
fprintf('Welcome to Projectile Motion Simulation (Project 5)
');
fprintf('-------------------------------------------------
');
% Prompt the user for inputs for velocity, angle, and height above the ground
v0= input('Enter the initial velocity (m/s): ');
theta = input('Enter the launch angle (degrees): ');
h = input('Enter the height above the ground (m): ');
% Convert the launch angle to radians
theta_rad = deg2rad(theta);
% Decompose the velocity into x and y components
vx0= v0* cos(theta_rad); % Horizontal component
vy0= v0* sin(theta_rad); % Vertical component
% Acceleration due to gravity
g =9.81; % m/s^2
% Calculate the time to impact (t_f) using the quadratic formula
% The equation is: y(t)= h + vy0* t -(1/2)* g * t^2
% We set y(t)=0 to solve for t when the projectile hits the ground
% Coefficients for the quadratic equation: (1/2)* g * t^2- vy0* t - h =0
a =-0.5* g;
b = vy0;
c = h;
% Using the quadratic formula: t =(-b \pm sqrt(b^2-4ac))/2a
discriminant = b^2-4* a * c;
if discriminant <0
error('No real solution for time to impact. Check inputs.');
end
t_f =(-b + sqrt(discriminant))/(2* a); % Taking the positive root
% Create additional time variables for different points
t_0=0; % Initial time
t_1= t_f /4; %1/4th of the flight time
t_2= t_f /2; % Half of the flight time
t_3=3* t_f /4; %3/4th of the flight time
t_4= t_f; % Final time (impact)
% Time array
t =[t_0, t_1, t_2, t_3, t_4];
% Calculate the five horizontal and vertical positions
x = vx0* t; % Horizontal position: x = vx0* t
y = h + vy0* t -0.5* g * t.^2; % Vertical position: y = h + vy0* t -(1/2)* g * t^2
% Calculate the five horizontal and vertical velocities
vx = vx0* ones(size(t)); % Horizontal velocity remains constant
vy = vy0- g * t; % Vertical velocity: vy = vy0- g * t
% Print the results table
fprintf('
Projectile Motion Results:
');
fprintf('Initial velocity: %.2f m/s
', v0);
fprintf('Launch angle: %.2f degrees
', theta);
fprintf('Initial height: %.2f m
', h);
fprintf('
%-10s %-10s %-10s %-10s %-10s
', 'Time (s)','X (m)','Y (m)','Vx (m/s)','Vy (m/s)');
% Print the results for each time step
for i =1:length(t)
fprintf('%-10.2f %-10.2f %-10.2f %-10.2f %-10.2f
', t(i), x(i), y(i), vx(i), vy(i));
end

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!