FINAL INDEPENDENT PROJECT
Time-based simulations of projectile motion
Introduction:
Projectile motion is the motion of an object thrown or projected into the air, subject to only
the acceleration of gravity. The object is called a projectile, and its path is called its trajectory. The motion of falling objects, as covered in Problem-Solving Basics for One-Dimensional Kinematics, is a simple one-dimensional type of projectile motion in which there is no horizontal movement. In this section, we consider two-dimensional projectile motion, such as that of a football or other object for which air resistance is negligible.
Figure 1. The total displacement s of a soccer ball at a point along its path. The vector s has components x and y along the horizontal and vertical axes. Its magnitude is s, and it makes an angle θ with the horizontal.
Objectives:
- Projectile motion is the motion of an object subject only to the acceleration of gravity, where the acceleration is constant, as near the surface of Earth.
- To solve projectile motion problems, we analyze the motion of the projectile in the horizontal and vertical directions using the one-dimensional kinematic equations for xand y.
- The time of flight of a projectile launched with initial vertical velocity
on an even surface is given by
This equation is valid only when the projectile lands at the same elevation from which it was launched.
- The maximum horizontal distance travelled by a projectile is called the range. Again, the equation for range is valid only when the projectile lands at the same elevation from which it was launched.
Projectile Motion Formula or trajectory formula is formulated as
Where,
Vx = the velocity (along the x-axis)
Vxo = Initial velocity (along the x-axis)
Vy = velocity (along the y-axis)
Vyo = Initial velocity (along the y-axis)
g = acceleration due to gravity
t = Time taken
Equations related to the projectile motion is given as
Where
v0 = Initial Velocity
sin θ = Component along the y-axis
cos θ = component along the x-axis
The formula of projectile motion is used to calculate the velocity, distance and the time observed in the projectile motion of the object.
Computational procedure:
Step 1.
Free up system memory and set the output format. The ‘Editor Window’ (for a new M-File) is opened in MATLAB. All of the current contents of system memory and all variables in the ‘Command Window’ are erased, and the visual ‘Command Window’ and command output are customized using the built-in functions clear (which releases all variables from the current ‘Workspace’ and from system memory), clc (which clears all input and output commands from the display and homes the cursor), and format short (which sets the output display format to a scaled fixed-decimal format with four digits after the decimal point).
Step 2.
Set the input parameters. In the ‘Editor Window’, an assignment is made for each input variable using the built-in function input: for projectile motion—launch angles (theta1, theta2, theta3), initial speeds (v1,v2,v3…..vn), number n of balls projected, and gravitational acceleration g.
Step 3. Define the formulas. The equations are coded using different built-in functions according to the symbolic and numerical methods used: (a) for projectile motion—the horizontal and vertical components of the initial velocities υ0x and υ0y, total flight time T, range R, and maximum height H of each ball are computed using the built-in functions cosd (cosine of the argument in degrees), sind (sine of the argument in degrees), and max (returns the largest element in an array).
Step 4. Output the results. Output formats are created using the built-in function fprintf. This function specifies a floating-point number %d1.d2 with a width of d1 digits having d2 digits after the decimal point. It can also include the control character /n to start a new line. After running the scripts in the ‘Editor Window’, the resulting numeric and character arrays are sent to the ‘Command Window’ and stored in the ‘Workspace’.
Step 5. Construct the animations. Primitive line objects, in terms of which time-based motions are animated, are created using the built-in function line using data stored in vectors x and y. As options in the line command, ‘LineStyle’ such as ‘-’, ‘LineWidth’ such as ‘2’, ‘Color’ such as ‘r’, ‘Marker’ such as ‘.’, and ‘MarkerSize’ such as ‘40’ are defined to control the appearance and behavior of the lines and markers. Additionally, the following built-in functions are introduced to enhance the simulation environment: axis (which sets the axis limits and aspect ratios), axis equal (which uses the same length for the data units along each axis), title (which adds a specified title to the axes or chart), xlabel (which labels the x-axis of the current axes or chart), ylabel (which labels the y-axis of the current axes or chart), box on (which displays a box around the current axes), grid on (which displaysmajorgridlinesfor the current axes or chart),set (which specifies a value for the property name of the identified object), gca (which returns the current axes or chart for the current figure), findall (which finds a text object for the x-axis label), gcf (which returns the current figure handle), hold on (which holds the current plot in memory while adding new curves to it), sprintf (which formats a data string), and pause (which temporarily stops execution).
Step 6. Animate the motions. In the final step, ‘for index = values, statements, end’ loops are constructed to execute a group of statements a specified number of times. For the present problems, values are created as time vectors by using the built-in function linspace (which returns a row vector of n evenly spaced points in the interval [0, T] for projectile motion, [θ0, −θ0]. This function is used to display the property names and values of the line objects created in Step 5. For the projectile motion and pendulum oscillation, the function plot creates a two-dimensional (2D) line graph of the y data versus the corresponding x values. To simulate the projectile motion the sprint function is used to format the time data as a string. In all scripts, the built-in function M(r)=get frame is implemented
To capture the current axes as it appears on the screen as a movie frame. Here M is a structure containing the image data, and r denotes its index.
Matlab Code:
%Script for the projectile motion problem of 3 balls having different initial speeds and launch angles.
%Ball motion problem (simulation of n balls)
%STEP 1
clear all
close all
clc
format short
disp(datestr(now,0));
%STEP 2
theta1=input('Enter the angle (deg) between velocity vector and ground?=');
v01=input('Enter initial velocity (m/s)?=');
theta2=input('Enter the angle (deg) between velocity vector and ground?=');
v02=input('Enter initial velocity (m/s)?=');
theta3=input('Enter the angle (deg) between velocity vector and ground?=');
v03=input('Enter initial velocity (m/s)?=');
tic;
r=1;
%STEP 3
%Horizontal and vertical components of the velocity vectors
vx1=v01*cosd(theta1);
vy1=v01*sind(theta1);
vx2=v02*cosd(theta2);
vy2=v02*sind(theta2);
vx3=v03*cosd(theta3);
vy3=v03*sind(theta3);
%Total ?ight times (sec) for the motion
g=9.807; %Gravitational acceleration (m/s2)
tf1=2*vy1/g; %#ok<*NASGU>
tf2=2*vy2/g;
tf1=2*vy1/g;
tf3=2*vy3/g;
%Maximum horizontal and vertical ranges (meters) for the motion
xmax1=vx1*tf1;
hmax1=vy1*(tf1/2)-0.5*g*(tf1/2)^2;
xmax2=vx2*tf2;
hmax2=vy2*(tf2/2)-0.5*g*(tf2/2)^2;
xmax3=vx3*tf3;
hmax3=vy3*(tf3/2)-0.5*g*(tf3/2)^2;
%STEP 4
table1=[theta1;v01;vx1;vy1;tf1;xmax1;hmax1];
disp('theta1 v01 vx1 vy1 tf1 xmax1 hmax1');
fprintf('%10.0f %6.0f %9.4f %9.4f %9.4f %9.4f %9.4f',table1);
disp('————————————————————————')
table2=[[theta2;v02;vx2;vy2;tf2;xmax2;hmax2]];
disp('theta2 v02 vx2 vy2 tf2 xmax2 hmax2');
fprintf('%10.0f %6.0f %9.4f %9.4f %9.4f %9.4f %9.4f',table2);
disp('————————————————————————');
table3=[theta3;v03;vx3;vy3;tf3;xmax3;hmax3];
disp('theta3 v03 vx3 vy3 tf3 xmax3 hmax3');
fprintf('%10.0f %6.0f %9.4f %9.4f %9.4f %9.4f %9.4f',table3);
disp('————————————————————————')
toc;
%STEP 5
xmax=max([xmax1,xmax2,xmax3]);
hmax=max([hmax1,hmax2,hmax3]);
f1=line([0 xmax],[00],'linestyle','-','color','b','linewidth',4);
f11=line(0,0,'linestyle','none','marker','.','markersize',70,'color','b');
f12=line(0,0,'linestyle','none','marker','.','markersize',60,'color','r');
f13=line(0,0,'linestyle','none','marker','.','markersize',40,'color','k');
axis([0 xmax 0 hmax]);
%title('Projectile Motion')
xlabel('\itHorizontal range(m)','FontSize',14,'FontWeight','Bold');
ylabel('\itVertical range(m)','FontSize',14,'FontWeight','Bold');
box on;
grid on;
set(gca,'FontSize',14,'FontWeight','Bold');
set(findall(gcf,'type','text'),'FontSize',14,'FontWeight','Bold');
hold on;
tt=title(sprintf('Maximum hight time: %0.4f sec',0),'FontSize',14);
pause(5);
%STEP 6
tmax=max([tf1,tf2,tf3]);
t=linspace(0,tmax,200);
for r=1:length(t)
x1(r)=vx1*t(r);
h1(r)=vy1*t(r)-0.5*g*t(r)^2;
x2(r)=vx2*t(r);
h2(r)=vy2*t(r)-0.5*g*t(r)^2;
x3(r)=vx3*t(r);
h3(r)=vy3*t(r)-0.5*g*t(r)^2;
plot(x1(r),h1(r),'.b',x2(r),h2(r),'.r',x3(r),h3(r),'.k','LineWidth',1);
set(f1,'xdata',[0 xmax],'ydata',[00]);
set(f11,'xdata',x1(r),'ydata',h1(r));
set(f12,'xdata',x2(r),'ydata',h2(r));
set(f13,'xdata',x3(r),'ydata',h3(r));
set(tt,'String',sprintf('Maximum hight time: %0.4f sec', t(r)));
M(r)=getframe;
r=r+1;
end
movie(M)
videofile=VideoWriter('projectilemotion.avi','uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)
Fig: a screenshot at the end of the animation
Fig: a screenshot taken at an intermediate point during the animation
Fig: the contents of the ‘Workspace’ window after completion of the script.
References:
https://opentextbc.ca/universityphysicsv1openstax/chapter/4-3-projectile-motion/