create a matlab script to calculate and plot the motion of an object in projecti
ID: 3111664 • Letter: C
Question
create a matlab script to calculate and plot the motion of an object in projectile motion. The script should prompt the user for the launch velocity, launch angle, and the length of time for the simulation, and then plot the y-position vs. x-position.
Ive done some of the code already but am having troubles getting it to work...can someone help me get this code to work right?
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Function to calculate and plot the trajectory of an object in projectile
% motion.
%
% Inputs:
% V_o - initial launch veloctiy (m/s)
% theta - launch angle (degrees)
% t_end - end time for trajectory calc (s)
%
% Ouputs:
% x - horizontal position time history x(t) (m)
% y - vertical position time history y(t) (m)
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Define input values
g = 9.81; %Acceleration in m/s^2
V_o = input('Enter initial launch velocity in meters/seconds: ');
theta = input('Enter launch angle in degrees: ');
t_end = input('Enter the time in seconds of landing: ');
% Initialize time array
% Calculate x and y position as function of time
x = V_o*cos(theta)*t_end;
y = -1/2*g*(t_end)^2 + V_o * sin(theta)*t_end;
% plot results
plot(x,y,'k:')
Explanation / Answer
MATLAB CODE
clc
clear all
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Function to calculate and plot the trajectory of an object in projectile
% motion.
%
% Inputs:
% V_o - initial launch veloctiy (m/s)
% theta - launch angle (degrees)
% t_end - end time for trajectory calc (s)
%
% Ouputs:
% x - horizontal position time history x(t) (m)
% y - vertical position time history y(t) (m)
% assuming projectile is projected from ground
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Define input values
g = 9.81; %Acceleration in m/s^2
V_o = input('Enter initial launch velocity in meters/seconds: ');
theta = input('Enter launch angle in degrees: ');
t_end = input('Enter the time in seconds of landing: ');
T = linspace(0,t_end,2000); % Initialize time array
angle = theta*pi/180; % angle in radians
% Calculate x and y position as function of time
x = V_o*cos(angle)*T;
y = -1/2*g*T.^2 + V_o * sin(angle)*T;
%% if projectile is projected through ground hence it can not go below the ground
% hence y can not be negative
for i = 1:length(y)
if y(i) < 0
y(i) = 0;
end
end
% plot results
plot(x,y,'ob',...
'LineWidth',1,...
'MarkerSize',2,...
'MarkerEdgeColor','b',...
'MarkerFaceColor','b')
title('Projectile Motion')
ylabel('Y - axis')
xlabel('X - axis')
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.