Write a MATLAB Graphical User interface (GUI) to simulate and plot the projectil
ID: 3863004 • Letter: W
Question
Explanation / Answer
Projectile: run this code only.. and also save another two file with same name I have written.
% Numerical on Bouncing Projectile
%--------------------------------------------------------------------------
clc
close all;
clear all;
%--------------------------------------------------------------------------
v0 = 10; % meters/second: Initial velocity
theta = 45; % DEGREES: Initial angle of projectile
K = 0.2; % Linear AIR Drag Coefficient (a constant)
Reflect = 0.5; % 0.5 to 1 : Reflection coefficient of the surface
nt = 1000; % Number of time steps
%--------------------------------------------------------------------------
% Numerically computed values
[N,Nd,Nt] = numerical2(v0,theta,nt,K,Reflect);
% Algorithm can be found in the function " numerical2.m "
x = N(1,:);
y = N(2,:);
vx = N(3,:);
vy = N(4,:);
xd = Nd(1,:);
yd = Nd(2,:);
vxd = Nd(3,:);
vyd = Nd(4,:);
%--------------------------------------------------------------------------
% This is for Figure(1)
% animate(x,y,xd,yd,K);
%--------------------------------------------------------------------------
% TO COMPARE NUMERICAL SOLUTIONS with and without air drag
figure(2)
plot(x,y,'r','linewidth',2); % Without drag
hold on;
plot(xd,yd,'linewidth',2); % With drag
grid on;
axis([0 ceil(max(x)) 0 ceil(max(y))]);
xlabel('Range in meters','FontSize',15);
ylabel('Height in meters','FontSize',15);
title('Projectile Motion with LINEAR Air Drag coefficient K' ,'FontSize',15);
ns = ['K = ', num2str(K)];
legend('K = 0',ns);
fh = figure(2);
set(fh, 'color', 'white');
h = gca;
set(h,'FontSize',14);
% %--------------------------------------------------------------------------
% NUMERICAL COMPUTATION OF PROJECTILE MOTION
% This function computes the trajectory (path) of a projectile for 2 cases:
%--------------------------------------------------------------------------
% case 1: GOVERNING EQUATION WITHOUT AIR DRAG
%
% mx" = 0
% my" = -mg'
%--------------------------------------------------------------------------
% case 2: GOVERNING EQUATION WITH AIR DRAG
%
% mx" = -Kmx' where x' = vx
% my" = -Kmy'-mg where y' = vy, K = DRAG COEFFICIENT
%--------------------------------------------------------------------------
% INPUT values :
% v0 = Initial velocity in meters/second
% theta = Initial angle of projectile in DEGREES
% N = Number of Time steps
% K : Drag Coefficient ( a constant)
%--------------------------------------------------------------------------
% OUTPUT values :
% r is a 2D matrix without Air Drag
% rd is a 2D matrix with Air drag
%
% r/rd(1,:) gives x ----> x coordinate
% r/rd(2,:) gives y ----> y coordinate
% r/rd(3,:) gives vx ---> Velocity in x-direction
% r/rd(4,:) gives vy ---> Velocity in y-direction
%--------------------------------------------------------------------------
%function [r,rd,t] = numerical(v0,theta,dt,K)
function [r,rd,t] = numerical2(v0,theta,N,K,Reflect)
g = 9.8; % Acceleration due to gravity in meters/second
Tmax = 2*v0*sind(theta)/g; % seconds: Total time of flight
U = v0*cosd(theta); % m/s: Initial Velocity in x-direction
V = v0*sind(theta); % m/s: Initial Velocity in x-direction
Reflect = 0.99*Reflect;
t = linspace(0,1.5*Tmax,N); % seconds: Time axis
dt = t(2)-t(1); % seconds: Time step
%--------------------------------------------------------------------------
% Initialization
%--------------------------------------------------------------------------
vx = zeros(1,N);
vy = zeros(1,N);
x = zeros(1,N);
y = zeros(1,N);
vxd = zeros(1,N);
vyd = zeros(1,N);
xd = zeros(1,N);
yd = zeros(1,N);
vx(1) = U;
vxd(1) = U;
vy(1) = V;
vyd(1) = V;
x(1) = 0;
y(1) = 0;
%--------------------------------------------------------------------------
% Numerically Calculated Parameters
%--------------------------------------------------------------------------
for n = 2:N
%----------- WITHOUT DRAG ( k = 0) ---------
vx(n) = vx(n-1); % m/s: Velocity in x-direction
x(n) = vx(n)*dt+x(n-1); % meters: x-coordinate
% ----------------------------- ----------------------------- ----------------------------- CHANGES
if (y(n-1)>=0) % Before the bouncing point
vy(n) = vy(n-1)-g*dt; % m/s: Velocity in y-direction
y(n) = vy(n)*dt+y(n-1); % meters: y-coordinate
elseif (n>2 && y(n-1)<=0) % At the Bouncing Point
vy(n) = -Reflect*(vy(n-1)-g*dt); % m/s: Velocity in y-direction
y(n) = -Reflect*y(n-1);
end
%--------------------------------------------------------------------------
%----------- WITH DRAG ( k > 0) ---------
vxd(n) = vxd(n-1)/(1+K*dt); % m/s: Velocity in x-direction
xd(n) = vxd(n)*dt+xd(n-1); % meters: x-coordinate
% ----------------------------- ----------------------------- ----------------------------- CHANGES
if (yd(n-1)>=0) % Before the bouncing point
vyd(n) = (vyd(n-1)-g*dt)/(1+K*dt); % m/s: Velocity in y-direction
yd(n) = vyd(n)*dt+yd(n-1); % meters: y-coordinate
elseif (n>2 && yd(n-1)<=0) % At the Bouncing Point
vyd(n) = -Reflect*((vyd(n-1)-g*dt)/(1+K*dt)); % m/s: Velocity in y-direction
yd(n) = -Reflect*yd(n-1);
end
%--------------------------------------------------------------------------
end
%--------------------------------------------------------------------------
% RETURN OUTPUT
%--------------------------------------------------------------------------
r(1,:) = x;
r(2,:) = y;
r(3,:) = vx;
r(4,:) = vy;
rd(1,:) = xd;
rd(2,:) = yd;
rd(3,:) = vxd;
rd(4,:) = vyd;
%--------------------------------------------------------------------------
end
function out = animate(x_axis,y_axis,xd,yd,K)
N1 = length(x_axis);
aviobj = avifile('Projectile.avi','compression','None','fps',50);
figure(1)
fh = figure(1);
set(fh,'Position',[10 50 810 390]);
for s = 1:N1
plot(x_axis(s),y_axis(s),'o','Linewidth',20); hold on;
plot(xd(s),yd(s),'ro','Linewidth',20);
ns = ['K = ', num2str(K)];
legend('K = 0',ns);
plot(x_axis(1:s),y_axis(1:s),'k-','Linewidth',2);
plot(xd(1:s),yd(1:s),'k-','Linewidth',2); hold off;
grid on;
axis([0 ceil(max(x_axis)) 0 ceil(max(y_axis))]);
xlabel('Range in meters','FontSize',20);
ylabel('Height in meters','FontSize',20);
title('Projectile Motion with LINEAR Air Drag coefficient K' ,'fontsize',14);
set(fh, 'color', 'white');
h = gca;
set(h,'FontSize',14);
F = getframe(fh);
aviobj = addframe(aviobj,F);
end % End For loop
out =0;
close(fh);
end % End of function animate.m
Note: Run only projectile.m and u can change the value in the same file.. do not touch other file…
This is the Another new code hope this will be more better than previous
Simply open matlab and run function projectile_motion.m
function projectile_motion
% This program is used to view the path & distance covered by a particle in
% animated form.
%
% Execute function in command window it will prompt yout to enter values
% required by program, after getting input parameters it will calculate the
% equations for projectile motion then it will show the animated path and
% distance covered by particle.
%
% Created by Awanit kumar
% Date 17.03.2017
%% Input Parameters for projrctile motion
theta = input('Enter launch angle= '); %Launch angle, theta (degrees)
m = input('Enter mass of the body= '); %Mass, m (grams)
Fs = input('Enter constant force spring force= '); %Constant force spring force, Fs (N)
Ns = input('Enter number of springs= '); %Number of springs, Ns
LL = input('Enter distance over which force is applied= '); %Distance over which force is applied, LL (mm)
eta = input('Enter efficiency= '); %efficiency, eta
g = input('Enter acceleration due to gravity= '); %Acceleration due to gravity, g (m/s^2)
N = input('Enter no. of steps= ');
%% Calcultion for projectile motion
V = sqrt(2.*Ns.*Fs.*LL.*eta./m); %Launch velocity, V (m/s)
Vy = V.*sind(theta); %Vertical velocity component, Vy (m/s)
Vx = V.*cosd(theta); %Horizontal velocity component, Vx (m/s)
tmh = V.*sind(theta)./g; % Time to maximum height, tmh (seconds)
ttotal = 2.*tmh; % Total travel time, ttotal (seconds)
ymax = (V.^2).*(sind(theta).^2)./(2.*g); %Maximum height, ymax (m)
xmax = (V.^2).*(sind(2.*theta))./g; %Maximum range, xmax (m)
tinc = ttotal./N; %Time increment, tinc
%% Preallocation of answer spaces
time = zeros(N,1); %Preallocation for time
xpos = zeros(N,1); %Preallocation for X-Position
ypos = zeros(N,1); %Preallocation for Y-Position
%% Loop for generation of time vector
for l = 1:N-1
time(l+1) = time(l)+tinc;
end
%% Loop for generation of x-position & y-position vector
for j = 1:N
xpos(j) = time(j).*Vx;
ypos(j) = V.*time(j).*sind(theta)-0.5.*g.*time(j).^2;
end
%% Loop for visualization of plot
for k = 1:N
hold all
h = plot(time(k),xpos(k),'p',time(k),ypos(k),'o',...
'LineWidth',2,'MarkerEdgeColor','g',...
'MarkerFaceColor','y','MarkerSize',2+k);
pause(0.6)
drawnow
end
%% Annotation of graph
legend('Distance','Path','Location','NorthWest')
title('Projetile Trajectory','FontSize',14,'FontWeight','Bold','FontName','Calibri')
ylabel('Distance(m)','FontSize',12,'FontWeight','Bold','FontName','Calibri')
xlabel('Time(s)','FontSize',12,'FontWeight','Bold','FontName','Calibri')
grid on
save it as projectile_motion.m
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.